Spring+springmvc+mybatis整合案例
2016-06-11 00:27
567 查看
今天在整合过程中发生了很多问题,根本原因在于整合jar不完整,导致的NotFoundClass报错,这个错误很经典,一旦jar有误就会发生,我在这里粘贴上我的项目源码,以便于提供大家参考。
源码下载地址:http://download.csdn.net/detail/u013821825/9546000
项目中我用的是spring3.1.2+mybatis3.0.5整合的
项目结构:
1、数据准备
2、实体类
3、映射Mapper配置文件
4、映射接口
5、业务控制类
6、web.xml
7、spring位置文件
8、jsp文件
9、结果
源码下载地址:http://download.csdn.net/detail/u013821825/9546000
项目中我用的是spring3.1.2+mybatis3.0.5整合的
项目结构:
1、数据准备
create table CREATE TABLE `emp` ( `id` int(10) NOT NULL, `name` varchar(20) DEFAULT NULL, `age` int(3) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 INSERT INTO `test`.`emp`(`id`,`name`,`age`) VALUES ( '1','xsx','24'); INSERT INTO `test`.`emp`(`id`,`name`,`age`) VALUES ( '2','mpt','23');
2、实体类
package org.xsx.entity; public class Emp { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
3、映射Mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="org.xsx.dao.EmpDao"> <select id="findAll" resultType="org.xsx.entity.Emp"> select id,name,age from emp </select> </mapper>
4、映射接口
package org.xsx.dao; import java.util.List; import org.xsx.annotation.MyBatisRepository; import org.xsx.entity.Emp; /** * DAO组件 * @author xusha * */ @MyBatisRepository public interface EmpDao { public List<Emp> findAll(); }
5、业务控制类
package org.xsx.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.xsx.dao.EmpDao; import org.xsx.entity.Emp; @Controller @RequestMapping("emp") public class EmpController { @Resource private EmpDao empDao; @RequestMapping("/findEmp.do") public String find(Model model){ List<Emp> list = empDao.findAll(); model.addAttribute("emps", list); return "emp/emp_list"; } }
6、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>SpringMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
7、spring位置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <!-- property池启动时的初始值 --> <property name="password" value="root" /> <!-- 连接name="initialSize" value="${initialSize}"/> --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:org/xsx/entity/*.xml" /> </bean> <!-- 配置MyBatis注解 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.xsx.dao" /> <property name="annotationClass" value="org.xsx.annotation.MyBatisRepository" /> </bean> <!-- 开启注解扫描 --> <context:component-scan base-package="org.xsx" /> <!-- 开启ResultMapping注解 --> <mvc:annotation-driven /> <!-- 处理请求转发 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
8、jsp文件
<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head></head> <body> <table width="60%" cellpadding="2" cellspacing="0"> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> </tr> <c:forEach items="${emps}" var="e"> <tr> <td>${e.id }</td> <td>${e.name }</td> <td>${e.age }</td> </tr> </c:forEach> </table> </body> </html>
9、结果
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- Mybatis传递多个参数的解决办法(三种)
- 获取Java的MyBatis框架项目中的SqlSession的方法
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Mybatis逆工程jar包的修改和打包
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- springmvc 发送ajax出现中文乱码的解决方法汇总
- MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法