easyui分页时,总页数出错

本文解决了一个在使用MyBatis和EasyUI进行后台分页时遇到的问题,即翻页后总页数显示错误。问题根源在于带有LIMIT子句的SQL计数语句在分页参数变化时无法正确计算总记录数。通过调整SQL语句,移除LIMIT,确保了总记录数的准确计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值