怎样通过Spring整合Mybatis来实现业务
目录
1.导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.13</version>
</dependency>
<!--用Spring操作数据库,还需要一个spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.13</version>
</dependency>
<!--AOP织入-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
2. spring配置文件
spring整合mybatis之后,就不再需要mybatis的配置文件
<?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">
<!--Spring整合Mybatis,省略了Mybatis的核心配置文件,转而在Spring的配置文件中配置Mybatis-->
<!--datasource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/sun/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate:就是我们使用的sqlSession,这个是spring提供的-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--我们只能使用构造器注入,因为没有set方法-->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
上面的配置文件是固定的,配置文件需要三个bean
- dataSource 用来连接数据库
- sqlSessionFactory 用来创建sqlSession
- sqlSession 用来创建sqlSession的bean
3.编写实体类(原mybatis的步骤)
public class User {
private int id;
private String name;
private int age;
}
4.编写接口(原mybatis的步骤)
public interface UserMapper {
public List<User> selectUser();
}
5.编写xml映射文件(原mybatis的步骤)
<?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.sun.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from user.tb_user
</select>
</mapper>
6.编写接口实例
public class UserMapperImpl implements UserMapper{
//之前使用Mybatis时候,我们用的是sqlSession,现在用Spring整合Mybatis,我们使用Spring提供的SqlSessionTemplate(相当于sqlSession)
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> selectUser() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
7.将实例注册到spring中
<bean id="UserMapperImpl" class="com.sun.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
8.测试
直接获取实例的bean即可
public class MyTest {
@Test
public void selectTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserMapper userMapperImpl = (UserMapper) context.getBean("UserMapperImpl");
List<User> users = userMapperImpl.selectUser();
for (User user : users) {
System.out.println(user);
}
}
}
总的来说,使用Spring整合Mybatis,思想还是不变,只不过形式是采用了Spring的ioc容器
9.第二种方式
- Spring提供了一个SqlSessionDaoSupport类,可以通过getSqlSession方法直接获取SqlSession,原本我们是通过SqlSessionTemplate作为中间人,现在通过这个方法我们不再需要SqlSessionTemplate
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
//这个类继承了SqlSessionDaoSupport,可以直接get一个SqlSession
@Override
public List<User> selectUser() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
将这个类放到Spring中:
<bean id="UserMapperImpl2" class="com.sun.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
测试:
@Test
public void selectTest2(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserMapper userMapperImpl2 = (UserMapper) context.getBean("UserMapperImpl2");
List<User> users = userMapperImpl2.selectUser();
for (User user : users) {
System.out.println(user);
}
}