jpa基于ExampleMatcher,Example实现动态筛选,模糊查询,分页,排序,时间范围筛选

简介:

素材来源于网络,加入个人理解进行组装,减少后台代码量(偷懒),在代码配置化得路上远行

满足条件:

基础条件赛选(模糊查询)

范围筛选(此文针对于时间,其他类似)

基础分页排序等

优点:代码一次成型,减少代码维护成本(非个性化需求)

缺点:范围筛选部分代码量较多,不易理解

表sql:

CREATE TABLE `hospital` (
  `hospital_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '医院ID',
  `hospital_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '医院名称',
  `hospital_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '医院地址',
  `longitude` decimal(10,0) DEFAULT NULL COMMENT '医院经度',
  `dimensionality` decimal(10,0) NOT NULL COMMENT '医院维度',
  `create_by` int(11) DEFAULT NULL COMMENT '创建人',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_by` int(11) DEFAULT NULL COMMENT '修改人',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`hospital_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='医院表';

INSERT INTO hospital (hospital_id, hospital_name, hospital_address, longitude, dimensionality, create_by, create_time, update_by, update_time) VALUES(1, '太平大药房(新华路店)', '天津市和平区新华路203号', 23, 46, 1, '2022-04-15 10:08:40', 1, '2022-04-15 10:08:44');
INSERT INTO hospital (hospital_id, hospital_name, hospital_address, longitude, dimensionality, create_by, create_time, update_by, update_time) VALUES(2, '和平医药(和平大药房)', '天津和平区南市街多伦道226号药店', 56, 447, 1, '2022-04-15 10:19:31', 1, '2022-04-15 10:19:37');

实体类创建,参数类创建,此处也可用实体类做参数类(个人喜好)

jpa实现类创建

 

public interface HospitalRepo extends JpaRepository<Hospital, Integer> {

    public Hospital findTopByHospitalId(Integer id);


    Page<Hospital> queryByExampleWithRange(Example example, List<Range<Hospital>> ranges, Pageable pageable);


}

 参数类继承了分页实现,此处为自己编辑书写,并加入字段过滤条件属性(后面会介绍)

@Data
public class PageAndSortReq {
    private Integer page;
    private Integer size;
    private List<Order> sort;

    @ApiModelProperty(value = "字段过滤条件", required = true)
    private List<MatcherReq> MatcherList;

    @Data
    @Accessors(chain = true)
    public static class MatcherReq {
        @ApiModelProperty(value = "字段码", required = true)
        private String name;
        @ApiModelProperty(value = "过滤类型:E:根据后缀模糊查询B:根据包含模式查询A:根据前缀模糊查询", required = true)
        private String type;
    }

    public Pageable getPageable() {
        List<Sort.Order> sortOrders = new ArrayList<>();
        if (page == null) {
            page = 0;
        }
        if (size == null) {
            size = 20;
        }
        if (sort != null && sort.size() > 0) {
            for (Order order : sort) {
                sortOrders.add(new Sort.Order(Sort.Direction.valueOf(order.getDirection()), order.getProperty()));
            }

            return PageRequest.of(page, size, Sort.by(sortOrders));
        } else {
            return PageRequest.of(page, size);
        }
    }

    public static class Order {
        private String direction;
        private String property;

        public String getDirection() {
            return direction;
        }

        public Order setDirection(String direction) {
            this.direction = direction;
            return this;
        }

        public String getProperty() {
            return property;
        }

        public Order setProperty(String property) {
            this.property = property;
            return this;
        }
    }

    public Integer getPage() {
        return page;
    }

    public PageAndSortReq setPage(Integer page) {
        this.page = page;
        return this;
    }

    public Integer getSize() {
        return size;
    }

    public PageAndSortReq setSize(Integer size) {
        this.size = size;
        return this;
    }

    public List<Order> getSort() {
        return sort;
    }

    public PageAndSortReq setSort(List<Order> sort) {
        this.sort = sort;
        return this;
    }
}

范围筛选可参考地址,上面有源码地址(源码一扒直接能用)https://www.jb51.net/article/117163.htmicon-default.png?t=M3K6https://www.jb51.net/article/117163.htm基于时间筛选做了封装(减少代码量),基于考虑大部分筛选用createTime,updateTime,如下

@Override
public Page<Hospital> queryHospital(Integer userId, HospitalParam hospitalParam) throws Exception {
    Hospital hospital=new Hospital();
    //页面传参放入实体,用于筛选
    BeanUtils.copyProperties(hospitalParam, hospital);
    ExampleMatcher matcher=ExampleMatcher.matchingAll();
    List<PageAndSortReq.MatcherReq> matcherList=hospitalParam.getMatcherList();
    //此处为字段筛选条件,PageAndSortReq 中有定义,优点(减少代码修改,条件由页面决定)
    if(matcherList.size()>0){
        matcher =TimeRange.addExampleMatcher(matcher,matcherList);
    }
    Example example = Example.of(hospital, matcher);
    //时间字段为参数类中定义,满足大部分条件,缺点:其他字段时间需进行书写
    List<Range<Hospital>> rangeList= TimeRange.addTime(hospitalParam.getStartTime(),hospitalParam.getEndTime(),hospitalParam.getStartUpdateTime(),hospitalParam.getEndUpdateTime());
    return hospitalRepo.queryByExampleWithRange(example,rangeList,hospitalParam.getPageable());
}

TimeRange为自己封装得类

public class TimeRange {

    //时间筛选封装,也可根据实际情况修改
    public static <T>List<Range<T>> addTime(Timestamp startDate, Timestamp endDate, Timestamp startUpdateDate, Timestamp endUpdateDate){
        List<Range<T>> ranges = newArrayList();
        if(startDate!=null && endDate!=null) {
            Range<T> createRange = new Range<T>("createTime", startDate, endDate);
            ranges.add(createRange);
        }
        if(startUpdateDate!=null && endUpdateDate!=null) {
            Range<T> updateRange = new Range<T>("updateTime", startUpdateDate, endUpdateDate);
            ranges.add(updateRange);
        }
        return ranges;
    }

    //字段过滤条件
    public static ExampleMatcher addExampleMatcher(ExampleMatcher matcher,List<PageAndSortReq.MatcherReq> matcherList){
        for (PageAndSortReq.MatcherReq matcherReq:matcherList){
            if(matcherReq.getType().equals("A")){
                matcher=matcher.withMatcher(matcherReq.getName(), ExampleMatcher.GenericPropertyMatcher::startsWith);
            }else if(matcherReq.getType().equals("B")){
                matcher=matcher.withMatcher(matcherReq.getName(), ExampleMatcher.GenericPropertyMatcher::contains);
            }else if(matcherReq.getType().equals("E")){
                matcher=matcher.withMatcher(matcherReq.getName(), ExampleMatcher.GenericPropertyMatcher::endsWith);
            }else {
                matcher=matcher.withMatcher(matcherReq.getName(), ExampleMatcher.GenericPropertyMatcher::contains);
            }
        }
        return matcher;
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的示范代码展示了如何使用ExampleMatcher进行模糊查询。在示范代码中,对姓名字段进行了前后模糊查询,而对学号字段进行了精确查询。 引用提到,在使用ExampleMatcher时,可以根据需要对不同字段使用不同的查询方式。比如,可以对姓名字段实现前后模糊查询,而对学号字段实现精确查询。 根据以上引用内容,可以使用ExampleMatcher实现模糊查询,具体步骤如下: 1. 创建一个ExampleMatcher对象,并使用matching()方法进行初始化。 2. 使用withMatcher()方法来指定字段的查询方式。例如,对于姓名字段,可以使用contains()方法来实现前后模糊查询。 3. 将ExampleMatcher对象与实体对象一起传入Example.of()方法中,创建一个Example对象。 4. 使用Example对象进行查询操作,可以通过调用相应的查询方法来实现模糊查询。 请根据具体的需求和示范代码中的实现方式,适当修改代码以实现模糊查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [ExampleMatcher 针对不同字段实现不同方式查询](https://blog.csdn.net/weixin_43481812/article/details/115615691)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [jpa基于ExampleMatcherExample实现动态筛选模糊查询分页排序时间范围筛选](https://blog.csdn.net/Years_of_sorrow/article/details/124444759)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值