使用Spring整合Mybatis 因为Spring是一个大且杂的容器 几乎没有什么框架是它不能整合的 所以我们可以把Mybatis框架整合到Spring中 以后学习了 SpringMVC了 就把SpringMVC也整合到Spring中 这样就是SSM框架了。
Spring整合Mybatis有以下几个步骤 我们以前单纯写Mybatis的时候 需要写一个Mybatis-config.xml配置文件 同样的 在Spring中也需要写这个配置文件 这是不能省的
<?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>
<typeAlias type="com.Jee.entity.User" alias="user"/>
</typeAliases>
</configuration>
可以看到 我们这个xml文件中 很多很多东西都已经不见了 例如我们以前需要配置的source都不见了 只存正在一个typeAlias标签来设置别名了 那么其他的东西都去哪了呢
我们还在resource包下建了一个名为 spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置数据源 这是Mybatis必须要配置的 也是SqlSessionFactory所需要的属性 -->
<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="123456"/>
</bean>
<!-- 配置SqlSessionFactory 这个是SqlSession的工厂类 用于创建SqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--这个标签是和Mybatis-config.xml文件连接的标签 因为我们把Mybatis整合到Spring中
所以我们需要让Sprig和 这个Mybatis-config.xml文件相关联-->
<property name="configLocation" value="classpath:Mybatis-config.xml"/>
<!--这个标签是配置mapper的匹配标签 可以在Mybatis-config.xml文件中写 也可以在这里写-->
<property name="mapperLocations" value="classpath:com/Jee/dao/UserMapper.xml"/>
</bean>
<!-- 配置SqlSessionTemplate(就是Mybatis中的SqlSession 只不过在Spring中叫SqlSessionTemplate)
功能和SqlSession一模一样 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--固定写法 通过注入构造方法来实例化这个类-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
原本在Mybatis中 我们对Mapper接口不需要写实现类 只需要通过Mapper.xml去获得一个Mapper对象 再通过这个Mapper对象去操作这个Mapper接口的方法 想下面这样
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.queryAll();
而在Spring中 我们需要多写一个实现类 去实现这个Mapper接口 因为我们需要通过Spring容器直接去拿到可以操作Mapper接口中声明的方法的类 所以我们需要这样一个实现类
package com.Jee.dao;
import com.Jee.entity.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> queryAll() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.queryAll();
}
}
注意 我们在Spring中每创建一个类 都要把它注册到Spring容器中 我们对这个类写了set方法 是为了让容器对他注入SqlSession
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.Jee.dao.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
这里的import标签是引入Spring-dao.xml文件 我们最终使用的是这个applicationContext.xml文件
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapperImpl userMapper = (UserMapperImpl) context.getBean("userMapper");
List<User> users = userMapper.queryAll();
for (User user : users) {
System.out.println(user);
}
}
总之 我们是按以下几个步骤去整合Mybatis框架到Spring中的:
- 编写数据源配置
- SqlSessionFactory(需要数据源)
- SqlSessionTemplate(需要SqlSessionFactory)
- 给接口写一个实现类
- 将这个实现类注入到Spring中