**
MybatisPlus——邏輯洗掉
**
首先我們要知道為什么要進行邏輯洗掉:
邏輯洗掉可以理解為假洗掉,并不是真的將資料進行洗掉,
假洗掉只是為了資料安全起見,在資料庫中增添一個欄位,我們可以默認這個欄位為1時資料被洗掉,而欄位值為0時資料被洗掉
邏輯洗掉是為了方便資料恢復和保護資料本身價值的一種方案,我們電腦里的回收站正是利用了邏輯洗掉,我們點擊洗掉的檔案不會被立刻洗掉,而是放入回收站,等我們反悔時可以將其進行還原,
在MybatisPlus中設定邏輯洗掉有以下幾個步驟
1.首先為Strudent表添加logic_delete欄位,通過判斷logic_delete欄位的數值,我們可以確定資料是否已被洗掉

2.如果logic_delete欄位在資料庫中并沒有設定默認值(推薦設定默認值,這樣更加方便),我們需要在applicationContext.xml檔案中對默認值進行設定,已設定默認值可跳過此步
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<!--配置globalConfig,在全域中對默認值進行設定-->
<property name="globalConfig" ref="globalConfig"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.xml"></property>
<property name="typeAliasesPackage" value="com.yq.entity"></property>
</bean>
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
<!-默認被洗掉為1-->
<property name="logicDeleteValue" value="1"></property>
<!-默認未被洗掉為0-->
<property name="logicNotDeleteValue" value="0"></property>
</bean>
</property>
</bean>
3.物體類屬性與表中欄位保持一致,屬性 logicDelete添加@TableLogic注解
package com.yq.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
public class Student extends Model<Student> {
@TableId(type = IdType.AUTO)
private int stuNo;
private String stuName;
@Version
private Integer version;
private int stuAge;
//邏輯洗掉欄位
@TableLogic
private Integer logicDelete;//這里用Integer默認為null,而int默認為0,0會產生誤解
public Student(int stuNo, String stuName, Integer version, int stuAge) {
this.stuNo = stuNo;
this.stuName = stuName;
this.version = version;
this.stuAge = stuAge;
}
public Student(int stuNo, String stuName, int stuAge) {
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
}
public Student(String stuName, int stuAge) {
this.stuName = stuName;
this.stuAge = stuAge;
}
public Student() {
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public Integer getLogicDelete() {
return logicDelete;
}
public void setLogicDelete(Integer logicDelete) {
this.logicDelete = logicDelete;
}
}
4.進行簡單呼叫,查看結果
//測驗假刪
public static void testLogicdelete(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentMapper studentMapper = context.getBean("studentMapper", StudentMapper.class);
studentMapper.delete(null);
}
public static void main(String[] args) throws Exception {
testLogicdelete();
}
執行方法之前的資料庫表:

執行方法之后的資料庫表:

此時我們查看日志發現
我們原本執行的洗掉陳述句(delete from student ):

在日志中變為對logic_delete欄位的更新操作:

執行洗掉陳述句后資料并沒有被直接洗掉,而且MybatisPlus自動將logic_delete欄位的值更新為1,使其處于假刪狀態,保證了資料安全,
當我們資料庫中所有資料的欄位logic_delete為1,也就是被洗掉時,我們進行查詢全部操作,發現
select * from student陳述句被轉換為
SELECT stu_no,stu_name,version,stu_age,logic_delete FROM student WHERE logic_delete=0

MybatisPlus為我們自動進行了拼接操作將 WHERE logic_delete=0 在查詢陳述句后進行拼接,以保證我們只能查詢出
logic_delete=0 ,也就是未被洗掉的資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/271248.html
標籤:其他
上一篇:滲透測驗之資訊收集總結
下一篇:演算法競賽的一些小技巧
