c/c++语言开发共享JPA Specification常用查询+排序实例

目录jpa specification常用查询+排序1.第一步:继承父类2.第二步jpa specification复杂查询+排序需求开始了一、dao二、service三、排序前端结束语jpa spe

目录
  • jpa specification常用查询+排序
    • 1.第一步:继承父类
    • 2.第二步
  • jpa specification复杂查询+排序
    • 需求
    • 开始了
      • 一、dao
      • 二、service
      • 三、排序
    • 前端
      • 结束语

      jpa specification常用查询+排序

      1.第一步:继承父类

        public interface tblcarton2rcardlogrepository extends jparepository<tblcarton2rcardlog, string>,jpaspecificationexecutor<tblcarton2rcardlog> {

      2.第二步

        tblcarton2rcardlogrepository.findall(new specification<tblcarton2rcardlog>() {      @override      public  predicate topredicate(root<tblcarton2rcardlog> root, criteriaquery<?> query,criteriabuilder cb) {          list<predicate> list = new arraylist<predicate>();          list.add(cb.equal(root.get("cartonno").as(string.class), cartonno));//某普通字段          list.add(cb.equal(root.get("id").get("rcard").as(string.class), rcard));//主键中某字段          list.add(cb.like(root.get("mocode").as(string.class), "%" + mocode + "%"));//like          list.add(cb.between(root.get("frozendate").as(long.class), frozendatestart, frozendateend));//between and          list.add(cb.greaterthanorequalto(root.get("id").get("rcard").as(string.class), rcardstart));//大于等于          list.add(root.get("id").get("lotno").as(string.class).in(lotnos));//in          //order by packdate desc,packtime desc          predicate[] p = new predicate[list.size()];          query.where(cb.and(list.toarray(p)));          query.orderby(cb.desc(root.get("packdate")),cb.desc(root.get("packtime")));          return query.getrestriction();      }  });  

      jpa specification复杂查询+排序

      刚使用spring-data-jpa,遇到不少难题,网上查了很多资料,发现讲jpa的不多,发个我刚做过的接口的过程吧。

      需求

      JPA Specification常用查询+排序实例

      看到图了吗?需要实现搜索以及各种字段的排序还要分页,还有可能有选择各种条件的下拉列表,是不是很变态?

      开始了

      一、dao

      需要先处理dao层,这里喜欢叫repository。做一个实体类的dao层接口,继承jpaspecificationexecutor,再写一个查询接口。

      JPA Specification常用查询+排序实例

      二、service

      JPA Specification常用查询+排序实例

      在这里主要处理的是查询条件,我这里是搜索功能的模糊查询,当然如果有更多的查询也可以添加进这里。这里需要注意的是specification。

      三、排序

      需要先建一个辅助的实体类,属性名我取和需要排序的实体类一样的免费精选名字大全,但是注意属性都是string类型的啊。后面细说,先上我建的辅助类。

        @data  public class deptsort {  private string id;//编码  private string name;//名称  private string highdeptname;//上级部门  private string principal;//负责人  private string depttype;//部门类型  private string enable;//启用  }

      字段都是需要排序的字段,这是为了好区分,叫别的也可以。

      下面是controller层,排序功能的具体实现。

        public responsemodel table(@requestparam("search")string search,  @requestparam("pagenumber")integer pagenumber,  @requestparam("pagesize")integer pagesize,  @requestbody deptsort deptsort){  responsemodel model = null;  try {  list<sort.order> orders = new arraylist<sort.order>();  if (stringutils.isnotblank(deptsort.getid())){  orders.add(new sort.order(sort.direction.fromstring(deptsort.getid()),"id"));  }  if (stringutils.isnotblank(deptsort.getname())){  orders.add(new sort.order(sort.direction.fromstring(deptsort.getname()),"name"));  }  if (stringutils.isnotblank(deptsort.gethighdeptname())){  orders.add(new sort.order(sort.direction.fromstring(deptsort.gethighdeptname()),"highdeptname"));  }  if (stringutils.isnotblank(deptsort.getprincipal())){  orders.add(new sort.order(sort.direction.fromstring(deptsort.getprincipal()),"principal"));  }  if (stringutils.isnotblank(deptsort.getdepttype())){  orders.add(new sort.order(sort.direction.fromstring(deptsort.getdepttype()),"depttype"));  }  if (stringutils.isnotblank(deptsort.getenable())){  orders.add(new sort.order(sort.direction.fromstring(deptsort.getenable()),"enable"));  }  //orders不能为空,所以如果为空设置按id排序[/code][code] if (orders.size() == 0){  orders.add(new sort.order(sort.direction.asc,"id"));  }  sort sort = new sort(orders);  pageable pageable = new pagerequest(pagenumber,pagesize,sort);  page<businessdept> all = service.findall(search, pageable);  model = responsemodel.getsuccessresponsemodel().setdata(all);  }catch (exception e){  e.printstacktrace();  model = responsemodel.getfailedresponsemodel();  }  return model;  }

      需要的参数有搜索内容search,还有deptsort辅助类。首先建立

        list<sort.order> orders = new arraylist<sort.order>();

      集合,然后if判断将参数加入集合。

      需要说明的是类似

        orders.add(new sort.order(sort.direction.fromstring(deptsort.getenable()),"enable"))

      语句,“enable”是需要查询的businessdept里的字段,不是辅助类的,当然这里我的辅助类和businessdept类一致,但是不一样的同学需要注意了。

      前端

      对于前端传递的参数有什么要求呢?

      JPA Specification常用查询+排序实例

      deptsort的各个属性的参数只能限定两种asc和desc,即升序和降序。上图的功能需求只需要传deptsort里的一个属性就可以了,这里传两个参数演示一下。

      查询成功的数据不展示了,给大家看一个后台的sql语句

        hibernate:   /* select  count(generatedalias0)   from  businessdept as generatedalias0   where  (  generatedalias0.name like :param0   )   and (  generatedalias0.deleteis=1   ) */ select  count(businessde0_.id) as col_0_0_   from  t_department businessde0_   where  (  businessde0_.name like ?  )   and businessde0_.delete_is=1  hibernate:   /* select  generatedalias0   from  businessdept as generatedalias0   where  (  generatedalias0.name like :param0   )   and (  generatedalias0.deleteis=1   )   order by  generatedalias0.depttype asc,  generatedalias0.enable desc */ select  businessde0_.id as id1_3_,  businessde0_.delete_is as delete_i2_3_,  businessde0_.dept_type as dept_typ3_3_,  businessde0_.enable as enable4_3_,  businessde0_.high_dept_id as high_dep5_3_,  businessde0_.high_dept_name as high_dep6_3_,  businessde0_.name as name7_3_,  businessde0_.principal as principa8_3_   from  t_department businessde0_   where  (  businessde0_.name like ?  )   and businessde0_.delete_is=1   order by  businessde0_.dept_type asc,  businessde0_.enable desc limit ?

      可以看到条件查询,升序、降序都有。

      结束语

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

      需要了解更多c/c++开发分享JPA Specification常用查询+排序实例,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/948582.html

      (0)
      上一篇 2021年11月19日
      下一篇 2021年11月19日

      精彩推荐