mybatis和spring整合的思路
- 让spring管理SqlSessionFactory
- 让spring管理mapper对象和dao
1. 整合的jar包
- spring的jar包
- Mybatis的jar包
- Spring+mybatis的整合包。
- Mysql的数据库驱动jar包。
- 数据库连接池的jar包。
2. 加入配置文件
2.1 SqlMapConfig.xml文件
<?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>
<properties resource="db.properties"/>
<typeAliases>
<!-- 别名 包以其子包下所有类 头字母大小都行-->
<package name="cn.raine.pojo"/>
</typeAliases>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}" />
<property name="url"
value="jdbc:mysql://localhost:3306/springdemo?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456789" />
</dataSource>
</environment>
</environments>
<!--加映射文件-->
<mappers>
<!--<mapper resource="cn/raine/mapper/UserMapper.xml"/>-->
<package name="cn.raine.mapper"/>
</mappers>
</configuration>
2.2 applicationContext.xml
SqlSessionFactoryBean属于mybatis-spring这个jar包对于spring来说,mybatis是另外一个架构,需要整合jar包
<?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>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置mybatis核心配置文件-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
3. Dao的开发
3.1 传统的Dao的开发方式
3.1.1 实现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">
<!-- namespace:命名空间,用于隔离sql -->
<!-- 还有一个很重要的作用,使用动态代理开发DAO,1. namespace必须和Mapper接口类路径一致 -->
<mapper namespace="cn.raine.mapper.UserMapper">
<!-- 根据用户id查询用户 -->
<!-- 2. id必须和Mapper接口方法名一致 -->
<!-- 3. parameterType必须和接口方法参数类型一致 -->
<!-- 4. resultType必须和接口方法返回值类型一致 -->
<select id="queryUserById" parameterType="int"
resultType="cn.raine.pojo.User">
select * from s_user where id = #{id}
</select>
<!-- 根据用户名查询用户 -->
<select id="queryUserByUsername" parameterType="string"
resultType="cn.raine.pojo.User">
select * from s_user where username like '%${value}%'
</select>
<insert id="saveUser" parameterType="cn.raine.pojo.User">
<selectKey keyProperty="id" keyColumn="id" order="AFTER"
resultType="int">
select last_insert_id()
</selectKey>
insert into s_user(username,birthday,sex,address) values
(#{username},#{birthday},#{sex},#{address});
</insert>
<!--使用包装类型查询用户-->
<select id="queryUserByQueryVo" parameterType="QueryVo"
resultType="User">
SELECT * FROM `s_user` WHERE username LIKE '%${user.username}%'
</select>
<!--根据条件查询用户-->
<select id="queryUserByWhere" parameterType="user" resultType="user">
SELECT <include refid="userFields"/> FROM `s_user`
<!-- where标签可以自动添加where,同时处理sql语句中第一个and关键字 -->
<where>
<if test = "sex != null">
AND sex = #{sex}
</if>
<if test="username != null and username != ''">
AND username LIKE '%${username}%'
</if>
</where>
</select>
</mapper>
3.1.2 之后在SqlMapConfig中加载Mapper.xml配置
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
3.1.3 实现UserDao接口
public interface UserDao {
/**
* 根据id查询用户
*
* @param id
* @return
*/
User queryUserById(int id);
/**
* 根据用户名模糊查询用户列表
*
* @param username
* @return
*/
List<User> queryUserByUsername(String username);
/**
* 保存
*
* @param user
*/
void saveUser(User user);
}
3.1.4 实现UserDaoImpl实现类
实现类必须集成SqlSessionDaoSupport
SqlSessionDaoSupport提供getSqlSession()方法来获取SqlSession
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User queryUserById(int id) {
// 获取SqlSession
SqlSession sqlSession = super.getSqlSession();
// 使用SqlSession执行操作
User user = sqlSession.selectOne("queryUserById", id);
// 不要关闭sqlSession
return user;
}
@Override
public List<User> queryUserByUsername(String username) {
// 获取SqlSession
SqlSession sqlSession = super.getSqlSession();
// 使用SqlSession执行操作
List<User> list = sqlSession.selectList("queryUserByUsername", username);
// 不要关闭sqlSession
return list;
}
@Override
public void saveUser(User user) {
// 获取SqlSession
SqlSession sqlSession = super.getSqlSession();
// 使用SqlSession执行操作
sqlSession.insert("saveUser", user);
// 不用提交,事务由spring进行管理
// 不要关闭sqlSession
}
}
3.1.5 配置Dao
在AplicationContext中
<!--原始Dao-->
<bean id="userDao" class="cn.raine.mybatis.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
3.2 Mapper代理形式开发dao
3.2.1配置mapper代理
在applicationContext.xml添加配置
<!--Mapper动态代理-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.raine.mybatis.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
3.2.2 扫描包形式配置mapper
在applicationContext.xml添加配置
<!--扫描方式配置代理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.raine.mybatis.mapper"/>
</bean>
每个mapper代理对象的id就是类名,首字母小写