分頁實現
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 //顯示一個行號列