easyui分页时,总页数出错

宁静致远 2019-03-15 01:4539 阅读

MyBatis用easyui写后台分页代码时,出现翻页后显示总页数错误

可能原因在于后台mappers.xml里的sql语句错误

<select id="getProductTotal" parameterType="Map" resultType="Long">
  select count(*) from t_product
  <where>
    <if test="name!=null and name!=''">
      and name like #{name}
    </if>
  </where>
  <if test="start!=null and size!=null">
    limit #{start},#{size}
  </if>
</select>

去掉limit语句

<select id="getProductTotal" parameterType="Map" resultType="Long">
  select count(*) from t_product
  <where>
    <if test="name!=null and name!=''">
      and name like #{name}
    </if>
  </where>
</select>

controller的代码如下

@RequestMapping("/list")
public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,Product product,HttpServletResponse response)throws Exception{
  PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
  Map<String,Object> map = new HashMap<String,Object>();
  map.put("name", StringUtil.formatLike(product.getName()));
  map.put("start", pageBean.getStart());
  map.put("size", pageBean.getPageSize());
  List<Product> productList = productService.productList(map);
  Long total = productService.getProductTotal(map);
  JSONObject result=new JSONObject();
  JsonConfig jsonConfig=new JsonConfig();
  jsonConfig.setExcludes(new String[]{"orderProductList"});
  jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
  jsonConfig.registerJsonValueProcessor(ProductBigType.class,new ObjectJsonValueProcessor(new String[]{"id","name"}, ProductBigType.class));
  jsonConfig.registerJsonValueProcessor(ProductSmallType.class,new ObjectJsonValueProcessor(new String[]{"id","name"}, ProductSmallType.class));
  JSONArray jsonArray=JSONArray.fromObject(productList,jsonConfig);
  result.put("rows", jsonArray);
  result.put("total", total);
  ResponseUtil.write(response, result);
  return null;
}

因为前台easyui传入的数据start在变,变化前的sql语句为

SELECT COUNT(*) FROM t_product LIMIT 0,10

可以查询出总记录数
但start变化后

SELECT COUNT(*) FROM t_user LIMIT 10,10

无法查询出总记录数,所以导致出错!

上一篇: 面试题总结
下一篇: Java中的线程池
回复数量: 0
暂无评论~~
  请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!