组合查询--表单对象转化为json对象

//1.将page和rows封装到pageable中
  Pageable pageable = new PageRequest(page, rows);
  //2.创建组合条件查询条件对象
  Specification<FixedArea> spec = new Specification<FixedArea>() {

   @Override
   public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    //2.1创建存放已拼装条件的list集合
    ArrayList<Predicate> predicates = new ArrayList<Predicate>();
    
    //2.2进行组合查询条件的拼装
    String id = model.getId();
    if(StringUtils.isNotBlank(id)){
     
     predicates.add(cb.equal(root.get("id").as(String.class), id));
     
    }
    String company = model.getCompany();
    if(StringUtils.isNotBlank(company)){
     
     predicates.add(cb.like(root.get("company").as(String.class), "%"+company+"&"));
     
    }
    
    //2.3创建存放拼装条件的predicate数组
    Predicate[] predicateArr = new Predicate[predicates.size()];

    //2.4返回拼装好的条件
    return query.where(predicates.toArray(predicateArr)).getRestriction();
   }
  };
  
  //3.执行分页查询
  Page<FixedArea> page = fixedAreaService.pageQuery(spec,pageable);
  
  //4.去除会造成no session 的 FixedArea实体类中的"many"字段
  String[] excludes = { "subareas", "couriers" };
  
  //调用baseAction中的方法将查询到的结果转化成json发送到前台
  this.write2JsonObject(page, excludes);

-------------------------------------------------------------------------------------------------------------

baseAction中:

public void write2JsonObject(Page<?> page, String[] excludes) throws IOException {

  // 构建json对象型数据
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("total", page.getTotalElements());
  map.put("rows", page.getContent());

  // JsonConfig: 配置转换的json数据中不需要的属性
  JsonConfig jsonConfig = new JsonConfig();

  jsonConfig.setExcludes(excludes);

  // 将map转化为json
  String json = JSONObject.fromObject(map, jsonConfig).toString();
  ServletActionContext.getResponse().setContentType("test/json;charset=UTF-8");
  ServletActionContext.getResponse().getWriter().print(json);

 }

转载于:https://www.cnblogs.com/xubing520/p/7280709.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值