分页实现
Colltroller层语句
Controller
@RequestMapping( value="findPage",method=RequestMethod.POST,produces="application/json;charset=utf-8")//produces设置字符集编码格式,method设置请求方式
@ResponseBody
public String findPage(int page,int rows){
Map map = new HashMap<>();
// 将查询出来的总数据存放到map中
map.put("total", userService.findAll().size());
int startIndex = (page - 1) * rows;
int Size = rows;
List<User> list = userService.findPage(startIndex, Size);
// 将使用分页查询出来的数据存放到map中
map.put("rows", list);
Gson gson = new Gson();
String json = gson.toJson(map);
return json;
}
//Mapper.xml中的Sql语句
<select id="listPage" resultType="com.bjsxt.pojo.User">
select id,username,pwd from t_user order by id limit #{page},#{rows}
</select>
前台代码
<script type="text/javascript">
/* 页面加载事件 */
$(function(){
/* 取得数据 */
$('#dg').datagrid({
url:'findPage',
columns:[[
/* field:实体类的属性名称 */
{field:'id',title:'用户id',width:100},
{field:'username',title:'用户名',width:100},
{field:'pwd',title:'密码',width:100,align:'right'}
]],
toolbar: '#tb',
singleSelect:true,
pagination:true, //在前台界面显示分页控件
rownumbers:true //显示一个行号列
});
})
</script>