使用LambdaQueryWrapper動態(tài)加過濾條件?動態(tài)Lambda
LambdaQueryWrapper動態(tài)加過濾條件 動態(tài)Lambda
1、遇到這樣的需求,在baseservice類中處理數(shù)據(jù)權(quán)限,子類可能使用QueryWrapper或者LambdaQueryWrapper調(diào)用base類的方法進行查詢。
2、可以拿到的:PO的類,數(shù)據(jù)權(quán)限屬性的屬性名(是固定的)
直接上代碼:
/**
* 可序列化
*/
private static final int FLAG_SERIALIZABLE = 1;
//獲取當前登錄人權(quán)限
Integer secretLevel = getUserSecretLevel();
if(secretLevel!=null){
SFunction func = null;
final MethodHandles.Lookup lookup = MethodHandles.lookup();
//po的返回Integer的一個方法
MethodType methodType = MethodType.methodType(Integer.class, entityClass);
final CallSite site;
try {
//方法名叫做:getSecretLevel 轉(zhuǎn)換為 SFunction function interface對象
site = LambdaMetafactory.altMetafactory(lookup,
"invoke",
MethodType.methodType(SFunction.class),
methodType,
lookup.findVirtual(entityClass, "getSecretLevel", MethodType.methodType(Integer.class)),
methodType,FLAG_SERIALIZABLE);
func = (SFunction) site.getTarget().invokeExact();
//數(shù)據(jù)小于這個級別的都查出來
queryWrapper.le(func,secretLevel);
} catch (Throwable e) {
log.error("獲取getSecretLevel方法錯誤",e);
}
}mybatis-plus QueryWrapper LambdaQueryWrapper
ContractTemplate::getTemplateCode 轉(zhuǎn)為對應(yīng)的字段
LambdaQueryWrapper<SomeClass> objectLambdaQueryWrapper = Wrappers.lambdaQuery(); ? ? ? ? objectLambdaQueryWrapper.eq(searchDto.getTemplateCode() != null, ContractTemplate::getTemplateCode, searchDto.getTemplateCode()); ? ? ? ? IPage<SomeClass> page = page(MybatisPlusUtil.setPageParams(pageNo, pageSize), objectLambdaQueryWrapper); ? ? ? return page
QueryWrapper
QueryWrapper<SomeClass> queryWrapper = Wrappers.query(); ? ? ? ? queryWrapper.eq(searchDto.getTemplateCode() != null, TemplateConst.COL_TEMPLATE_CODE, searchDto.getTemplateCode()) ? ? ? ? ? ? ? ? .ge(searchDto.getStartDate() != null, TemplateConst.COL_CREATE_TIME, searchDto.getStartDate()) ? ? ? ? ? ? ? ? .lt(searchDto.getEndDate() != null, TemplateConst.COL_CREATE_TIME, DateUtils.addDays(searchDto.getEndDate(), 1)) ? ? ? ? ? ? ? ? .eq(searchDto.getContractCategoryId() != null, TemplateConst.COL_CATEGORY_ID, searchDto.getContractCategoryId()) ? ? ? ? ? ? ? ? .and(i -> i.like(TemplateConst.COL_TEMPLATE_NAME, searchDto.getKeyword()).or().like(TemplateConst.COL_DESCRIPTION, searchDto.getKeyword())) ? ? ? ? ? ? ? ? .eq(searchDto.getIs_enabled() != null, TemplateConst.COL_IS_ENABLED, searchDto.getIs_enabled()) ? ? ? ? ? ? ? ? // 未標志刪除的數(shù)據(jù) ? ? ? ? ? ? ? ? .eq(CommonConst.DBColName.DEL_FLAG, CommonConst.IsEnabled.ENABLED.getCode()) ? ? ? ? ? ? ? ? .orderByDesc(TemplateConst.COL_CREATE_TIME) ? ? ? ? ? ? ? ? ; ? ? ? ? IPage<ContractTemplate> page = page(MybatisPlusUtil.setPageParams(pageNo, pageSize), queryWrapper); ? ? ? ? return MybatisPlusUtil.parsePageDTO(page);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java基礎(chǔ)強化訓(xùn)練輸入錯誤即結(jié)束進程
本文主要介紹了Java編程的基礎(chǔ)知識強化應(yīng)用,文中實例涉及到了許多基礎(chǔ)知識,new對象,控制臺輸入,if語句等。很實用,需要的朋友可以參考下2017-09-09
關(guān)于HashSet與HashMap的區(qū)別及說明
這篇文章主要介紹了關(guān)于HashSet與HashMap的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot可視化監(jiān)控的具體應(yīng)用
最近越發(fā)覺得,任何一個系統(tǒng)上線,運維監(jiān)控都太重要了,本文介紹了SpringBoot可視化監(jiān)控的具體應(yīng)用,分享給大家,有興趣的同學(xué)可以參考一下2021-06-06
在IDEA中 實現(xiàn)給main方法附帶參數(shù)的操作
這篇文章主要介紹了在IDEA中 實現(xiàn)給main方法附帶參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

