0️⃣初始配置
🚩pom.xml导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SMDemo1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--mybatis与数据库相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--spring与mybatis关联-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--其他-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
🚩dao类
package com.app.dao;
import com.app.pojo.User;
import java.util.List;
public interface UserDao {
List<User> selectUser();
}
🚩pojo类
package com.app.pojo;
public class User {
private int id;
private String name;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
🚩dao.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">
<mapper namespace="com.app.dao.UserDao">
<select id="selectUser" resultType="com.app.pojo.User">
select *
from user
</select>
</mapper>
1️⃣整合前MyBatis代码编写
在以前仅使用MyBatis的时候,我们编写完配置文件、dao类、dao.xml、pojo类后,会编写下面的代码5步生成UserDao 代理类进行测试:
// 获取流对象,读取核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
// 创建 sqlSessionFactoryBuilder 工厂构建者对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 获取 sqlSessionFactory 工厂对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
// 获取 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取 UserDao 代理类对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
// 使用 UserDao 的相关方法
userDao.XXX();
// 提交事务
sqlSession.commit();
// 释放资源
sqlSession.close();
上面代码可以看出,我们需要通过:
SqlSessionFactoryBuilder -> SqlSessionFactory -> SqlSession -> UserDao
来一步步获得UserDao的代理类
2️⃣使用SqlSessionTemplate
在Spring 整合MyBatis中,我们将上面的5步生成 UserDao 代理类的java代码写成xml配置在Sping的配置文件中。通过下面的步骤来获取UserDao的代理类:
SqlSessionFactoryBean -> SqlSessionTemplate -> UserDaoImpl -> UserDao
在整合MyBatis开发中,我们可以不编写MyBatis的配置文件,关于数据源等配置在Spring配置文件中编写:
🚩创建Spring配置文件spring-config.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
🚩生成数据源
<!--生成Druid数据源-->
<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/db_study"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
🚩注册SqlSessionFactory
<!--注册SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--关联Mapper-->
<property name="mapperLocations" value="classpath:com.app.dao/UserMapper.xml"/>
</bean>
🚩注册SqlSessionTemplate
<!--注册sqlSessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--关联sqlSessionFactory,利用构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
🚩编写UserDaoImpl实现类
使用SqlSessionTemplate对MyBatis进行整合时,我们仍需要编写UserDaoImpl实现类,然后在spring-config.xml中进行注册。在UserDaoImpl实现类中,我们添加 SqlSession 属性,这样将之前注册的SqlSessionTemplate进行注入。
在下面使用MapperScannerConfigurer开发时,我们将不再需要编写UserDaoImpl实现类。
package com.app.dao;
import com.app.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserDaoImpl implements UserDao{
private SqlSession sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> selectUser() {
UserDao userDao = sqlSession.getMapper(UserDao.class);
return userDao.selectUser();
}
}
🚩注册userDao实现
<!--注册userDao实现-->
<bean id="userDaoImpl" class="com.app.dao.UserDaoImpl">
<property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>
🚩测试
import com.app.dao.UserDao;
import com.app.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class UserTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserDao userDao = (UserDao) context.getBean("userDaoImpl");
List<User> users = userDao.selectUser();
for (User user : users) System.out.println(user);
}
}
🚩项目结构
1️⃣使用MapperScannerConfigurer
使用MapperScannerConfigurer对MyBatis进行整合,我们就不需要编写UserDaoImpl实现类。通过以下步骤生成UserDao:
SqlSessionFactoryBean -> MapperScannerConfigurer -> UserDao
Mybatis在与Spring集成的时候可以配置MapperFactoryBean来生成Mapper接口的代理。MapperFactoryBean的出现为了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate编写数据访问对象(DAO)的代码,使用动态代理实现。
🚩修改Spring配置文件spring-config.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描所有要被Spring管理的实例-->
<context:component-scan base-package="com.app"/>
<!--生成Druid数据库连接池对象-->
<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/db_study"/>
<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"/>
<property name="typeAliasesPackage" value="com.app.pojo"/>
</bean>
</beans>
🚩注册MapperScannerConfigurer:
<!--扫描所有的dao,将所有dao对应的dao.xml生成对应的实例-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--依赖于sqlSessionFactory-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="basePackage" value="com.app.dao"/>
</bean>
🚩测试:
package com.app.test;
import com.app.dao.UserDao;
import com.app.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class AppTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserDao userDao = context.getBean(UserDao.class);
List<User> users = userDao.selectUser();
for (User user : users) System.out.println(user);
}
}