SpringBoot整合Mybatis

在介绍整合Mybatis 前,我们先介绍下如何使用传统的Dao 类方式来实现

首先,配置maven

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
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>

然后我们在application.properties 中进行配置

1
2
3
4
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_db?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

接下来我们创建一个User 类

1
2
3
4
5
6
7
public class User {
private Integer id;
private String username;
private String jobs;
private String phone;
//get
//set

然后创建一个UserDao 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;

public int addUser(User user){
return jdbcTemplate.update("INSERT INTO t_user(username,jobs,phone) VALUE (?,?,?)",
user.getName(),user.getJobs(),user.getPhone());
}
public int updateUser(User user){
return jdbcTemplate.update("UPDATE t_user SET username=?,jobs=?,phone=? WHERE id=?",
user.getName(),user.getJobs(),user.getPhone(),user.getId());
}
public int deleteUser(Integer id){
return jdbcTemplate.update("DELETE FROM t_user WHERE id=?",id);
}
public User getUserById(Integer id){
return jdbcTemplate.queryForObject("SELECT * FROM t_user WHERE id =?",new BeanPropertyRowMapper<>(User.class),id);
}
public List<User> getAllUser(){
return jdbcTemplate.query("SELECT * FROM t_user",new BeanPropertyRowMapper<>(User.class));
}
}

接下来创建一个UserService 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Service
public class UserService {

@Autowired
UserDao userDao;
public int addUser(User user){
return userDao.addUser(user);
}
public int updateUser(User user){
return userDao.updateUser(user);
}
public int deleteUser(Integer id){
return userDao.deleteUser(id);
}
public User getUserById(Integer id){
return userDao.getUserById(id);
}
public List<User> getAllUser(){
return userDao.getAllUser();
}
}

最后是UserController类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/user")
public void UserOps(){
User u1 = new User();
u1.setName("章三三");
u1.setJobs("程序员");
u1.setPhone("131444224433");
int i = userService.addUser(u1);
System.out.println(i);
List<User> allUser = userService.getAllUser();
System.out.println(allUser);
}
}

我们运行程序,然后访问127.0.0.1:8080/user 我们看到IDE 控制台输出了如下内容
20200421103149

访问数据库,我们查询后结果如下

20200421103223

这是传统Dao 方式的整合,接下来,我们讲下使用Mybatis 整合

首先,在pom.xml 中增加

1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>

然后创建一个Mapper 文件夹,在该文件夹下创建一个UserMapper 接口

1
2
3
4
5
6
7
8
9
@Mapper
public interface UserMapper {
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
User getUserById(Integer id);
List<User> getAllUsers();

}

接下来,在resource下创建一个Mapper 文件夹,然后在文件夹中创建一个UserMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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.example.demo.Mapper.UserMapper">
<insert id="addUser" parameterType="com.example.demo.Bean.User">
INSERT INTO t_user(username,jobs,phone) VALUE (#{username},#{jobs},#{phone})
</insert>
<delete id="deleteUser" parameterType="int">
DELETE FROM t_user WHERE id=#{id}
</delete>
<update id="updateUser" parameterType="com.example.demo.Bean.User">
UPDATE t_user set username=#{username},jobs=#{jobs},phone=#{phone} WHERE id=#{id}
</update>
<select id="getUserById" parameterType="int" resultType="com.example.demo.Bean.User">
SELECT * FROM t_user WHEN id=#{id}
</select>
<select id="getAllUsers" resultType="com.example.demo.Bean.User">
SELECT * FROM t_user
</select>
</mapper>

然后对之前的UserService 类进行改造

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Service
public class UserService {

@Autowired
UserMapper userMapper;
public int addUser(User user){
return userMapper.addUser(user);
}
public int updateUser(User user){
return userMapper.updateUser(user);
}
public int deleteUser(Integer id){
return userMapper.deleteUser(id);
}
public User getUserById(Integer id){
return userMapper.getUserById(id);
}
public List<User> getAllUser(){
return userMapper.getAllUsers();
}
}

然后我们在application.properties中添加mybatis 的配置

1
2
mybatis.typeAliasesPackage=com.example.demo.Mapper
mybatis.mapperLocations=classpath:Mapper/*.xml

最后,我们修改下UserController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/user")
public void UserOps(){
User u1 = new User();
u1.setName("李思思");
u1.setJobs("美工");
u1.setPhone("131444224443");
int i = userService.addUser(u1);
System.out.println(i);
List<User> allUser = userService.getAllUser();
System.out.println(allUser);
}
}

运行程序,我们访问结果如下

20200421105525

查询数据库

20200421105657

整个工程的目录结构如下

20200421110020