阅读本篇文章前建议先看看七、Mybatis整合spring之手动实现dao层
环境准备
我这里直接复制上篇文章的工程,然后将dao层删掉,把user.xml映射文件删掉,测试类删掉,删掉SqlMapConfig.xml配置中的user.xml的关联,删掉ApplicationContext.xml配置中的userDao的注入。
完成后的工程如下所示:
添加Mapper相关的操作
1.创建UserMapper接口类
在src目录下的根包中创建一个mapper包,定义UserMapper接口类
package blog.csdn.net.mchenys.mapper;
import blog.csdn.net.mchenys.pojo.User;
public interface UserMapper {
public User findUserById(Integer id);
}
2.创建对应的mapper映射文件
在mapper包中创建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="blog.csdn.net.mchenys.mapper.UserMapper">
<select id="findUserById" parameterType="java.lang.Integer"
resultType="blog.csdn.net.mchenys.pojo.User">
select * from user where id = #{id}
</select>
</mapper>
完成后的包结构是这样的:
3.修改Mybatis配置文件
编辑SqlMapConfig.xml,在mappers节点开启包扫描的方式引入Mapper接口类
<?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>
<!-- 配置映射文件 -->
<mappers>
<!-- 使用包扫描的方式批量引入Mapper接口-->
<package name="blog.csdn.net.mchenys.mapper"/>
</mappers>
</configuration>
修改Spring配置文件
这里有2种方式接入Mapper接口的代理实现类,先介绍手动注入的方式,编辑ApplicationContext.xml,在beans标签节点内添加如下内容:
<!-- Mapper接口代理实现 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 配置mapper接口的全路径名称 -->
<property name="mapperInterface" value="cn.itheima.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
关于org.mybatis.spring.mapper.MapperFactoryBean可以在mybatis-spring-1.2.2.jar中找到
完整的Spring配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.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>
<!-- 整合Sql会话工厂归spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 指定会话工厂使用的数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Mapper接口代理实现 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 配置mapper接口的全路径名称 -->
<property name="mapperInterface" value="blog.csdn.net.mchenys.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
测试
在测试包内创建UserMapperTest测试类
package blog.csdn.net.mchenys.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import blog.csdn.net.mchenys.mapper.UserMapper;
import blog.csdn.net.mchenys.pojo.User;
public class UserMapperTest {
private ApplicationContext applicatonContext;
@Before
public void setUp() throws Exception {
applicatonContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
}
@Test
public void testFindUserById() throws Exception{
UserMapper userMapper = (UserMapper)applicatonContext.getBean("userMapper");
User user = userMapper.findUserById(1);
System.out.println(user);
}
}
执行后,绿条
控制台结果如下,说明Mapper接口代理的方式也整合Spring也ok了。
优化
上面修改Spring配置文件的时候我说过还有另一种方式来注入Mapper接口的代理实现类,这种方式是比较推荐的,步骤如下:
1.首先删掉SqlMapConfig.xml配置文件,没错,这次可以不再需要Mybatis的配置文件了。
2.修改ApplicationContext.xml配置文件,将Sql会话工厂内的指定mybatis核心配置文件的那个property去掉
3.将手动注入Mapper接口代理实现的配置去掉
4.使用包扫描的方式批量引入Mapper(重点),配置如下:
<!-- 使用包扫描的方式批量引入Mapper,使用的时候就用Mapper接口的类名,首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->
<property name="basePackage" value="blog.csdn.net.mchenys.mapper"/>
</bean>
使用包扫描方式的好处就是可以避免频繁的修改Spring的配置文件,以后有新的Mapper接口和mapper映射文件就直接在Mapper包中添加即可,完整的Spring配置文件修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.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>
<!-- 整合Sql会话工厂归spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定会话工厂使用的数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用包扫描的方式批量引入Mapper,使用的时候就用Mapper接口的类名,首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->
<property name="basePackage" value="blog.csdn.net.mchenys.mapper"/>
</bean>
</beans>
删掉Mybatis配置文件后的工程结构如下:
看起来就挺爽的,少了一个配置文件,执行测试类效果是一样的,这里就不啰嗦了。