1.dao層聲明查詢以及分頁方法
public interface UserDao { /** * 查詢用戶集合 * @param map * @return */ public List<User> find(Map<String,Object> map); /** * 獲取總記錄數 * @param map * @return */ public Long getTotal(Map<String,Object> map);}2.service層復制dao層的方法
public interface UserService { /** * 查詢用戶集合 * @param map * @return */ public List<User> find(Map<String,Object> map); /** * 獲取總記錄數 * @param map * @return */ public Long getTotal(Map<String,Object> map);}3.service層的實現類
@Service("userService")public class UserServiceImpl implements UserService{ @Resource PRivate UserDao userDao; @Override public List<User> find(Map<String, Object> map) { return userDao.find(map); } @Override public Long getTotal(Map<String, Object> map) { return userDao.getTotal(map); }}4.Controller層
@Controller@RequestMapping("/user")public class UserController { @Resource private UserService userService; /** * 分頁條件查詢用戶 * @param page * @param rows * @param s_user * @param response * @return * @throws Exception */ @RequestMapping("/list") public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,User s_user,HttpServletResponse response)throws Exception{ PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows)); Map<String,Object> map=new HashMap<String,Object>(); map.put("userName", StringUtil.formatLike(s_user.getUserName())); map.put("start", pageBean.getStart()); map.put("size", pageBean.getPageSize()); List<User> userList=userService.find(map); Long total=userService.getTotal(map); JSONObject result=new JSONObject(); JSONArray jsonArray=JSONArray.fromObject(userList); result.put("rows", jsonArray); result.put("total", total); ResponseUtil.write(response, result); return null; }}5.UserMapper.xml
<select id="find" parameterType="Map" resultMap="UserResult"> select * from t_user <where> <if test="userName!=null and userName!='' "> and userName like #{userName} </if> </where> <if test="start!=null and size!=null"> limit #{start},#{size} </if> </select> <select id="getTotal" parameterType="Map" resultType="Long"> select count(*) from t_user <where> <if test="userName!=null and userName!='' "> and userName like #{userName} </if> </where> </select>6.添加一個分頁實體類PageBean
public class PageBean { private int page; // 第幾頁 private int pageSize; // 每頁記錄數 private int start; // 起始頁 public PageBean(int page, int pageSize) { super(); this.page = page; this.pageSize = pageSize; } //省去get/set方法}7.添加一個工具包,聲明2個工具類
7.1ResponseUtil
public class ResponseUtil { public static void write(HttpServletResponse response,Object o)throws Exception{ response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); out.println(o.toString()); out.flush(); out.close(); }}7.2StringUtil
public class StringUtil { /** * 判斷是否是空 * @param str * @return */ public static boolean isEmpty(String str){ if(str==null||"".equals(str.trim())){ return true; }else{ return false; } } /** * 判斷是否不是空 * @param str * @return */ public static boolean isNotEmpty(String str){ if((str!=null)&&!"".equals(str.trim())){ return true; }else{ return false; } } /** * 格式化模糊查詢 * @param str * @return */ public static String formatLike(String str){ if(isNotEmpty(str)){ return "%"+str+"%"; }else{ return null; } }}8.創建一個page文件夾,存放userManage.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--省去部分js,CSS引入代碼,看著累--><script type="text/Javascript"> function searchUser(){ $("#dg").datagrid('load',{ "userName":$("#s_userName").val() }); }</script><title>Insert title here</title></head><body style="margin: 1px"> <table id="dg" title="用戶管理" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true" url="${pageContext.request.contextPath}/user/list.do" fit="true" toolbar="#tb"> <thead> <tr> <th field="cb" checkbox="true" align="center"></th> <th field="id" width="50" align="center">編號</th> <th field="userName" width="50" align="center">用戶名</th> <th field="passWord" width="50" align="center">密碼</th> <th field="trueName" width="50" align="center">真實姓名</th> <th field="email" width="50" align="center">郵件</th> <th field="phone" width="50" align="center">聯系電話</th> <th field="roleName" width="50" align="center">角色</th> </tr> </thead> </table> <div id="tb"> <div> <a href="javascript:openUserAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a> <a href="javascript:openUserModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a> <a href="javascript:deleteUser()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">刪除</a> </div> <div> 用戶名: <input type="text" id="s_userName" size="20" onkeydown="if(event.keyCode==13) searchUser()"/> <a href="javascript:searchUser()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a> </div> </div></body></html>新聞熱點
疑難解答