Spring和Mybatis整合
使用配置方式开发
整合思路
- 需要spring通过单例方式管理sqlSessionFactory
- spring和mybatis整合生成mapper接口代理对象使用sqlSessionFactory创建SqlSession
- 持久层的mapper需要spring进行管理
准备jar包
- 核心包 :mybatis-spring.jar
- spring jar包
- mybatis jar包
- c3p0数据库连接池
- 数据库驱动
编写核心配置文件
- spring的配置文件:applicationContext.xml
- mybatis的配置文件:SqlMapConfig.xml
- 日志文件:log4j.properties
- db.properties
代码实例
Mapper接口
public interface UserMapper {
// 根据id查询用户信息
User selectUserById(Integer id);
}
Mapper.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.hpe.mapper.UserMapper">
<!-- 根据id查询用户信息 -->
<select id="selectUserById" parameterType="int" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
编写UserService接口和其实现类
public interface UserService {
// 根据id查询用户信息
User selectUserById(Integer id);
}
public class UserServiceImpl implements UserService{
// 注入UserMapper的动态代理对象
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public User selectUserById(Integer id) {
return userMapper.selectUserById(id);
}
}
配置applicationContext.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/context"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载外部资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 通过Spring管理SqlSessionFactory mapper接口 -->
<!-- 配置SqlSessionFactory为了使用 Spring来管理SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件的路径 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定批量创建别名的包 -->
<property name="typeAliasesPackage" value="com.hpe.po"></property>
</bean>
<!-- 配置UserService -->
<bean id="userService" class="com.hpe.service.UserServiceImpl">
<!-- 注入UserMapper属性 -->
<property name="userMapper" ref="userMapper"></property>
</bean>
<!-- 配置UserMapper接口实现类的Bean(Mapper代理类) -->
<!-- 通过MapperFactoryBean生成Mapper代理对象
mapperInterface:指定接口
sqlSessionFactory:指定会话工厂
-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 指定创建mapper接口对应的实现类 -->
<property name="mapperInterface" value="com.hpe.mapper.UserMapper"></property>
<!-- 注入SqlSessionFactory:为了创建sqlSession -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
注此时Mybatis的核心配置文件只有声明,没有编写任何内容。
使用注解方式开发
代码实例
此时Mapper接口、XML文件、Service接口及测试Test方法均未发生变化(相比较于配置方式),此处不再贴重复代码
ServiceImpl.java
@Service(value="userService")
public class UserServiceImpl implements UserService{
// 注入UserMapper的动态代理对象
//@Resource(name="userMapper")
// 自动装配:因为加载容器的时候就创建了UserMapper实现类的Bean,这里可以自动装配
@Autowired
private UserMapper userMapper;
@Override
public User selectUserById(Integer id) {
return userMapper.selectUserById(id);
}
}
applicationContext.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/context"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.hpe"></context:component-scan>
<!-- 加载外部资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 通过Spring管理SqlSessionFactory mapper接口 -->
<!-- 配置SqlSessionFactory为了使用 Spring来管理SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件的路径 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定批量创建别名的包 -->
<property name="typeAliasesPackage" value="com.hpe.po"></property>
</bean>
<!-- 批量创建Mapper接口实现类的Bean,可以不指定id
默认Bean是由id的,是Mapper接口的名称且首字母小写-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定需要创建实现类的Mapper接口所在的包
可以指定多个包,使用逗号隔开即可-->
<property name="basePackage" value="com.hpe.mapper"></property>
<!-- 需要指定SqlSessionFactory,使用sqlSessionFactoryBeanName
通过这种方式就可以等到Spring初始化完成后,再去构造SqlSessionFactory,否则无法连接数据库 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>