在上已个Java Spring MVC项目基础上加MyBatis
代码目录:
/Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC
1. 首先在resource目录加上jdbc.properties:
driverClasss=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://10.117.x.x:8306/springdemo?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull username=x password=x #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
2. 在pom.xml里面添加如下依赖:
<properties> <spring.version>4.2.6.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mysql-driver.version>5.1.29</mysql-driver.version> </properties> <!-- 添加mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 添加mybatis/spring整合包依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 添加mysql驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!-- 添加数据库连接池依赖 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency>
3. 把之前的applicationContext.xml配置 refactor成 spring-mybatis.xml,用refactor菜单的时候,web.xml里面如下也会改:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mybatis.xml</param-value> </context-param>
4. 最终写成的spring-mybatis.xml如下:
<?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/cache" xmlns:contex="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <contex:component-scan base-package="com.webapp.hello"/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClasss}"/> <property name="url" value="${jdbcUrl}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="initialSize" value="${initialSize}"/> <property name="maxActive" value="${maxActive}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> <property name="maxWait" value="${maxWait}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" value="dataSource"/> <property name="mapperLocations" value="classpath:mapping/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.webapp.hello.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
5. 几个代码文件如下:
HelloController
package com.webapp.hello; import com.webapp.hello.dao.UserDao; import com.webapp.hello.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource; import java.util.LinkedList; import java.util.List; /** * Created by baidu on 16/10/7. */ @Controller public class HelloController { @Resource UserDao userDao; @RequestMapping(value="/", method= RequestMethod.GET) public String index(ModelMap modelMap) { /*List userList = new LinkedList(); userList.add("张三"); userList.add("李四"); modelMap.addAttribute("userList", userList);*/ List<User> userList = userDao.selectAllUser(); modelMap.addAttribute("userList", userList); return "hello"; } }
model/User
package com.webapp.hello.model; /** * Created by baidu on 16/10/8. */ public class User { private Long id; private String userName; public String getUserName() { return userName; } }
dao/UserDao
package com.webapp.hello.dao; import com.webapp.hello.model.User; import org.springframework.stereotype.Repository; import java.util.List; /** * Created by baidu on 16/10/8. */ @Repository public interface UserDao { List<User> selectAllUser(); }
mapping/UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.webapp.hello.dao.UserDao">
<resultMap id="UserMap" type="com.webapp.hello.model.User">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="user_name" property="userName" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectAllUser" resultMap="UserMap">
SELECT id, user_name FROM t_user
</select>
</mapper>
Jsp文件,对user加上属性:
${user.userName}
运行之后,首先报的一个错,是针对sqlSessionFactory初始话,不能convert string to DataSource。对比排查后,在spring-mybatis.xml里面改了下面第二行的value变成 ref:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
</bean>
然后再运行,报错找不到class,是commons/pool等,应该就是commons-pool的依赖,所以在pom.xml加上对commons-pool的依赖:
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool --> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency>
然后再运行,就可以成功显示了: