easyui分页时,总页数出错

时间:2022-07-25 05:46:20

错误出现

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

easyui分页时,总页数出错

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

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