实现用户管理中的查询功能并将之呈现
系统的用户管理功能包含用户查询、用户添加、用户删除与用户修改等功能。
先贴上该方法代码:
@RequestMapping(value="/selectUser")
public String selectUser(
@ModelAttribute User user,Model model,HttpServletRequest request){
System.out.println(user.getLoginName());
System.out.println(user.getPassword());
HttpSession session=request.getSession();
int pageIndexNum;
if(request.getParameter("pageIndex")==null){
pageIndexNum=1;
}
else{
String pageIndex=request.getParameter("pageIndex");
pageIndexNum=Integer.parseInt(pageIndex);
System.out.println("pageIndex="+pageIndex);
}
PageModel pageModel=new PageModel(pageIndexNum);
//if(pageIndex!=null){
//pageModel.setPageIndex(1);
//}
/*查询用户信息*/
List<User> users=hrmService.selectUser(user, pageModel);
if(users.isEmpty()){
System.out.println("empty");
}
int index=0;
for(int size=users.size();index<size;index++){
System.out.println(users.get(index).getUserName());
System.out.println(users.get(index).getLoginName());
}
model.addAttribute("users", users);
model.addAttribute("pageModel",pageModel);
session.setAttribute("userForPage",user);
System.out.println(pageModel.getPageSize());
System.out.println(pageModel.getPageIndex());
System.out.println(pageModel.getRecordCount());
System.out.println(pageModel.getTotalSize());
return "hrm.view.front/front_view/hrm.view.user/user_select";
}
先解释其中PageModel类的作用,代码如下:
package org.fkit.hrm.util.tag;
public class PageModel {
//记录总数
int recordCount;
//当前页面
int pageIndex;
//每页尺寸
int pageSize=4;
//总页数
int totalSize;
//数据库记录查询位置
public int firstLimitParam;
//构造函数
public PageModel(){
}
public PageModel(int pageIndex){
this.pageIndex=pageIndex;
}
//获取总记录数以及设置总记录数
public int getRecordCount() {
this.recordCount=this.recordCount<=0?0:this.recordCount;
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
//获取请求页以及设置请求页
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
//更新pageIndex
public void updatePageIndex(){
this.pageIndex=this.pageIndex<=0?0:this.pageIndex;
this.pageIndex=this.pageIndex>=this.getTotalSize()?this.getTotalSize():this.pageIndex;
}
//获取页面尺寸以及设置页面尺寸
public int getPageSize() {
this.pageSize=this.pageSize<=4?4:this.pageSize;
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
//获取总页数以及设置总页数
public int getTotalSize() {
if(this.getRecordCount()<=0){
this.totalSize=0;
}
else{
totalSize=(this.getRecordCount()-1)/this.getPageSize()+1;
}
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
//获取查询的记录位置
public void setFirstLimitParam(){
this.firstLimitParam=(this.getPageIndex()-1)*this.pageSize;
}
}
该类是为了实现分页查询而创建的,包含pageSize,pageIndex,totalSize,recordCount,firstLimitParam,这几个属性之间互相关联,在实现分页查询的功能时,首先需要知道每一页展示的记录条数pageSize,通过用户输入的查询条件可查询到满足条件的记录总数recordCount,知道了recordCount与pageSize可以计算出总页数totalPage,firstLimitParam是用户请求某一页是在数据库中记录所在的查询位置,结合url中包含的用户需访问的当前页面号pageIndex可算出firstLimitParam,这时即可调用相关DAO层方法查询到请求页面的Users了,再将users发送到前端页面即可呈现查询数据。下面结合具体代码进行讲解。
在查询页面用户输入查询信息点击查询按钮,发送请求selectUser,调用控制器 UserController方法selectUser()处理用户查询的操作,@ModelController注解将用户动态查询信息(用户名、用户状态)统一封装到User类。因为是首次查询,并非页面跳转此时pageIndex默认为1,并实例化一个PageModel对象
PageModel pageModel=new PageModel(pageIndexNum);
pageModel对象中pageSize大小默认为4。
接下来调用hrmService接口方法selectUser(user, pageModel),该方法代码如下:
public List<User> selectUser(User user,PageModel pageModel){
Map<String,Object> params=new HashMap<String,Object>();
params.put("user", user);
int recordCount=userDao.count(params);
System.out.println("调用HrmServiceImpl类方法selecctAllUser -->");
System.out.println(pageModel.getPageIndex());
System.out.println(pageModel.firstLimitParam);
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
if(recordCount>0){
/**开始分页查询数据:查询第几页的数据**/
params.put("pageModel", pageModel);
}
List<User> users=userDao.selectByPage(params);
return users;
}
实例化一个Map对象,先保存user对象,接着调用DAO组件方法count()查询满足条件的user总个数recordCount.
int recordCount=userDao.count(params);
此时pageModel中已知pageSize,pageIndex,recordCount,即可算出totalPage以及记录查询位置firstLimitParam
//设置记录总数,从而可以计算出totalSize,继而计算出实际的pageIndex
pageModel.setRecordCount(recordCount);
//更新pageIndex
pageModel.updatePageIndex();
//更新记录查询位置
pageModel.setFirstLimitParam();
接着就是将pageModel保存到Map中,调用下列方法进行查询获得User集合
if(recordCount>0){
/**开始分页查询数据:查询第几页的数据**/
params.put("pageModel", pageModel);
}
List<User> users=userDao.selectByPage(params);
再讲讲DAO组件中count()方法与selectByPage()方法的不同,前者是为了得出满足查询条件的User总数recordCount,此时参数Map中仅包含一个User对象;而selectByPage()方法是为了查询数据库表中指定位置开始pageSize个记录,因而Map不仅包含Usr对象也包含一个PageModel对象,就是为了提取firstLimitParam。