1. 整合 Mybatis
-
步骤
-
导入相关 jar 包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!-- spring 操作数据库,需要一个 Spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!-- 织入包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> </dependencies>
-
编写配置文件
-
测试
-
1.1 回顾 Mybatis
-
导入依赖
-
编写实体类
@Data public class User { private int id; private String name; private String pw; }
-
编写核心配置文件
<?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.why.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="981030"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.why.mapper.UserMapper"/> </mappers> </configuration>
-
编写接口
public interface UserMapper { public List<User> selectUser(); }
-
编写 Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.why.mapper.UserMapper"> <select id="selectUser" resultType="user"> select * from user ; </select> </mapper>
-
开启资源过滤
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
-
测试
public class MyTest { @Test public void test() throws IOException { String resources = "mybatis-config.xml"; InputStream in = Resources.getResourceAsStream(resources); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.selectUser(); for (User user : users) { System.out.println(user); } } }
1.2 什么是 MyBatis-Spring
MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession
并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException
。 最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。
1.3 知识基础
在开始使用 MyBatis-Spring 之前,你需要先熟悉 Spring 和 MyBatis 这两个框架和有关它们的术语。这很重要——因为本手册中不会提供二者的基本内容,安装和配置教程。
MyBatis-Spring 需要以下版本:
MyBatis-Spring | MyBatis | Spring Framework | Spring Batch | Java |
---|---|---|---|---|
2.0 | 3.5+ | 5.0+ | 4.0+ | Java 8+ |
1.3 | 3.4+ | 3.2.2+ | 2.1+ | Java 6+ |
12.4 整合方式一
-
添加数据源
-
配置数据源(spring-dao.xml)
<!-- dataSource: 使用 Spring 的数据源替换 Mybatis 的配置 此处使用 Spring 提供的 JDBC (spring-jdbc) 还可以使用 C3p0 / Dbcp / 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/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="981030"/> </bean>
-
创建 SqlSessionFactor (spring-dao.xml)
<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/why/mapper/*.xml"/> </bean>
-
获取 SqlSessionTemplate / SqlSession (spring-dao.xml)
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 因为没有 set 方法,只能使用构造器注入 --> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean>
-
创建接口的实现类【用于执行Mybatis的操作】
public class UserMapperImpl implements UserMapper { // 使用 sqlSessionTemplate 替换 sqlSession 操作Mybatis private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public List<User> selectUser() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.selectUser(); } }
-
将实现类注入到 Spring (applicationContext.xml)
<bean id="userMapper" class="com.why.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean>
-
测试
public class MyTest { @Test public void test() throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user); } } }
-
目录结构
12.5 整合方式二
-
让实现类继承 SqlSessionDaoSupport ,此时实现类的方法中可以直接使用 getSqlSession() 方法来获取 SqlSession ,本质还是第一种方法。
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper { public List<User> selectUser() { return getSqlSession().getMapper(UserMapper.class).selectUser(); } }
-
XML 文件中不需要配置和注入
SqlSession
了,但是需要配置SqlSessionFactory
,SqlSessionDaoSupport
需要用SqlSessionFactory
来获取 SqlSession。<bean id="userMapper" class="com.why.mapper.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>