SpringBoot配置mybatis使用的两种方式

目前在SpringBoot中的使用Mybatis的pom文件是

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
<
dependency
>
<
groupId
>
org.mybatis.spring.boot
</
groupId
>
<
artifactId
>
mybatis-spring-boot-starter
</
artifactId
>
<
version
>
.0.0
</
version
>
</
dependency
>

1. 注解版本
添加相关的依赖

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<
dependency
>
<
groupId
>
org.springframework.boot
</
groupId
>
<
artifactId
>
spring-boot-starter-web
</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>
org.mybatis.spring.boot
</
groupId
>
<
artifactId
>
mybatis-spring-boot-starter
</
artifactId
>
<
version
>
.0.0
</
version
>
</
dependency
>
<!-- mysql-->
<
dependency
>
<
groupId
>
mysql
</
groupId
>
<
artifactId
>
mysql-connector-java
</
artifactId
>
</
dependency
>
<!-- lombok 自动创建bean的 -->
<
dependency
>
<
groupId
>
org.projectlombok
</
groupId
>
<
artifactId
>
lombok
</
artifactId
>
<
optional
>
true
</
optional
>
</
dependency
>

添加properties配置文件

1
2
3
4
5
6
7
8
9
10
11
spring.datasource.url
=jdbc:mysql://
.0
.
.1
:
/test?serverTimezone=UTC
spring.datasource.username
=root
spring.datasource.password
=root

配置完后,SpringBoot会加载spring.datasource的所有配置。数据源就会自动注入到 sqlSessionFactory 中,sqlSessionFactory 会自动注入到 Mapper 中。
在主类上开启mapper包扫描

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@MapperScan
(
"com.yuanzhou.mybatis.mapper"
)
@SpringBootApplication
public class MybatisApplication {
public
static
void
main
(String[] args) {
SpringApplication
.run
(MybatisApplication.class, args);
}
}

创建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
38
39
public
interface
UserMapper
{
@Select
(
"select * from user"
)
@Results
({
@Result
(property=
"id"
, column=
"id"
),
@Result
(property=
"name"
, column=
"name"
),
@Result
(property=
"age"
, column=
"age"
),
@Result
(property=
"email"
, column=
"email"
)
})
List<User>
userList
();
}

之后测试是正确能读取到的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@RunWith
(SpringRunner.
class
)
@SpringBootTest
public
class
MybatisTestCase {
@Autowired
UserMapper userMapper;
@Test
public
void
selectTest() {
List<User> list = userMapper.list();
list.forEach(System.
out
::println);
}
}


2.XML版本 (前公司使用的)
在properties文件中添加如下配置:

1
2
3
4
5
6
#实体类映射地址(我这里和mapper文件放在一起了,impl文件夹等同于mapper接口的实现了)
mybatis.mapper-locations=classpath:com
/yuanzhou/my
batis
/mapper/im
pl/*.xml

在上述位置中添加user的映射文件

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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.yuanzhou.mybatis.mapper.UserMapper"
>
<
resultMap
id
=
"BaseResultMap"
type
=
"com.yuanzhou.mybatis.entity.User"
>
<
id
property
=
"id"
column
=
"id"
jdbcType
=
"VARCHAR"
>
</
id
>
<
result
property
=
"name"
column
=
"name"
jdbcType
=
"VARCHAR"
>
</
result
>
<
result
property
=
"age"
column
=
"age"
jdbcType
=
"VARCHAR"
>
</
result
>
<
result
property
=
"email"
column
=
"email"
jdbcType
=
"VARCHAR"
>
</
result
>
</
resultMap
>
<
sql
id
=
"BaseColumnList"
>
id, name, age, email
</
sql
>
<
select
id
=
"getList"
resultMap
=
"BaseResultMap"
>
select
<
include
refid
=
"BaseColumnList"
/>
from user
</
select
>
</
mapper
>

mapper层直接写简单的接口方法就可以了,相较之前的版本,直接把注解删除即可
个人角色第二种方法更简洁一下,接口里看起来更加干净


SpringBoot配置mybatis使用的两种方式
https://zhouyinglin.top/2022/08/06/SpringBoot配置mybatis使用的两种方式/
作者
小周
发布于
2022年8月6日
许可协议