Spring和Mybatis整合
整合思路:
Spring通过单例的方式管理SQLSessionFactory
Spring和mybatis整合生成代理对象,使用SQLSessionFactory创建SQLSession(spring和Mybatis整合自动完成)
持久层的Mapper都需要Spring进行管理。
整合过程
(1)创建Maven工程,并修改配置
(2)导包
除了Spring和Mybatis的包,还需要引入整合包。早期ibatis和Spring整合的包由Spring提供,后期改由Mybatis
这里需要导入Spring的包,mybatis的包以及Spring-Mybatis整合包。
可以在github官网上下载。https://github.com/mybatis/spring/releases
(3)配置文件,并修改
配置文件需要配置Mybatis的也要配置Spring的。
(4)DAO的开发
方式一:原始DAO开发方式
Mybatis需要干的事情:
DAO接口
package com.huadian.dao;
import com.huadian.entity.User;
public interface UserDao {
public User getUserById(Integer id);
public void insertUser(User u);
}
DAO接口实现
package com.huadian.dao.impl;
import com.huadian.dao.UserDao;
import com.huadian.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
public User getUserById(Integer id) {
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne( "user.findUserById", id );
return user;
}
public void insertUser(User u) {
}
}
继承SqlSessionDaoSupport,可以获取SQLSession,导致DAO依赖SQLSessionFactory
配置信息(user.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进行分类管理,SQL隔离-->
<mapper namespace="user">
<select id="findUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
核心配置文件:(sqlMapconfig)
加载配置信息
别名
<?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>
<typeAliases>//别名
<package name="com.huadian.entity"></package>
</typeAliases>
<mappers>//加载配置
<mapper resource="com/huadian/entity/User.xml"></mapper>
<!--<package name="com.huadian.mapper"></package>-->
</mappers>
</configuration>
Spring要干的事情(配置applicationContext.xml文件)
连接C3P0:数据源
<!--dataSource:C3P0连接池-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis01?characterEncoding=UTF-8"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
SqlSessionFactoryBean
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlMapconfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
实体对象
<bean name="user" class="com.huadian.entity.User"></bean>
Dao,依赖sqlSessionFactory
<bean name="userDao" class="com.huadian.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
创建user实体:
public class User {
private Integer id;
private String username;
private String sex;
private String birthday;
private String address;
}
测试类:
package com.huadian;
import com.huadian.dao.UserDao;
import com.huadian.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demodao {
@Autowired
private UserDao userDao;
@Test
public void fun() {
User user = userDao.getUserById(10);
System.out.println(user);
}
}
方式二:Mapper代理开发方式:
Mybatis需要干的事情:
接口和配置文件
package com.huadian.mapper;
import com.huadian.entity.User;
public interface UserMapper {
/**
* 根据用户ID查询用户
* @param id
* @return
*/
public User findUserById(Integer id);
}
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">
<!--namespace:命名空间,对SQL进行分类管理,SQL隔离-->
<mapper namespace="com.huadian.mapper.UserMapper">
<!--查询用户列表:主键查询-->
<select id="findUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
</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>
<typeAliases>
<package name="com.huadian.entity"></package>
</typeAliases>
</configuration>
只剩下一个别名了,
Spring需要干的事情:
Mapper对象需要管理
单个单个写
<!--mapper:Mapper代理 单个对象-->
<bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="mapperInterface" value="com.huadian.mapper.UserMapper"></property>
</bean>
批量扫描
<!--批量-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--指定扫描的包,如果扫描多个包,每个包之前用半角的逗号分隔-->
<property name="basePackage" value="com.huadian.mapper"></property>
</bean>