删除操作
物理删除
1 2 3 4 5 6 7
| userMapper.deleteBatchIds (Arrays.asList (4L,2L,3L)); userMapper.deleteById (1L); -------------------------------------------------------------
HashMap<String, Object> map = new HashMap<> (); map.put ("name","saxon"); userMapper.deleteByMap (map);
|
逻辑删除
实际上就是在数据库里面添加一个字段,然后再查询的时候,把这个字段加入我们的数据库条件查询的条件;
使用方法:
1 2 3 4 5 6
| mybatis-plus: global-config: db-config: logic-delete-field: flag logic-delete-value: 1 logic-not-delete-value: 0
|
步骤2: 实体类字段上加上@TableLogic注解
1 2
| @TableLogic private Integer deleted;
|
执行 SQL 分析打印
依赖:
1 2 3 4 5
| <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency>
|
application.yaml
1 2 3 4 5 6 7 8
| spring: profiles: dev datasource: url: jdbc:p6spy:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT%2B8 username: root password: 123456 driver-class-name: com.p6spy.engine.spy.P6SpyDriver type: com.p6spy.engine.spy.P6DriverManagerDataSource
|
spy.properties 配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
deregisterdrivers=true
useprefix=true
excludecategories=info,debug,result,commit,resultset
dateformat=yyyy-MM-dd HH:mm:ss
outagedetection=true
outagedetectioninterval=1
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat customLogMessageFormat=%(currentTime) | SQL waste Time: %(executionTime) ms | connect message: %(category)-%(connectionId) | sql : %(sql)
|
1
| 2020-10-30 16:23:34 | SQL waste Time: 2 ms | connect message: statement-0 | sql : UPDATE user SET deleted=1 WHERE id=1322017096762994691 AND deleted=0
|
日志输出