jpa-Example 单表模糊查询

 
        AreaEntity areaEntityExample = new  AreaEntity();

        // 设置parentId 的精确匹配 
        areaEntityExample.setParentId(req.getParentId());
        areaEntityExample.setDelFlag(false);


        // 模糊查询
        //实例化对象
        ExampleMatcher matching = ExampleMatcher.matching();
        if(StringUtils.isNotBlank(req.getVagueName())){
            //设置搜索的字段 模糊匹配 (name 是 类属性,而不是 表字段,比如 可以用 fullName 而不是 full_name)
            matching = matching.withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            // 接收前端 传过来的值
            apsHSysAreaEntityExample.setName(req.getVagueName());
        }

        Example<ApsHSysAreaEntity> example = Example.of(apsHSysAreaEntityExample,matching);


        List<ApsHSysAreaEntity> sonListFromDb = repository.findAll(example);

2. 不用框架的 findAll

JPAQueryFactory queryFactory = SpringUtil.getBean(JPAQueryFactory.class);

        String numberOrShortName = req.getQuery().getNumberOrShortName();
        // QApsHCustomerManageEntity
        //查询排产数据
        JPAQuery<ApsHCustomerManageResp> query = queryFactory.select(Projections.fields(ApsHCustomerManageResp.class,
                customerEntity.uid,
                customerEntity.address,
                customerEntity.cityId,
                customerEntity.city,
                customerEntity.country,
                customerEntity.countryId,
                customerEntity.province,
                customerEntity.provinceId,
                customerEntity.discount,
                customerEntity.fax,
                customerEntity.fullName,
                customerEntity.shortName,
                customerEntity.mailbox,
                customerEntity.number,
                customerEntity.priority,
                customerEntity.operationType,
                customerEntity.postalCode,
                customerEntity.transportationDuration,
                customerEntity.status,
                customerEntity.tel,
                customerEntity.remark,
                customerEntity.website
        )).from(customerEntity).where(customerEntity.delFlag.eq(false)

                .and(customerEntity.number.like("%" + numberOrShortName + "%").or(customerEntity.fullName.like("%" + numberOrShortName + "%")))




        );

  List<ApsHCustomerManageResp> data = query.orderBy(customerEntity.creationDate.desc())
                // 前端从 1 页开始传
                .offset((req.getPageNo() - 1) * req.getPageSize())
                .limit(req.getPageSize())
                .fetch();
        log.info("客户列表的参数 {}", numberOrShortName);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值