SpringBoot全家桶

SpringBoot 全家桶 | MyBatisPlus(六)软

2020-09-24  本文已影响0人  码农StayUp

本文源码:Gitee·点这里

在项目中软删除是必须用到的,那么需要一些配置来支持软删除

mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: deleted  # 全局逻辑删除的实体字段名
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

logic-delete-field 用于配置全局软删除实体字段名

logic-delete-value软删除值

logic-not-delete-value未软删除值

也可单独配置某个实体类的软删除字段,在字段上加上@TableLogic注解即可:

@TableLogic
private boolean deleted;

插入

插入时会自动将deleted设为false

@Test
public void testInsert() {
    Dept dept = new Dept();
    dept.setName("总经办");
    int result = deptMapper.insert(dept);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@1631959070 wrapping com.mysql.cj.jdbc.ConnectionImpl@64355120] will not be managed by Spring
==>  Preparing: INSERT INTO dept ( id, name, deleted ) VALUES ( ?, ?, ? )
==> Parameters: 8858788c7b5f5dc28b78110ea1810de7(String), 总经办(String), false(Boolean)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7573e12f]
1

删除

删除时,实际是执行update语句,将deleted设为true

@Test
public void testDeleteById() {
    int result = deptMapper.deleteById("8858788c7b5f5dc28b78110ea1810de7");
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@719146276 wrapping com.mysql.cj.jdbc.ConnectionImpl@3f93e4a8] will not be managed by Spring
==>  Preparing: UPDATE dept SET deleted=true WHERE id=? AND deleted=false
==> Parameters: 8858788c7b5f5dc28b78110ea1810de7(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@70e02081]
1

查询

查询时,带上deleted = false的条件

@Test
public void testSelectById() {
    Dept dept = deptMapper.selectById("4b0e878b5bfc2f22f44f1a3691403116");
    System.out.println(dept);
}

执行日志:

JDBC Connection [HikariProxyConnection@987950392 wrapping com.mysql.cj.jdbc.ConnectionImpl@7f572c37] will not be managed by Spring
==>  Preparing: SELECT id,name,deleted,create_time,update_time FROM dept WHERE id=? AND deleted=false
==> Parameters: 4b0e878b5bfc2f22f44f1a3691403116(String)
<==    Columns: id, name, deleted, create_time, update_time
<==        Row: 4b0e878b5bfc2f22f44f1a3691403116, 研发部, 0, null, null
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5e4fa1da]
Dept{id='4b0e878b5bfc2f22f44f1a3691403116', name='研发部', deleted=false, createTime=null, updateTime=null}
上一篇下一篇

猜你喜欢

热点阅读