添加依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>

添加数据库配置

1
2
3
4
5
6
7
8
9
10
11
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kira?characterEncoding=utf8&serverTimezone=UTC&useSSL=false
username: root
password: root
mybatis:
configuration:
map-underscore-to-camel-case: true

创建实体类

1
2
3
4
5
6
7
8
9
10
11
12
package com.kagarise.kira.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
private Integer userId;
private String userName;
private String userPwd;
}

创建动态SQL语句

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
package com.kagarise.kira.provider;

import com.kagarise.kira.entity.User;
import org.apache.ibatis.jdbc.SQL;

public class UserSqlProvider {
public String insertUser(final User user) {
return new SQL() {{
INSERT_INTO("user");
// INTO_COLUMNS("user_name", "user_pwd");
// INTO_VALUES("#{userName}", "#{userPwd}");
VALUES("user_name", "#{userName}");
if (user.getUserPwd() != null) {
VALUES("user_pwd", "#{userPwd}");
} else {
VALUES("user_pwd", "123456");
}
}}.toString();
}

public String updateUser(final User user) {
return new SQL() {{
UPDATE("user");
SET("user_id=#{userId}");
if (user.getUserName() != null)
SET("user_name=#{userName}");
if (user.getUserPwd() != null)
SET("user_pwd=#{userPwd}");
WHERE("user_id=#{userId}");
}}.toString();
}

public String deleteUser() {
return new SQL() {{
DELETE_FROM("user");
WHERE("user_id=#{userId}");
}}.toString();
}

public String selectUser(final User user) {
return new SQL() {{
SELECT("*");
FROM("user");
if (user.getUserId() != 0)
WHERE("user_id=#{userId}");
if (user.getUserName() != null) {
WHERE("user_name=#{userName}");
if (user.getUserPwd() != null)
WHERE("user_pwd=#{userPwd}");
}
}}.toString();
}
}

创建接口

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
package com.kagarise.kira.mapper;

import com.kagarise.kira.entity.User;
import com.kagarise.kira.provider.UserSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

@Select("SELECT * FROM user")
List<User> findAll();

// @Select("SELECT * FROM user WHERE user_id = #{userId}")
@SelectProvider(type = UserSqlProvider.class, method = "selectUser")
User selectUser(User user);

// @Insert("INSERT INTO user (user_name, user_pwd) VALUES(#{userName}, #{userPwd})")
@InsertProvider(type = UserSqlProvider.class, method = "insertUser")
@Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
boolean insertUser(User user);

// @Delete("DELETE FROM user WHERE user_id = #{userId}")
@DeleteProvider(type = UserSqlProvider.class, method = "deleteUser")
boolean deleteUser(int userId);

// @Update("UPDATE user SET user_name = #{userName},user_pwd = #{userPwd} WHERE user_id = #{userId}")
@UpdateProvider(type = UserSqlProvider.class, method = "updateUser")
boolean updateUser(User 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
package com.kagarise.kira.controller;

import com.kagarise.kira.entity.User;
import com.kagarise.kira.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
@Autowired
UserMapper userMapper;

@GetMapping("/users")
public List<User> findAll() {
return userMapper.findAll();
}

@GetMapping("/user")
public User selectUser(User user) {
return userMapper.selectUser(user);
}

@PostMapping("/user")
public boolean insertUser(@RequestBody User user) {
User nUser = new User();
nUser.setUserName(user.getUserName());
User sUser = userMapper.selectUser(nUser);
if (sUser != null && sUser.getUserId() != 0)
return false;
return userMapper.insertUser(user);
}

@DeleteMapping("/user/{userId}")
public boolean deleteUser(@PathVariable int userId) {
return userMapper.deleteUser(userId);
}

@PatchMapping("/user")
public boolean updateUser(@RequestBody User user) {
return userMapper.updateUser(user);
}

}