步骤
1.导入相关jar包
junit\mybatis\mysql数据库\spring相关\aop织入\mybatis-spring(new)
2.编写配置文件
3.测试
8.1回忆mybatis
1.编写实体类
2.编写核心配置文件
3.编写接口
4.编写Mapper.xml
5.测试
8.2Spring整合Mybatis
8.2.1方式一
1.编写数据源配置(spring-dao.xml)
2.sqlSessionFactory
3.sqlSessionTemplate
spring-dao.xml配置文件只专注于数据源的配置,这个文件基本是固定的,然后因为Mybatis被Spring内部整合了,所以其核心配置文件mybatis-config.xml现在只做别名和Mybatis设置;为了更加体现分工明确,我们在Spring的核心配置文件applicationContext.xml中引入spring-dao.xml,并且在其中完成相关Mapper的注册
- spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid 我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasorce-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/dong/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate:就是我们 以前使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入 sqlSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
- mybatis-config.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>
<typeAliases>
<package name="com.dong.pojo"/>
</typeAliases>
</configuration>
4.需要给接口增加实现类
- UserMapperImpl.class
public class UserMapperImpl implements UserMapper{
//在原来我们的所有操作都使用sqlSession来执行,现在都是使用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getUsers() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUsers();
}
}
5.将上面的实现类注册到Spring中
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--引入外部配置文件-->
<import resource="spring-dao.xml"/>
<--注册mapper-->
<bean id="userMapper" class="com.dong.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
6.测试
- MyTest.calss
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user:userMapper.getUsers()) {
System.out.println(user);
}
}
}
8.2.2方式二(简化)
对比两种方式中的接口的实现类
- UserMapperImpl.calss
public class UserMapperImpl implements UserMapper{
//在原来我们的所有操作都使用sqlSession来执行,现在都是使用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getUsers() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUsers();
}
}
- UserMapperImpl2.class
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
SqlSession sqlSession = getSqlSession();
@Override
public List<User> getUsers() {
System.out.println("UserMapperImpl2");
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUsers();
}
}