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>新闻热点
疑难解答