概述
在公司项目中大多不会将数据直接删除,而是将数据进行逻辑删除,即加一个字段,0表示未删除,1表示删除。在查询的时候,and,0或1就行了,相关pom文件及配置文件在文章末尾给出
controller
@RequestMapping("/logicDelete")
public Result logicDelete(int id){
if (iTbBrandService.removeById(id)){
return new Result("1","删除成功");
}else {
return new Result("2","删除失败");
}
}
这里的 iTbBrandService.removeById(id) 等效于调用baseMapper的deleteById()方法
看到了嘛,你在application中配置的logic-not-delete-value: 0就and进来的,找出的是未删除的数据,将它置为已删除标识1。记得数据库中deleted字段要有0的记录。
那么使用mybatis-plus的select是否也会有变化呐?
来看
@RequestMapping("/findAll")
public List<TbBrand> testcontroller(){
return iTbBrandService.list();
}
它确实是and上了delete字段,并且查找未删除记录
application
server:
port: 8081
spring:
application:
name: pinyougou-brand
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.126.128:3306/pinyougoudb
username: root
password: root
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
mapper-locations: classpath*:com/pinyougou/brand/mapper/xml/*.xml
logging:
level:
com.pinyougou.brand: debug
pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.3.0.RELEASE</version>
</dependency>
<!--简化代码的工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.12</version>
</dependency>
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
</dependencies>