MyBatisPlus系列九 自定義全域性操作

2021-09-20 13:15:30 字數 3890 閱讀 2515

int deleteall();

}

/**

* 自定義全域性操作

*/public class mysqlinjector extends autosqlinjector

}

(3)、在 mybatisplus 全域性策略中,配置自定義注入器。

測試:

@test

public void testmysqlinjector()

控制台輸出:

preparing: delete from tbl_employee

parameters:

updates: 4

result: 4

execute sql:

delete

from

tbl_employee]

2、自定義注入器的應用之邏輯刪除

假刪除、邏輯刪除: 並不會真正的從資料庫中將資料刪除掉,而是將當前被刪除的這條資料中的乙個邏輯刪除欄位置為刪除狀態。 logic_flag → -1

建立表和資料:

(4)、在 pojo 的邏輯刪除字段新增 @tablelogic 註解。

public class user 

public void setid(integer id)

public string getname()

public void setname(string name)

public integer getlogicflag()

public void setlogicflag(integer logicflag)

@override

public string tostring()

}

(5)、會在 mybatisplus 自帶查詢和更新方法的 sql 後面,追加『邏輯刪除字段』 = 『logicnotdeletevalue 預設值』 刪除方法: deletebyid()和其他 delete 方法, 底層 sql 呼叫的是 update tbl_*** set 『邏輯刪除字段』=『logicdeletevalue 預設值』

@test

public void testlogicdelete()

控制台輸出:

deletebyid方法:

preparing: update tbl_user set logic_flag=-1 where id=?

parameters: 1(integer)

updates: 1

result:1

execute sql:

update

tbl_user

setlogic_flag=-1

where

id=1]

selectbyid方法:

preparing: select id,`name`,logic_flag as logicflag from tbl_user where id=? and logic_flag=1

parameters: 1(integer)

total: 0

result:null

execute sql:

select

id,`name`,

logic_flag as logicflag

from

tbl_user

where

id=1

and logic_flag=1]

原理分析:

public class logicsqlinjector extends autosqlinjector 

if (table.islogicdelete())

string sql = string.format(sqlmethod.getsql(), table.gettablename(), this.sqllogicset(table), table.getkeycolumn(), idstr);

sqlsource sqlsource = this.languagedriver.createsqlsource(this.configuration, sql, modelclass);

} else

}if (table.islogicdelete()) else

}if (table.islogicdelete()) else

}if (table.islogicdelete()) else

} else

}if (table.islogicdelete()) =#

" + this.getlogicdeletesql(table));

sqlsource sqlsource = this.languagedriver.createsqlsource(this.configuration, sql, modelclass);

} else

}public string getlogicdeletesql(tableinfo table) else }}

return sql.tostring();

}protected string sqllogicset(tableinfo table)

if (stringutils.ischarsequence(fieldinfo.getpropertytype())) else }}

return set.tostring();

}protected string sqlwhere(tableinfo table) else

iterator i$ = fieldlist.iterator();

while(i$.hasnext())

return where.tostring();}}

if (!table.islogicdelete()) else

listfieldlist = table.getfieldlist();

iterator i$ = fieldlist.iterator();

while(i$.hasnext())

");");

return where.tostring();}}

protected string sqlwherebymap(tableinfo table) else }}

MyBatisPlus系列十 公共字段自動填充

元資料處理器介面 insertfill metaobject metaobject updatefill metaobject metaobject metaobject 元物件。是 mybatis 提供的乙個用於更加方便,更加優雅的訪問物件的屬性,給物件的屬性設定值 的乙個物件。還會用於包裝物件....

mybatis plus系列 快速體驗樣例工程

rul set foreign key checks 0 drop table if existst mybatis plus user create tablet mybatis plus user idint not null,namevarchar 127 default null,ageti...

Elasticsearch系列九 Bool 查詢

案例書籍推薦 bool查詢對應lucenne中的booleanquery,它由乙個或者多個子句組成,match 分詞匹配,下面的例子會對查詢盡心分詞,分為 寶馬 多少 馬力 那麼所有包含這三個詞中的乙個或者多個文件就會被搜尋出來.match phrase 針對上面的例子,乙個文件 我的保時捷也不錯 ...