转载请注明出处:
前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存
1.整合思路
需要Spring通过单例方式管理SqlSessionFactory。
Spring和MyBatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(Spring和MyBatis整合自动完成)
持久层的mapper都需要由Spring进行管理。
2.整合环境
创建一个java工程(接近实际开发的工程结构)
jar包:
mybatis3.2.7的jar包
spring3.2.0的jar包
dbcp连接池
数据库驱动
mybatis和spring的整合包:早期ibatis和spring整合是由spring官方提供,mybatis和spring整合由mybatis提供。


3.spring配置文件
在applicationContext.xml配置sqlSessionFactory和数据源。SqlSessionFactory在mybatis和spring的整合包下。
1 2 3 4 5 6 7 8 9 10 | <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
"><!-- 加载配置文件 --><context:property-placeholder location=" classpath:db.properties "/><!-- 数据库连接池,使用dbcp --><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><!-- SqlSessionFactory配置 --><!-- 让Spring管理SqlSessionFactory使用mybatis和spring整合包中的 --><bean id=" sqlSessionFactory " class=" org.mybatis.spring.SqlSessionFactoryBean "><!-- 数据库连接池 --><property name=" dataSource " ref=" dataSource " /><!-- 加载mybatis的全局配置文件 --><property name=" configLocation " value=" classpath:mybatis/SqlMapConfig.xml" /></bean></beans>
|
登录后复制
4.Mapper编写的三种方法
4.1原始dao开发(和spring整合后)
4.1.1User.xml
1 2 3 4 5 6 7 8 | <?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" ><!-- namespace 命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时, namespace 有特殊作用 --><mapper namespace = "test" ><!--在映射文件中配置很多sql语句 --><!--需求:通过id查询用户表的记录 --><!--id:标识映射文件中的sql,称为statement的id。将sql语句封装在mapperStatement的对象中,所有id称为Statement的id;
parameterType:指定输入参数的类型,这里指定int型;
#{}:表示一个占位符;
#{id}:其中id表示接收输入的参数,参数名称就是id,如果输入参数是简单类型,#{}中的参数名可以任意,可以是value或其它名称;
resultType:指定输出结果所映射的Java对象类型,select指定resultType表示将单条记录映射成Java对象。 --><select id= "findUserById" parameterType= "int" resultType= "joanna.yan.po.User" >select * from user where id=#{value}</select> </mapper>
|
登录后复制
在SqlMapConfig.xml中加载User.xml
1 2 3 4 5 6 7 8 9 | <?xml version= "1.0" encoding= "UTF-8" ?><!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration> <!--
批量别名的定义:
package:指定包名,mybatis会自动扫描包中的pojo类,自定义别名,别名就是类名(首字母大写或小写都可以) --> <typeAliases> <package name= "joanna.yan.po" /> </typeAliases><mappers><mapper resource= "sqlmap/User.xml" /><!-- 批量加载映射配置文件,mybatis自动扫描包下的mapper接口进行加载;
遵循一定的规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中;
以上规范的前提是:使用的是mapper代理方法; --><package name= "joanna.yan.mapper" /></mappers>
</configuration>
|
登录后复制
4.1.2 dao(实现类继承SqlSessionDaoSupport)
1 2 | public interface UserDao {
}
|
登录后复制
dao接口实现类需要注入SqlSessionFactory,通过spring进行注入。
这里用spring声明配置方式,配置dao的bean:让UserDaoImpl实现类继承SqlSessionDaoSupport。
1 2 3 4 5 6 | public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
@Overridepublic User findUserById(int id) throws Exception {
User user=sqlSession.selectOne( "test.findUserById" , id); return user;
}
}
|
登录后复制
4.1.3配置dao
在applicationContext.xml中配置dao。
1 | <!-- 方法一:原始dao接口 --><bean id= "userDao" class = "joanna.yan.dao.UserDaoImpl" ><property name= "sqlSessionFactory" ref= "sqlSessionFactory" /></bean>
|
登录后复制
4.1.4测试程序
1 2 3 4 5 6 7 8 9 10 11 12 | public class UserDaoImplTest { private ApplicationContext applicationContext;
@Beforepublic void setUp(){
applicationContext= new ClassPathXmlApplicationContext( "classpath:spring/applicationContext.xml" );
}
@Testpublic void findUsetByIdTest() throws Exception{
UserDao userDao=(UserDao) applicationContext.getBean( "userDao" );
User user=userDao.findUserById(1);
System.out.println(user);
}
}
|
登录后复制
4.2mapper代理开发
4.2.1mapper.xml和mapper.java

1 2 3 4 | <?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" ><!-- namespace 命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时, namespace 有特殊作用, namespace 等于mapper接口地址 --><mapper namespace = "joanna.yan.mapper.UserMapper" ><select id= "findUserById" parameterType= "int" resultType= "user" >select * from user where id=#{value}</select></mapper>
|
登录后复制
1 2 | public interface UserMapper { public User findUserById(int id) throws Exception;
}
|
登录后复制
4.2.2通过MapperFactoryBean创建代理对象
在applicationContext.xml中配置mapper。
1 2 | <!-- 方法二:mapper配置
MapperFactoryBean:根据mapper接口生成代理对象 --><bean id= "userMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" ><property name= "mapperInterface" value= "joanna.yan.mapper.UserMapper" /><property name= "sqlSessionFactory" ref= "sqlSessionFactory" /></bean>
|
登录后复制
4.2.3测试程序
1 2 3 4 5 6 7 8 9 10 11 12 | public class UserMapperTest { private ApplicationContext applicationContext;
@Beforepublic void setUp(){
applicationContext= new ClassPathXmlApplicationContext( "classpath:spring/applicationContext.xml" );
}
@Testpublic void findUsetByIdTest() throws Exception{
UserMapper userMapper=(UserMapper) applicationContext.getBean( "userMapper" );
User user=userMapper.findUserById(1);
System.out.println(user);
}
}
|
登录后复制
此方法的问题:需要针对每个mapper进行配置,麻烦。
4.3通过MapperScannerConfigurer进行mapper扫描(建议使用)
在applicationContext.xml中配置mapper批量扫描。
1 2 3 4 5 | <!-- 方法三:mapper批量扫描
从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录中。
自动扫描出来的mapper的bean的id为mapper类名(首字母小写) --><bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" ><!-- 指定扫描的包名
如果扫描多个包,每个包中间使用半角逗号分隔 --><property name= "basePackage" value= "joanna.yan.mapper" /><property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" /></bean>
|
登录后复制
此时,SqlMapConfig.xml中批量加载joanna.yan.mapper下mapper的配置就可以省略掉了。

测试程序:
1 2 3 4 5 | @Testpublic void findUsetByIdTest() throws Exception{
UserMapper userMapper=(UserMapper) applicationContext.getBean( "userMapper" );
User user=userMapper.findUserById(1);
System.out.println(user);
}
|
登录后复制
以上是Spring+SpringMVC+MyBatis深入学习及搭建——MyBatis和Spring整合的详细内容。更多信息请关注PHP中文网其他相关文章!