package cn.itcast.domain;
import java.util.List;
public class PageBean<T> {
private Integer currentPage; //当前页
private Integer totalCount; //总记录数
private Integer totalPage; //总页数
private List<T> list; //分页显的数据
private Integer rows; //每页记录数
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
}
/**
* 分页查询数据
* @param start 起始位置
* @param rows 每页条数
* @return
*/
public List<User> findByPage(int start,int rows);
/**
* 查询总记录数
* @return
*/
public int findTotalCount();
@Override
public List<User> findByPage(int start, int rows) {
String sql = "select * from user limit ?,?";
List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), start, rows);
return list;
}
@Override
public int findTotalCount() {
String sql = "select count(*) from user";
int totalCount=0;
try {
totalCount = template.queryForObject(sql, Integer.class);
}catch(Exception ex){
}
return totalCount;
}
/**
* 分页查询
* @param currentPage 当前页
* @param rows 每页记录数
* @return
*/
PageBean<User> findByPage(int currentPage, int rows);
@Override
public PageBean<User> findByPage(int currentPage, int rows) {
//查询总记录数
int totalCount = userDao.findTotalCount();
//查询总页数
int totalPage=totalCount%rows==0?totalCount/rows:totalCount/rows+1;
PageBean<User> pb = new PageBean<>();
//判断如果前台传过来的当前页小于1,则让当前页重置为1
if(currentPage<1){
currentPage=1;
}
//判断如果前台传过来的当前页大于总页数,则让当前页为总页数
if(currentPage>totalPage){
currentPage=totalPage;
}
//查询分页内容
int start= (currentPage-1)*rows;
List<User> list =userDao.findByPage(start,rows);
pb.setList(list);
pb.setCurrentPage(currentPage);
pb.setRows(rows);
pb.setTotalCount(totalCount);
pb.setTotalPage(totalPage);
return pb;
}
@WebServlet(value = "/UserPageServlet")
public class UserPageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收前台传过来的当前页和每页显示记录数
String _currentPage = request.getParameter("currentPage");
String _rows = request.getParameter("rows");
//如果用户没有传当前页和每页显示记录数,默认值就给1和4
int currentPage=1;
if(_currentPage!=null&&!_currentPage.equals("")){
currentPage=Integer.parseInt(_currentPage);
}
int rows=4;
if(_rows!=null&&!_rows.equals("")){
rows=Integer.parseInt(_rows);
}
UserService userService = new UserServiceImpl();
PageBean<User> pb = userService.findByPage(currentPage, rows);
//将分页对象(PageBean)传到list.jsp
request.setAttribute("pb",pb);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
欢迎您!${sessionScope.user.name}
查询所有用户信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
用户信息管理系统
List findByPage(int start,int rows,User user);
int findTotalCount(User user);
只需要在两个方法里面加上User user参数。
@Override
public List<User> findByPage(int start, int rows,User user) {
StringBuilder sb = new StringBuilder("select * from user where 1=1");
//为“?”封装参数值
List params = new ArrayList();
if(user.getName()!=null&&!user.getName().equals("")){
sb.append( " and name like ?");
params.add("%"+user.getName()+"%");
}
if(user.getAddress()!=null&&!user.getAddress().equals("")){
sb.append( " and address like ?");
params.add("%"+user.getAddress()+"%");
}
if(user.getEmail()!=null&& !user.getEmail().equals("")){
sb.append(" and email like ?");
params.add("%"+user.getEmail()+"%");
}
sb.append(" limit ?,?");
params.add(start);
params.add(rows);
List<User> list = template.query(sb.toString(), new BeanPropertyRowMapper<>(User.class), params.toArray());
return list;
}
@Override
public int findTotalCount(User user) {
StringBuilder sb = new StringBuilder("select count(*) from user where 1=1");
List params = new ArrayList();
if(user.getName()!=null&&!user.getName().equals("")){
sb.append( " and name like ?");
params.add("%"+user.getName()+"%");
}
if(user.getAddress()!=null&&!user.getAddress().equals("")){
sb.append( " and address like ?");
params.add("%"+user.getAddress()+"%");
}
if(user.getEmail()!=null&& !user.getEmail().equals("")){
sb.append(" and email like ?");
params.add("%"+user.getEmail()+"%");
}
int totalCount=0;
try {
totalCount = template.queryForObject(sb.toString(), Integer.class,params.toArray());
}catch(Exception ex){
}
return totalCount;
}
参数中加上User user就行。
需要接受前台查询框中输入的内容,封装进user对象中传给后台。
//处理请求乱码
request.setCharacterEncoding("utf-8");
//接收查询条件
Map<String, String[]> parameterMap = request.getParameterMap();
//将查询条件封装到User对象中
User user = new User();
try {
BeanUtils.populate(user,parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
list页面中超链的位置需要回显查询输入的内容,所有分页链接的位置都要加上。
<li ><a href="${pageContext.request.contextPath}/UserPageServlet?currentPage=${i}&rows=4&name=${user.name}&address=${user.address}&email=${user.email}">${i}</a></li>
大体功能基本上已经完成了,一个小小的案例就做了几天了,难受啊,接下来再学习几天做一个网页javaweb阶段的学习就结束了。