user_list.jsp
<%@ page import="com.ssm.entity.User" %> <%@ page pageEncoding="UTF-8" import="java.util.List" %> <html> <head> <%@ include file="meta.jsp" %> <meta charset="UTF-8"> <title>用戶列表</title> <link rel="stylesheet" type="text/css" href="jquery-easyui-1.4/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="jquery-easyui-1.4/themes/icon.css"> <script type="text/javascript" src="jquery-easyui-1.4/jquery.min.js"></script> <script type="text/javascript" src="jquery-easyui-1.4/jquery.easyui.min.js"></script> </head> <body> <table id="dataTable" style="width:400px;height:250px"> <thead> <tr> <th data-options="field:'id'">ID</th> <th data-options="field:'name'">姓名</th> <th data-options="field:'age'">年齡</th> </tr> </thead> </table> <a href="/ssm">回到首頁</a> </body> <script> $(document).ready(function() { page_init(); }); function page_init() { $("#dataTable").datagrid({ url: "listUserWithPageForJson", //actionName queryParams:{pageNumber:2,pageSize:10},//查詢參數 loadMsg:"正在加載數據...", rownumbers:true,//查詢結果在表格中顯示行號 fitColumns:true,//列的寬度填滿表格,防止下方出現滾動條。 pageNumber:2, //初始頁碼,得在這設置才效果,pagination設置沒效果。 pagination: true//分頁控件 //如果后端返回的json的格式直接是data={total:xx,rows:{xx}},不需要設置loadFilter了, //如果有多層封裝,比如data.jsonMap = {total:xx,rows:{xx}},則需要在loadFilter處理一下。 /* loadFilter: function(data){ if(data.jsonMap) { return data.jsonMap; } }*/ }); var p = $('#dataTable').datagrid('getPager'); $(p).pagination({ pageSize: 10,//每頁顯示的記錄條數,默認為10 pageList: [5,10,15,20],//可以設置每頁記錄條數的列表 beforePageText: '第',//頁數文本框前顯示的漢字 afterPageText: '頁 共 {pages} 頁', displayMsg: '共 {total} 條記錄', onSelectPage: function (pageNumber, pageSize) {//分頁觸發 find(pageNumber, pageSize); } }); } function find(pageNumber, pageSize) { $("#dataTable").datagrid('getPager').pagination({pageSize : pageSize, pageNumber : pageNumber});//重置 $("#dataTable").datagrid("loading"); //加屏蔽 $.ajax({ type : "POST", dataType : "json", url : "listUserWithPageForJson", data : { pageNumber : pageNumber, pageSize : pageSize }, success : function(data) { $("#dataTable").datagrid('loadData',data); $("#dataTable").datagrid("loaded"); //移除屏蔽 }, error : function(err) { $.messager.alert('操作提示', '獲取信息失敗...請聯系管理員!', 'error'); $("#dataTable").datagrid("loaded"); //移除屏蔽 } }); } </script> </html>
UserController.java
package com.ssm.controller; import com.alibaba.fastjson.JSONObject; import com.ssm.common.PageModel; import com.ssm.dao.UserDao; import com.ssm.entity.JsonEntity; import com.ssm.entity.User; import com.ssm.service.UserService; import com.ssm.tools.BlankUtil; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by Administrator on 2015-10-15. */ @Controller public class UserController { @Resource private UserService userService; @RequestMapping(value = "listUserWithPageForJson") @ResponseBody public Object listUserWithPageForJson(PageModel pageModel,User user) { Object jsonObject = null; try { pageModel.setQueryObj(user); userService.listUserWithPage(pageModel); jsonObject = JSONObject.toJSON(pageModel); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } }
PageModel.java
package com.ssm.common; import java.util.List; /** * Created by hbd on 2015/10/22. */ public class PageModel<T> { private Integer pageNumber; //當前頁數 private Integer pageSize; //一頁顯示數量 private Integer startRow; //查詢起始行 private Integer total; //總記錄行數 private List<T> rows; //查詢結果數據 private T queryObj; //查詢對象 public Integer getStartRow() { if(pageNumber!=null && pageSize!=null) { return (pageNumber - 1) * pageSize; } else { return 0; } } public Integer getPageNumber() { return pageNumber; } public void setPageNumber(Integer pageNumber) { this.pageNumber = pageNumber; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } public void setQueryObj(T queryObj) { this.queryObj = queryObj; } public T getQueryObj() { return queryObj; } }
UserServiceImpl.java
package com.ssm.service.impl; import com.ssm.common.PageModel; import com.ssm.dao.UserDao; import com.ssm.entity.User; import com.ssm.service.UserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * Created by Administrator on 2015-10-16. */ @Service("userService") public class UserServiceImpl implements UserService { @Resource private UserDao userDao; @Override public User getUserById(int userId) { return userDao.findUserById(userId); } @Override public void listUserWithPage(PageModel<User> pageModel) { pageModel.setRows(userDao.selectUserListWithPage(pageModel)); pageModel.setTotal(userDao.selectUserCountWithPage(pageModel)); } }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--這塊等於dao接口的實現 namespace必須和接口的類路徑一樣--> <mapper namespace="com.ssm.dao.UserDao"> <sql id="sql_count"> SELECT COUNT(*) </sql> <sql id="sql_select"> SELECT * </sql> <sql id="sql_where"> FROM USER <where> <if test="queryObj != null"> <if test="queryObj.name != null and queryObj.name != ''"> AND name like CONCAT('%',#{queryObj.name},'%') </if> <if test="queryObj.age != null"> AND age = #{queryObj.age} </if> </if> </where> </sql> <select id="selectUserListWithPage" parameterType="com.ssm.common.PageModel" resultType="User"> <include refid="sql_select"></include> <include refid="sql_where"></include> <if test="pageNumber != null and pageSize!= null"> limit #{startRow},#{pageSize} </if> </select> <select id="selectUserCountWithPage" parameterType="com.ssm.common.PageModel" resultType="Integer"> <include refid="sql_count"></include> <include refid="sql_where"></include> </select> </mapper>
