使用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; //獲取當(dāng)前登錄人權(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)文章
SpringBoot實現(xiàn)自定義Starter的步驟詳解
在SpringBoot中,Starter是一種特殊的依賴,它可以幫助我們快速地集成一些常用的功能,例如數(shù)據(jù)庫連接、消息隊列、Web框架等。在本文中,我們將介紹如何使用Spring Boot實現(xiàn)自定義Starter,需要的朋友可以參考下2023-06-06基于JVM 調(diào)優(yōu)的技巧總結(jié)分析
本篇文章是對JVM 調(diào)優(yōu)的技巧進行了總結(jié)和分析。需要的朋友參考下2013-05-05解決springboot 連接 mysql 時報錯 using password: NO的方案
在本篇文章里小編給大家整理了關(guān)于解決springboot 連接 mysql 時報錯 using password: NO的方案,有需要的朋友們可以學(xué)習(xí)下。2020-01-01Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問題
這篇文章主要介紹了Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringApplicationRunListener監(jiān)聽器源碼詳解
這篇文章主要介紹了SpringApplicationRunListener監(jiān)聽器源碼詳解,springboot提供了兩個類SpringApplicationRunListeners、SpringApplicationRunListener(EventPublishingRunListener),spring框架還提供了一個ApplicationListener接口,需要的朋友可以參考下2023-11-11