实现mybatis与spring进行整合,通过spring管理SqlSessionFactory、mapper接口。
mybatis与spring整合jar
mybatis官方提供与mybatis与spring整合jar包:
还包括其它jar:
spring3.2.0
mybatis3.2.7
dbcp连接池
数据库驱动
参考:
mybatis与spring整合全部jar包
Mybatis配置文件
在classpath下创建mybatis/SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <mappers> <mapper resource="sqlmap/User.xml" /> <mapper resource="mapper/UserMapper.xml" /> </mappers></configuration>
Spring配置文件
在classpath下创建applicationContext.xml,定义数据库链接池、SqlSessionFactory。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
注意:在定义sqlSessionFactory时指定数据源dataSource和mybatis的配置文件。
1.原始dao开发(和spring整合后)
1.User.xml映射文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//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.huihui.pojo.User"> select * from user where id=#{id} </select></mapper>
在SqlMapconfig.xml中加载User.xml:
<mappers> <mapper resource="sqlmap/User.xml" /> </mappers>
2.UserDao接口:
public interface UserDao { public User findUserById(int id) throws Exception;}
3.UserDao接口的实现类:(需要继承SqlSessionDaoSupport)
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Override public User findUserById(int id) throws Exception { SqlSession sqlSession = this.getSqlSession(); User user = sqlSession.selectOne("test.findUserById", id); return user; }}
4.在applicationContext.xml配置文件中增加如下内容:
<bean id="userDao" class="com.huihui.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
5.测试:
public class UserDaoImplTest { private ApplicationContext applicationContext; @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test public void testFindUserById() throws Exception{ UserDao userdao = (UserDao)applicationContext.getBean("userDao"); User user = userdao.findUserById(1); System.out.println(user); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2.mapper代理的开放(与Spring整合后)
1.UserMapper.xml映射文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.huihui.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="com.huihui.pojo.User"> select * from user where id=#{id} </select></mapper>
在SqlMapconfig.xml中加载UserMapper.xml:
<mappers> <mapper resource="sqlmap/User.xml" /> <mapper resource="mapper/UserMapper.xml"/> </mappers>
2.mapper接口:
public interface UserMapper { public User findUserById(int id) throws Exception;}
3.在applicationContext.xml中通过MapperFactoryBean创建实体对象:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.huihui.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
4.测试:
@Test public void testFindUserByIdMapper() throws Exception{ UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper"); User user = userMapper.findUserById(1); System.out.println(user); }
上面的方法存在的问题:
需要针对每个mapper进行配置,麻烦。
解决方案:
通过MapperScannerConfigurer进行mapper扫描。
此方法即mapper接口开发方法,只需定义mapper接口,不用编写mapper接口实现类。只需要在spring配置文件中定义一个mapper扫描器,自动扫描包中的mapper接口生成代代理对象。
1、 mapper.xml文件编写
2、 定义mapper接口
注意mapper.xml的文件名和mapper的接口名称保持一致,且放在同一个目录
3、 配置mapper扫描器
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper接口包地址"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
basePackage:扫描包路径,中间可以用逗号或分号分隔定义多个包
4、 使用扫描器后从spring容器中获取mapper的实现对象
如果将mapper.xml和mapper接口的名称保持一致且放在一个目录 则不用在sqlMapConfig.xml中进行配置