第一步:数据库添加逻辑字段
第二步:实体类添加对应属性和注解
@TableLogic
private Integer logicFlag;
第三步:MybatisPlus全局配置中加入logicSqlInjector
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 在2.3版本以后,dbColumnUnderline 默认值就是true -->
<property name="dbColumnUnderline" value="true"></property>
<!-- 注入逻辑删除 -->
<property name="sqlInjector" ref="logicSqlInjector"></property>
<!-- 注入逻辑删除全局值 -->
<property name="logicDeleteValue" value = "-1"></property>
<property name="logicNotDeleteValue" value="1"></property>
</bean>
<!-- 逻辑删除 -->
<bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector"></bean>
第s四步:测试
@Test
public void testLogicSqlInjector() {
User user=new User();
/**
* 状态为: 1时
* DEBUG [main] - ==> Preparing: DELETE FROM tab_user_vsersion WHERE id=?
* DEBUG [main] - ==> Parameters: 7(Long)
* DEBUG [main] - <== Updates: 1
*
* 状态为: -1时
* DEBUG [main] - ==> Preparing: DELETE FROM tab_user_vsersion WHERE id=?
* DEBUG [main] - ==> Parameters: 7(Long)
* DEBUG [main] - <== Updates: 0
*/
user.deleteById(7L);
}