Spring整合Mybatis
步骤:
导入相关jar包
- junit
- mybatis
- mysql数据库
- spring相关
- aop织入
- mybatis-spring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>编写配置文件
测试
回忆MyBatis
编写实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.lrr.pojo;
import lombok.Data;
/**
* TODO
*
* @author DD
* @version 1.0
* @date 2021/7/13 17:20
*/
public class User {
private int id;
private String name;
private String perms;
}编写核心配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35<?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.lrr.pojo"/>
</typeAliases>
<environments default="development">
<!-- 数据库环境 -->
<environment id="development">
<!-- 使用JDBC使用管理 -->
<transactionManager type="JDBC"/>
<!-- 数据源配置 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="lrr1996429"/>
</dataSource>
</environment>
</environments>
<!-- 映射器 -->
<mappers>
<!-- <mapper resource="UserMapper.xml"/>-->
<mapper class="com.lrr.mapper.UserMapper" />
</mappers>
</configuration>编写接口
1
2
3
4
5
6
7
8
9package com.lrr.mapper;
import com.lrr.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> selectUser();
}编写Mapper.xml
1
2
3
4
5
6
7
8
9
10
11<?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.lrr.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user;
</select>
</mapper>测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38import com.lrr.mapper.UserMapper;
import com.lrr.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 javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* TODO
*
* @author DD
* @version 1.0
* @date 2021/7/13 17:34
*/
public class MyTest {
public void test1() throws IOException {
String resources = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resources);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
System.out.println(users);
sqlSession.close();
}
}
Mybatis-spring
编写数据源配置
1
2
3
4
5
6
7<!--DataSoure:使用Spring的数据源替换Mybatis的配置-->
<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/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="lrr1996429"/>
</bean>对应mybatis-config.xml中的数据源配置
sqlSessionFactory
1
2
3
4
5
6
7<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 绑定Mybatis配置文件,这两个必须要,具体内容见spring-10 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/lrr/mapper/*.xml"/>
</bean>对应
1
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSessionTemplate
1
2
3
4
5<!-- 这个就是之前使用的sqlSession-->
<bean id="sqlSessiomTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 因为它没有set方法只能用构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>对应
1
SqlSession sqlSession = sessionFactory.openSession(true);
需要给接口加实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package com.lrr.mapper;
import com.lrr.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
/**
* TODO
*
* @author DD
* @version 1.0
* @date 2021/7/13 20:06
*/
public class UserMapperImpl implements UserMapper{
// 我们的所有操作,原来都使用sqlSession来执行
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中
1
2
3<bean id="userMapper" class="com.lrr.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSessiomTemplate"/>
</bean>测试使用
1
2
3
4
5
6
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
System.out.println(userMapper.selectUser());
}
Spring整合Mybatis