整合思路:
需要spring通过单例方式管理SqlSessionFactory。
spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)
持久层的mapper都需要由spring进行管理。
1)加入jar包:
mybatis3.2.7的jar包;spring3.2.0的jar包;
mybatis和spring的整合包:mybatis-spring-1.2.2.jar
2)applicationContext.xml配置文件:
3)其他db.properties;log4j.properties和之前的一样;
一、使用原始dao接口开发(和spring整合后):
工程结构:
User.xml:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="test"> <select id="findUserById" parameterType="int" resultType="com.ssm.po.User"> SELECT * FROM USER WHERE id=#{value} </select> </mapper>
SqlMapConfig.xml配置(加入User.xml的映射):
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <typeAliases> 7 <package name="com.ssm.po"/> 8 </typeAliases> 9 10 <!-- 加载 映射文件 --> 11 <mappers> 12 <mapper resource="sqlMap/User.xml"/> 13 </mappers> 14 </configuration>
UserDao接口:
1 package com.ssm.dao; 2 3 import com.ssm.po.User; 4 5 public interface UserDao { 6 //根据id查询用户信息 7 public User findUserById(int id) throws Exception; 8 }
UserDaoImpl实现类(继承SqlSessionDaoSupport):
dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。
1 package com.ssm.dao; 2 3 import org.apache.ibatis.session.SqlSession; 4 import org.mybatis.spring.support.SqlSessionDaoSupport; 5 import com.ssm.po.User; 6 7 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { 8 9 @Override 10 public User findUserById(int id) throws Exception { 11 //继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSessoin 12 SqlSession sqlSession = this.getSqlSession(); 13 User user = sqlSession.selectOne("test.findUserById", id); 14 return user; 15 } 16 }
applicationContext.xml配置:
配置sqlSessionFactory、数据源、等、声明式配置UserDao的bean:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 15 16 <!-- 加载配置文件 --> 17 <context:property-placeholder location="classpath:db.properties" /> 18 19 <!-- 数据源,使用dbcp --> 20 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 21 <property name="driverClassName" value="${jdbc.driver}" /> 22 <property name="url" value="${jdbc.url}" /> 23 <property name="username" value="${jdbc.username}" /> 24 <property name="password" value="${jdbc.password}" /> 25 <property name="maxActive" value="10" /> 26 <property name="maxIdle" value="5" /> 27 </bean> 28 29 <!-- sqlSessionFactory --> 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <!-- 加载mybatis的配置文件 --> 32 <property name="configLocation" value="mybatis/SqlMapConfig.xml"></property> 33 <!-- 数据源 --> 34 <property name="dataSource" ref="dataSource"></property> 35 </bean> 36 37 <!-- 原始dao接口 --> 38 <bean id="userDao" class="com.ssm.dao.UserDaoImpl"> 39 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 40 </bean> 41 </beans>
测试程序:
1 package com.ssm.dao; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 import com.ssm.po.User; 8 9 public class UserDaoImplTest { 10 private ApplicationContext applicationContext; 11 12 //在setUp这个方法得到spring容器 13 @Before 14 public void setUp() throws Exception { 15 applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); 16 } 17 18 @Test 19 public void testFindUserById() throws Exception { 20 UserDao userDao = (UserDao) applicationContext.getBean("userDao"); 21 User user = userDao.findUserById(1); 22 System.out.println(user); 23 } 24 }
二、使用mapper接口代理的方式开发:
UserMapper.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.ssm.mapper.UserMapper"> 7 <select id="findUserById" parameterType="int" resultType="user"> 8 SELECT * FROM USER WHERE id=#{value} 9 </select> 10 </mapper>
UserMapper接口:和UserMapper.xml位于同目录下
1 package com.ssm.mapper; 2 3 import com.ssm.po.User; 4 5 public interface UserMapper { 6 //根据id查询用户信息 7 public User findUserById(int id) throws Exception; 8 }
在SqlMapConfig.xml中加载UserMapper.xml映射文件:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <typeAliases> 7 <package name="com.ssm.po"/> 8 </typeAliases> 9 10 <!-- 加载 映射文件 --> 11 <mappers> 12 <package name="com.ssm.mapper"/> 13 </mappers> 14 </configuration>
在applicationContext.xml中通过MapperFactoryBean创建代理对象:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 15 16 <!-- 加载配置文件 --> 17 <context:property-placeholder location="classpath:db.properties" /> 18 19 <!-- 数据源,使用dbcp --> 20 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 21 <property name="driverClassName" value="${jdbc.driver}" /> 22 <property name="url" value="${jdbc.url}" /> 23 <property name="username" value="${jdbc.username}" /> 24 <property name="password" value="${jdbc.password}" /> 25 <property name="maxActive" value="10" /> 26 <property name="maxIdle" value="5" /> 27 </bean> 28 29 <!-- sqlSessionFactory --> 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <!-- 加载mybatis的配置文件 --> 32 <property name="configLocation" value="mybatis/SqlMapConfig.xml"></property> 33 <!-- 数据源 --> 34 <property name="dataSource" ref="dataSource"></property> 35 </bean> 36 37 <!-- mapper配置 MapperFactoryBean:根据mapper接口生成代理对象--> 38 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 39 <!-- mapperInterface指定mapper接口 --> 40 <property name="mapperInterface" value="com.ssm.mapper.UserMapper"/> 41 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 42 </bean> 43 44 </beans>
测试代码:
1 package com.ssm.mapper; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import com.ssm.po.User; 9 10 public class UserMapperTest { 11 private ApplicationContext applicationContext; 12 13 @Before 14 public void setUp() throws Exception { 15 applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); 16 } 17 18 @Test 19 public void testFindUserById() throws Exception { 20 UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper"); 21 User user = userMapper.findUserById(1); 22 System.out.println(user); 23 } 24 25 }
针对上面的applicationContext.xml中通过MapperFactoryBean创建代理对象,需要针对每个mapper进行配置,比较麻烦,
优化:通过MapperScannerConfigurer进行Mapper扫描:
于是applicationContext.xml配置修改为:
1 <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 2 遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中 3 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) 4 --> 5 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 6 <!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔--> 7 <property name="basePackage" value="com.ssm.mapper"/> 8 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 9 </bean>
而且和spring整合后,使用mapper扫描器,SqlMapConfig.xm这里不需要配置了
<!-- <package name="com.ssm.mapper"/> -->
----------------------------------------------------------------------------------------------------------------------------------------------------------