使用LambdaQueryWrapper动态加过滤条件 动态Lambda

 更新时间:2022年01月11日 08:58:14   作者:陕西小伙伴网络科技有限公司  
这篇文章主要介绍了使用LambdaQueryWrapper动态加过滤条件 动态Lambda,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。

LambdaQueryWrapper动态加过滤条件 动态Lambda

1、遇到这样的需求,在baseservice类中处理数据权限,子类可能使用QueryWrapper或者LambdaQueryWrapper调用base类的方法进行查询。

2、可以拿到的:PO的类,数据权限属性的属性名(是固定的)

直接上代码:

  /**
     * 可序列化
     */
    private static final int FLAG_SERIALIZABLE = 1;  
//获取当前登录人权限
 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  转换为 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();
                //数据小于这个级别的都查出来
                queryWrapper.le(func,secretLevel);
            } catch (Throwable e) {
                log.error("获取getSecretLevel方法错误",e);
            }
        }

mybatis-plus QueryWrapper LambdaQueryWrapper

ContractTemplate::getTemplateCode 转为对应的字段

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())
                // 未标志删除的数据
                .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);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Struts2实现多文件上传功能

    Struts2实现多文件上传功能

    这篇文章主要为大家详细介绍了Struts2实现多文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • java遍历读取xml文件内容

    java遍历读取xml文件内容

    这篇文章主要为大家介绍了java遍历读取xml文件内容,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • SpringBoot实现自定义Starter的步骤详解

    SpringBoot实现自定义Starter的步骤详解

    在SpringBoot中,Starter是一种特殊的依赖,它可以帮助我们快速地集成一些常用的功能,例如数据库连接、消息队列、Web框架等。在本文中,我们将介绍如何使用Spring Boot实现自定义Starter,需要的朋友可以参考下
    2023-06-06
  • 基于JVM 调优的技巧总结分析

    基于JVM 调优的技巧总结分析

    本篇文章是对JVM 调优的技巧进行了总结和分析。需要的朋友参考下
    2013-05-05
  • 解决springboot 连接 mysql 时报错 using password: NO的方案

    解决springboot 连接 mysql 时报错 using password: NO的方案

    在本篇文章里小编给大家整理了关于解决springboot 连接 mysql 时报错 using password: NO的方案,有需要的朋友们可以学习下。
    2020-01-01
  • java把excel内容上传到mysql实例代码

    java把excel内容上传到mysql实例代码

    这篇文章主要介绍了java把excel内容上传到mysql实例代码,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • java 非对称加密算法RSA实现详解

    java 非对称加密算法RSA实现详解

    这篇文章主要介绍了java 非对称加密算法RSA实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • MyBatis获取参数值的两种方式详解

    MyBatis获取参数值的两种方式详解

    本文主要介绍了MyBatis获取参数值的两种方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Maven Pom 文件中的隐式依赖导致Jar冲突问题

    Maven Pom 文件中的隐式依赖导致Jar冲突问题

    这篇文章主要介绍了Maven Pom 文件中的隐式依赖导致Jar冲突问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • SpringApplicationRunListener监听器源码详解

    SpringApplicationRunListener监听器源码详解

    这篇文章主要介绍了SpringApplicationRunListener监听器源码详解,springboot提供了两个类SpringApplicationRunListeners、SpringApplicationRunListener(EventPublishingRunListener),spring框架还提供了一个ApplicationListener接口,需要的朋友可以参考下
    2023-11-11

最新评论