您的位置:首页 > 编程语言 > Java开发

JAVAWeb分页中查询功能的实现

2020-04-01 19:04 866 查看

个人博客https://www.nfreak-man.cn
实现查询数据分页显示,需要在分页显示功能的基础上,获取用户输入值,并进行 sql 语句的拼接,并且不影响分页数据的正常获取。分页功能的编写在上一篇博客。

Servlet改写

将获取的输入放入 map 集合 condition 中,并调用service查询返回数据集合,然后输入值要通过 request 传回 list.jsp 进行数据回显。

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
//获取参数
String currentPage = request.getParameter("currentPage");//当前页码
String rows = request.getParameter("rows");//每页显示条数
if(currentPage == null || "".equals(currentPage)){
currentPage = "1";
}
if(rows == null || "".equals(rows)){
rows = "5";
}

//获取条件查询参数
Map<String, String[]> codition = request.getParameterMap();

//调用service查询
UserSvice service = new UserServiceImpl();
PageBean<User> pb = service.finUserByPage(currentPage,rows,codition);

//将PageBean存入request
request.setAttribute("pb",pb);
request.setAttribute("condition",codition);//将查询条件存入request回显
//转发到list.jsp
request.getRequestDispatcher("/list.jsp").forward(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

Service 改写

调用 dao 查询总记录数增加参数 condition ,如果 condition 有值则为搜索状态,获得符合查询条件的总记录数,没有搜索时为空,获得总记录数。

@Override
public PageBean<User> finUserByPage(String _currentPage, String _rows, Map<String, String[]> codition) {
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
if(currentPage <= 0){
currentPage = 1;
}
//创建空的PageBean对象
PageBean<User> pb = new PageBean<User>();
//设置参数
pb.setCurrentPage(currentPage);
pb.setRows(rows);
//调用dao查询总记录数
int totalCount = dao.findTotalCount(codition);
pb.setTotalCount(totalCount);
//调用dao查询list集合
//计算开始的记录的索引
int start = (currentPage - 1) * rows;
List<User> list = dao.findByPage(start,rows,codition);
pb.setList(list);
//计算总页码
int totalPage = totalCount % rows == 0 ? totalCount/rows : totalCount/rows + 1;
pb.setTotalPage(totalPage);
return pb;
}

Dao方法改写

改写原 sql 语句为了拼接后续条件语句,添加 where 1 = 1 。当 condition 中的值不为空时,进行 sql 语句拼接。

@Override
public List<User> findByPage(int start, int rows, Map<String, String[]> codition) {
String sql = "select * from user where 1 = 1";
StringBuilder sb = new StringBuilder(sql);
//遍历map
Set<String> Keyset = codition.keySet();
//定义参数集合
List<Object> params = new ArrayList<Object>();
for (String key : Keyset) {
//排除分页条件参数
if ("currentPage".equals(key) || "rows".equals(key)){
continue;
}
//获取value
String value = codition.get(key)[0];
//判断value是否有值
if (value != null && !"".equals(value)){
//有值
sb.append(" and "+key+" like ? ");
params.add("%"+value+"%");//?条件的值
}
}
//添加分页查询
sb.append(" limit ?,? ");
//添加分页查询参数
params.add(start);
params.add(rows);

return template.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray());
}

前端页面改写

查询框:

<form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post">
<div class="form-group">
<label for="exampleInputName2">姓名</label>
<input type="text" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" >
</div>
<div class="form-group">
<label for="exampleInputName3">籍贯</label>
<input type="text" name="address" value="${condition.address[0]}" class="form-control" id="exampleInputName3" >
</div>
<div class="form-group">
<label for="exampleInputEmail2">邮箱</label>
<input type="text" name="email" value="${condition.email[0]}" class="form-control" id="exampleInputEmail2" >
</div>
<button type="submit" class="btn btn-default">查询</button>
</form>

分页框:

须在页码、上一页、下一页的点击链接上添加用户输入的值,然后才能在查询出的页面进行正常跳转。

<div>
<nav aria-label="Page navigation">
<ul class="pagination">
<c:if test="${pb.currentPage == 1}"><li class="disabled"></c:if>
<c:if test="${pb.currentPage != 1}"><li></c:if>
<a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage - 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<c:forEach begin="1" end="${pb.totalPage}" var="i">
<c:if test="${pb.currentPage == i}">
<li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
</c:if>
<c:if test="${pb.currentPage != i}">
<li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
</c:if>
</c:forEach>
<c:if test="${pb.currentPage == pb.totalPage}"><li class="disabled"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li></c:if>
<c:if test="${pb.currentPage < pb.totalPage}"><li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</c:if>

<span style="font-size: 25px;margin-left: 5px">
共${pb.totalCount}条记录,共${pb.totalPage}页
</span>
</ul>
</nav>
</div>
  • 点赞
  • 收藏
  • 分享
  • 文章举报
William_GJIN 发布了21 篇原创文章 · 获赞 0 · 访问量 452 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: