简介:
素材来源于网络,加入个人理解进行组装,减少后台代码量(偷懒),在代码配置化得路上远行
满足条件:
基础条件赛选(模糊查询)
范围筛选(此文针对于时间,其他类似)
基础分页排序等
优点:代码一次成型,减少代码维护成本(非个性化需求)
缺点:范围筛选部分代码量较多,不易理解
表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.htmhttps://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; } }