MyBatisPlus中全局Sql注入器应用_逻辑删除使用
場景
項目搭建專欄:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194
MyBatisPlus中自定義全局操作流程:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89608052
什么是邏輯刪除
假刪除、邏輯刪除,并不會真正的從數據庫中將數據刪除掉,而是將當前被刪除的
這條數據的一個邏輯字段置為刪除狀態。
實現
全局配置注入LogicSqlInjector
打開項目的applicationContext.xml
<!-- 配置邏輯刪除 --> <bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector"></bean>然后在全局策略配置中注入<!-- 定義MybatisPlus的全局策略配置--><bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration"><!-- 在2.3版本以后,dbColumnUnderline 默認值就是true 開啟下劃線到駝峰命名支持--><property name="dbColumnUnderline" value="true"></property><!-- 全局的主鍵策略?? 全局使用主鍵自增的方式? value為 0--><property name="idType" value="0"></property><!-- 全局的表前綴策略配置 --><!-- <property name="tablePrefix" value="tbl_"></property> --><!-- 注入自定義全局操作 --><!-- <property name="sqlInjector" ref="mySqlInjector"></property> --><!-- 注入邏輯刪除--><property name="sqlInjector" ref="logicSqlInjector" /><!-- 注入邏輯刪除全局值--><property name="logicDeleteValue" value="1" /><property name="logicNotDeleteValue" value="0" /></bean>注:
logicDeleteValue? // 邏輯刪除全局值
logicNotDeleteValue // 邏輯未刪除全局值
?
添加logic字段
數據庫中添加logic字段
對應實體類添加字段
@TableLogicprivate Integer logicFlag;//邏輯刪除添加注解@TableLogic并生成get和set方法。
完整實體類
package com.badao.beans;import java.io.Serializable;import com.baomidou.mybatisplus.activerecord.Model; import com.baomidou.mybatisplus.annotations.TableField; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableLogic; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.annotations.Version; import com.baomidou.mybatisplus.enums.IdType; import com.mchange.util.FailSuppressedMessageLogger;@TableName(value="employee") public class Employee? extends Model<Model>{@TableId(value="id",type=IdType.AUTO)private Integer id;//@TableField(value="last_name")private String name;private String email;private Integer gender;private Integer age;@TableField(exist=false)private String remark;@Versionprivate Integer version;@TableLogicprivate Integer logicFlag;//邏輯刪除public Integer getVersion() {return version;}public Integer getLogicFlag() {return logicFlag;}public void setLogicFlag(Integer logicFlag) {this.logicFlag = logicFlag;}public void setVersion(Integer version) {this.version = version;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}/**** 必須指定當前實體類的主鍵屬性*/@Overrideprotected Serializable pkVal() {// TODO Auto-generated method stubreturn id;}}測試
編寫測試方法
/****? 邏輯刪除*/@Testpublic void testlogicdelete() {int result = employeeMapper.deleteById(24);System.out.println("***********"+result);}效果
會在mp自帶查詢和更新方法的sql后面,追加『邏輯刪除字段』=『LogicNotDeleteValue默認值』 刪除方法: deleteById()和其他delete方法, 底層SQL調用的是update tbl_xxx set 『邏輯刪除字段』=『logicDeleteValue默認值』
此時再查看數據庫,邏輯刪除字段已經變為1。
此時修改測試方法查詢id為24的實體類。
/****? 邏輯刪除*/@Testpublic void testlogicdelete() {/*int result = employeeMapper.deleteById(24);System.out.println("***********"+result);*/Employee employee =employeeMapper.selectById(24);System.out.println(employee);}已經查詢不到了。
源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11148612
總結
以上是生活随笔為你收集整理的MyBatisPlus中全局Sql注入器应用_逻辑删除使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中对POI的单元格设置背景色
- 下一篇: MyBatisPLus入门项目实战各教程