使用LambdaQueryWrapper動(dòng)態(tài)加過(guò)濾條件?動(dòng)態(tài)Lambda
LambdaQueryWrapper動(dòng)態(tài)加過(guò)濾條件 動(dòng)態(tài)Lambda
1、遇到這樣的需求,在baseservice類中處理數(shù)據(jù)權(quán)限,子類可能使用QueryWrapper或者LambdaQueryWrapper調(diào)用base類的方法進(jìn)行查詢。
2、可以拿到的:PO的類,數(shù)據(jù)權(quán)限屬性的屬性名(是固定的)
直接上代碼:
/** * 可序列化 */ private static final int FLAG_SERIALIZABLE = 1; //獲取當(dāng)前登錄人權(quán)限 Integer secretLevel = getUserSecretLevel(); if(secretLevel!=null){ SFunction func = null; final MethodHandles.Lookup lookup = MethodHandles.lookup(); //po的返回Integer的一個(gè)方法 MethodType methodType = MethodType.methodType(Integer.class, entityClass); final CallSite site; try { //方法名叫做:getSecretLevel 轉(zhuǎn)換為 SFunction function interface對(duì)象 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ù)小于這個(gè)級(jí)別的都查出來(lái) queryWrapper.le(func,secretLevel); } catch (Throwable e) { log.error("獲取getSecretLevel方法錯(cuò)誤",e); } }
mybatis-plus QueryWrapper LambdaQueryWrapper
ContractTemplate::getTemplateCode 轉(zhuǎn)為對(duì)應(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()) ? ? ? ? ? ? ? ? // 未標(biāo)志刪除的數(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);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)自定義Starter的步驟詳解
在SpringBoot中,Starter是一種特殊的依賴,它可以幫助我們快速地集成一些常用的功能,例如數(shù)據(jù)庫(kù)連接、消息隊(duì)列、Web框架等。在本文中,我們將介紹如何使用Spring Boot實(shí)現(xiàn)自定義Starter,需要的朋友可以參考下2023-06-06基于JVM 調(diào)優(yōu)的技巧總結(jié)分析
本篇文章是對(duì)JVM 調(diào)優(yōu)的技巧進(jìn)行了總結(jié)和分析。需要的朋友參考下2013-05-05解決springboot 連接 mysql 時(shí)報(bào)錯(cuò) using password: NO的方案
在本篇文章里小編給大家整理了關(guān)于解決springboot 連接 mysql 時(shí)報(bào)錯(cuò) using password: NO的方案,有需要的朋友們可以學(xué)習(xí)下。2020-01-01java把excel內(nèi)容上傳到mysql實(shí)例代碼
這篇文章主要介紹了java把excel內(nèi)容上傳到mysql實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01java 非對(duì)稱加密算法RSA實(shí)現(xiàn)詳解
這篇文章主要介紹了java 非對(duì)稱加密算法RSA實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問(wèn)題
這篇文章主要介紹了Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringApplicationRunListener監(jiān)聽(tīng)器源碼詳解
這篇文章主要介紹了SpringApplicationRunListener監(jiān)聽(tīng)器源碼詳解,springboot提供了兩個(gè)類SpringApplicationRunListeners、SpringApplicationRunListener(EventPublishingRunListener),spring框架還提供了一個(gè)ApplicationListener接口,需要的朋友可以參考下2023-11-11