0%

Springboot集成MyBatis


一、介绍

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。


二、创建项目

使用idea创建空项目并记得选择web,mysql,mybaties这几个依赖即可,在此就不赘述了,创建后包依赖大致如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependencies>
<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>1.3.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

三、在yml配置中添加相关配置

1
2
3
4
5
6
7
8
9
10
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver

# 之后的填写mybatis的mapper书写路径
mybatis:
mapper-locations: classpath:mapper/*.xml

四、创建一个实体类

SysUserEntity.java

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cn.gausscode.calo.user.entity;
import java.util.Date;

public class SysUserEntity {
private int id;
private String name;
private String loginName;
private String email;
private int tel;
private String password;
private String picUrl;
private String siteId;
private Date createDate;
private int createBy;
private Date updateDate;
private int updateId;
private int delFlag;
private String remarks;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLoginName() {
return loginName;
}

public void setLoginName(String loginName) {
this.loginName = loginName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getTel() {
return tel;
}

public void setTel(int tel) {
this.tel = tel;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPicUrl() {
return picUrl;
}

public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}

public String getSiteId() {
return siteId;
}

public void setSiteId(String siteId) {
this.siteId = siteId;
}

public Date getCreateDate() {
return createDate;
}

public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

public int getCreateBy() {
return createBy;
}

public void setCreateBy(int createBy) {
this.createBy = createBy;
}

public Date getUpdateDate() {
return updateDate;
}

public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}

public int getUpdateId() {
return updateId;
}

public void setUpdateId(int updateId) {
this.updateId = updateId;
}

public int getDelFlag() {
return delFlag;
}

public void setDelFlag(int delFlag) {
this.delFlag = delFlag;
}

public String getRemarks() {
return remarks;
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}

@Override
public String toString() {
return "SysUserEntity{" +
"id=" + id +
", name='" + name + '\'' +
", loginName='" + loginName + '\'' +
", email='" + email + '\'' +
", tel=" + tel +
", password='" + password + '\'' +
", picUrl='" + picUrl + '\'' +
", siteId='" + siteId + '\'' +
", createDate=" + createDate +
", createBy=" + createBy +
", updateDate=" + updateDate +
", updateId=" + updateId +
", delFlag=" + delFlag +
", remarks='" + remarks + '\'' +
'}';
}
}

五、创建一个dao层,service层以及controller层

SysUserDao.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package cn.gausscode.calo.user.dao;

import cn.gausscode.calo.user.entity.SysUserEntity;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface SysUserDao {
SysUserEntity get(int id);

void insert(SysUserEntity sysUserEntity);

void delete(int id);

void update(SysUserEntity sysUserEntity);
}

SysUserController.java

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
package cn.gausscode.calo.user.controller;

import cn.gausscode.calo.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SysUserController {

@Autowired
SysUserService sysUserService;

@RequestMapping("/get")
public String get(){
int id = 2;
return sysUserService.get(id);
}

@RequestMapping("/insert")
public String insert(){
return sysUserService.insert().toString();
}

@RequestMapping("/delete")
public String delete(){
sysUserService.delete();
return "successful delete";
}

@RequestMapping("/update")
public String update(){
return sysUserService.update();
}

}

SysUserService.java

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
package cn.gausscode.calo.user.service;

import cn.gausscode.calo.user.dao.SysUserDao;
import cn.gausscode.calo.user.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SysUserService {

@Autowired
SysUserDao sysUserDao;

public String get(int id){
return sysUserDao.get(id).toString();
}

public SysUserEntity insert(){
SysUserEntity sysUserEntity = new SysUserEntity();
sysUserEntity.setName("name");
sysUserEntity.setLoginName("loginName");
sysUserEntity.setEmail("email");
sysUserEntity.setTel(123);
sysUserEntity.setPassword("password");
sysUserEntity.setPicUrl("pic_url");
sysUserEntity.setSiteId("site_id");
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sysUserEntity.setCreateDate(new Date());
sysUserEntity.setCreateBy(1);
sysUserEntity.setDelFlag(1);
sysUserEntity.setRemarks("remarks");
sysUserDao.insert(sysUserEntity);
return sysUserEntity;

}

public void delete(){
sysUserDao.delete(1);
}

public String update(){
SysUserEntity sysUserEntity = new SysUserEntity();
sysUserEntity.setId(1);
sysUserEntity.setName("name1");
sysUserEntity.setLoginName("loginName1");
sysUserEntity.setEmail("email1");
sysUserEntity.setTel(1231);
sysUserEntity.setPassword("password1");
sysUserEntity.setPicUrl("pic_url1");
sysUserEntity.setSiteId("site_id1");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss1");
sysUserEntity.setCreateDate(new Date());
sysUserEntity.setCreateBy(11);
sysUserEntity.setDelFlag(11);
sysUserEntity.setRemarks("remarks1");
sysUserDao.update(sysUserEntity);
return sysUserDao.get(1).toString();
}
}

六、书写mybatis的mapper

SysUserMapper.xml

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
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.gausscode.calo.user.dao.SysUserDao">

<select id="get" parameterType="int" resultType="cn.gausscode.calo.user.entity.SysUserEntity">
select * from sys_user where id = #{id}
</select>

<insert id="insert" parameterType="cn.gausscode.calo.user.entity.SysUserEntity">
insert into sys_user (name,login_name,email,tel,password,pic_url,site_id,create_date,create_by,update_date,update_by,del_flag,remarks)
values (#{name},#{loginName},#{email},#{tel},#{password},#{picUrl},#{siteId},#{createDate},#{createBy},#{updateDate},#{updateId},#{delFlag},#{remarks})
</insert>

<update id="delete" parameterType="int">
update sys_user set del_flag = 0 where id = #{id}
</update>

<update id="update" parameterType="cn.gausscode.calo.user.entity.SysUserEntity">
update sys_user set
name = #{name},
login_name = #{loginName},
email = #{email},
tel = #{tel},
password = #{password},
pic_url = #{picUrl},
site_id = #{siteId},
create_date = #{createDate},
create_by = #{createBy},
update_date = #{updateDate},
update_by = #{updateId},
del_flag = #{delFlag},
remarks = #{remarks}
where id = #{id}
</update>
</mapper>
至此增删改查功能就完成了

七、总结

下面这个是整个项目的结构图

这里写图片描述

整体来说,mybatis可以大量减少了在sql代码部分的心思,特别是动态sql部分,简直太棒了,不过还没有完全学完,只是今天写了一个demo,之后会慢慢地把整个mybatis系统的学习总结一下