Mybaits-Plus实战 逻辑删除(八)

MyBatis-Plus 逻辑删除示例

脚本初始化

schema-h2.sql

1
2
3
4
5
6
7
8
9
10
11
DROP TABLE IF EXISTS user;

CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
is_delete INT(11) NOT NULL DEFAULT 0 COMMENT '是否删除',
PRIMARY KEY (id)
);

data-h2.sql

1
2
3
4
5
6
7
8
DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

添加依赖

父模块 pom.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
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ray</groupId>
<artifactId>mybatis-plus-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>mybatis-plus-study</name>
<description>Demo project for Spring Boot</description>

<modules>
<module>mybatis-plus-study-quickstart-springmvc</module>
<module>mybatis-plus-study-quickstart-springboot</module>
<module>mybatis-plus-study-generator</module>
<module>mybatis-plus-study-crud</module>
<module>mybatis-plus-study-wrapper</module>
<module>mybatis-plus-study-pagination</module>
<module>mybatis-plus-study-auto-fill-metainfo</module>
<module>mybatis-plus-study-logic-delete</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatisplus.version>3.0.1</mybatisplus.version>
</properties>

<dependencies>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>

<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--H2-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<!--Test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

子模块 pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>mybatis-plus-study-logic-delete</artifactId>

<description>MyBatis-Plus 逻辑删除示例</description>

<parent>
<groupId>com.ray</groupId>
<artifactId>mybatis-plus-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

</project>

实体类

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ray.mybatisplusstudylogicdelete.entity;

import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;

/**
* @author Ray
* @date 2018/10/8
*/
@Data
public class User {

private Integer id;
private String name;
private Integer age;
private String email;
@TableLogic
private Integer isDelete;
}

说明

@TableLogic:表字段逻辑处理注解(逻辑删除)

数据访问层

UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.ray.mybatisplusstudylogicdelete.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ray.mybatisplusstudylogicdelete.entity.User;
import org.apache.ibatis.annotations.Mapper;

/**
* @author Ray
* @date 2018/10/8
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

说明

使用@Mapper注解可以不用添加MapperScan(basePackages = ''),但必须在每个访问层添加,比较麻烦。

MP 配置类

PlusConfiguration.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ray.mybatisplusstudylogicdelete.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author Ray
* @date 2018/10/8
*/
@Configuration
public class PlusConfiguration {

@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
}

说明

  1. ISqlInjector: SQL 自动注入器接口
  2. LogicSqlInjector: SQL 逻辑删除注入器

配置文件

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test
username: root
password: root
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql

logging:
level:
com.ray.mybatisplusstudylogicdelete: debug

mybatis-plus:
global-config:
db-config:
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

测试类

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 com.ray.mybatisplusstudylogicdelete;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ray.mybatisplusstudylogicdelete.entity.User;
import com.ray.mybatisplusstudylogicdelete.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Arrays;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusStudyLogicDeleteApplicationTests {

@Resource
private UserMapper userMapper;

@Test
public void testLogicDeleteById() {
userMapper.deleteById(1);
}

@Test
public void testLogicDeleteBatchIds() {
userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
}

@Test
public void testLogicDelete() {
userMapper.delete(new QueryWrapper<User>().eq("age",2));
}

}

测试结果

1
2
3
4
5
6
7
8
9
2018-10-08 19:15:35.414 DEBUG 1308 --- [           main] c.r.m.mapper.UserMapper.delete           : ==>  Preparing: UPDATE user SET is_delete=1 WHERE is_delete=0 AND age = ? 
2018-10-08 19:15:35.481 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.delete : ==> Parameters: 2(Integer)
2018-10-08 19:15:35.485 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.delete : <== Updates: 0
2018-10-08 19:15:35.495 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.deleteBatchIds : ==> Preparing: UPDATE user SET is_delete=1 WHERE id IN ( ? , ? , ? ) AND is_delete=0
2018-10-08 19:15:35.503 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.deleteBatchIds : ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
2018-10-08 19:15:35.507 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.deleteBatchIds : <== Updates: 3
2018-10-08 19:15:35.510 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.deleteById : ==> Preparing: UPDATE user SET is_delete=1 WHERE id=? AND is_delete=0
2018-10-08 19:15:35.511 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.deleteById : ==> Parameters: 1(Integer)
2018-10-08 19:15:35.517 DEBUG 1308 --- [ main] c.r.m.mapper.UserMapper.deleteById : <== Updates: 0
-------------- 本文结束  感谢您的阅读 --------------
0%