实体类
PageBean.java
package domain;
import java.util.List;
public class PageBean<T> {
private int totalCount;//总记录数
private int totalPage;//总页数
private List<T> list;//每页的数据
private int currentPage;//当前页码
private int rows;//每页的记录数
public PageBean() {
}
@Override
public String toString() {
return "PageBean{" +
"totalCount=" + totalCount +
", totalPage=" + totalPage +
", list=" + list +
", currentPage=" + currentPage +
", rows=" + rows +
'}';
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
}
Student .java
package domain;
public class Student {
Integer id;
String name;
String sex;
Integer chinese;
Integer math;
Integer english;
public Student() {
}
public Student(Integer id, String name, String sex, Integer chinese, Integer math, Integer english) {
this.id = id;
this.name = name;
this.sex = sex;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getChinese() {
return chinese;
}
public void setChinese(Integer chinese) {
this.chinese = chinese;
}
public Integer getMath() {
return math;
}
public void setMath(Integer math) {
this.math = math;
}
public Integer getEnglish() {
return english;
}
public void setEnglish(Integer english) {
this.english = english;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
}
Pagesevlet.java
package web;
import domain.PageBean;
import domain.Student;
import server.Impl.StudentServerImpl;
import server.StudentServer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
@WebServlet("/pageServlet")
public class PageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(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";
} else if ("0".equals(currentPage)) {
currentPage = "1";
}
if (rows == null || "".equals(rows)) {
rows = "5";
}
Map<String, String[]> map = request.getParameterMap();
Set<String> set=map.keySet();
for (String o : set) {
System.out.println("key值:" +o);
}
StudentServer service = new StudentServerImpl();
PageBean<Student> pageBean = service.getPageBean(currentPage, rows,map);
List<Student> list= pageBean.getList();
for (Student student : list) {
System.out.println("student:"+student);
}
request.setAttribute("pb",pageBean);
request.setAttribute("map",map);
request.getRequestDispatcher("member-list.jsp").forward(request, response);
}
}
StudentServer .java
package server;
import domain.PageBean;
import domain.Student;
import java.util.Map;
public interface StudentServer {
PageBean<Student> getPageBean(String _currentPage, String _rows, Map<String, String[]> map);
}
StudentServerImpl .java
package server.Impl;
import dao.Impl.StudentDaoImpl;
import dao.StudentDao;
import domain.PageBean;
import domain.Student;
import server.StudentServer;
import java.util.List;
import java.util.Map;
public class StudentServerImpl implements StudentServer {
StudentDao dao = new StudentDaoImpl();
@Override
public PageBean<Student> getPageBean(String _currentPage, String _rows, Map<String, String[]> map) {
PageBean<Student> pageBean = new PageBean<>();
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
pageBean.setCurrentPage(currentPage);
pageBean.setRows(rows);
int totalCount = dao.getTotalCount(map);
pageBean.setTotalCount(totalCount);
int totalPage = totalCount % rows == 0 ? totalCount / rows : (totalCount / rows) + 1;
pageBean.setTotalPage(totalPage);
int start = (currentPage - 1) * rows;
if (currentPage > totalPage) {
start = (totalPage - 1) * rows;
pageBean.setCurrentPage(totalPage);
}
List<Student> list = dao.getStudentList(start, rows,map);
pageBean.setList(list);
return pageBean;
}
}
dao类
StudentDao .java
package dao;
import domain.Student;
import java.util.List;
import java.util.Map;
public interface StudentDao {
int getTotalCount(Map<String, String[]> map);
List<Student> getStudentList(int start, int rows, Map<String, String[]> map);
}
package dao.Impl;
import dao.StudentDao;
import domain.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import untils.JDBCUntils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUntils.getDs());
@Override
public int getTotalCount(Map<String, String[]> map) {
String sql = "Select count(*) from sc where 1 = 1";
StringBuilder sb = new StringBuilder(sql);
Set<String> set = map.keySet();
ArrayList<Object> list = new ArrayList<>();
for (String key : set) {
if ("currentPage".equals(key) || "rows".equals(key)) {
continue;
}
String value = map.get(key)[0];
if (value == null && "".equals(value)) {
sb.append(" and " + key + " like ? ");
list.add("%" + value + "%");
}
}
sql = sb.toString();
System.out.println(sql);
System.out.println(list);
return jdbcTemplate.queryForObject(sql, Integer.class, list.toArray());
}
@Override
public List<Student> getStudentList(int start, int rows, Map<String, String[]> map) {
String sql = "select * from sc where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
Set<String> set = map.keySet();
List<Object> list = new ArrayList<>();
for (String key : set) {
if ("currentPage".equals(key) || "rows".equals(key)) {
continue;
}
String value = map.get(key)[0];
if (value != null && !"".equals(value)) {
sb.append(" and " + key + " like ? ");
list.add("%" + value + "%");
}
}
sb.append("limit ?,?");
list.add(start);
list.add(rows);
sql = sb.toString();
System.out.println(sql);
System.out.println(list);
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class),list.toArray());
}
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
<meta charset="UTF-8">
<title>成绩列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="stylesheet" href="./css/font.css">
<link rel="stylesheet" href="./css/xadmin.css">
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="./js/xadmin.js"></script>
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="x-nav">
<span class="layui-breadcrumb">
<a href="">首页</a>
<a><cite>导航元素</cite></a>
</span>
<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
<div class="layui-row layui-col-space15">
<div class="layui-col-md12">
<div class="layui-card">
<div class="layui-card-body ">
<form class="layui-form layui-col-space5">
<form action="/pageServlet" method="post">
<div class="layui-inline layui-show-xs-block">
<input type="text" name="name" value="${map.name[0]}" placeholder="请输入用户名" autocomplete="off" class="layui-input">
</div>
<div class="layui-inline layui-show-xs-block">
<button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon"></i></button>
</div>
</form>
</form>
</div>
<div class="layui-card-header">
<%--<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>--%>
<button class="layui-btn" onclick="xadmin.open('添加用户','./member-add.jsp',600,400)"><i class="layui-icon"></i>添加</button>
</div>
<div class="layui-card-body layui-table-body layui-table-main">
<table class="layui-table layui-form">
<thead>
<tr>
<th>
<input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>语文</th>
<th>数学</th>
<th>英语</th>
<th>操作</th></tr>
</thead>
<tbody>
<c:forEach items="${pb.list}" var="member" varStatus="s">
<tr>
<td><input type="checkbox" name="uid" value="${member.id}"></td>
<td>${member.id}</td>
<td>${member.name}</td>
<td>${member.sex}</td>
<td>${member.chinese}</td>
<td>${member.math}</td>
<td>${member.english}</td>
<td>
<a title="编辑" onclick="xadmin.open('编辑','admin-edit.html')" href="${pageContext.request.contextPath}/findServlet?id=${member.id}">
<i class="layui-icon"></i>
</a>
<a title="删除" onclick="member_del(this,'要删除的id')" href="${pageContext.request.contextPath}/delServlet?id=${member.id}">
<i class="layui-icon"></i>
</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="layui-card-body ">
<div class="page">
<div>
<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}/pageServlet?currentPage=1&rows=5&username=${map.name[0]}&nationality=${map.nationality[0]}">
<span aria-hidden="true">首页</span>
</a>
<c:if test="${pb.currentPage == 1}">
</c:if>
<c:if test="${pb.currentPage != 1}">
</c:if>
<a href="${pageContext.request.contextPath}/pageServlet?currentPage=${pb.currentPage - 1}&rows=5&username=${map.name[0]}&nationality=${map.nationality[0]}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
<c:forEach begin="1" end="${pb.totalPage}" var="i" >
<c:if test="${pb.currentPage == i}">
<a href="${pageContext.request.contextPath}/pageServlet?currentPage=${i}&rows=5&username=${map.name[0]}&nationality=${map.nationality[0]}">${i}</a>
</c:if>
<c:if test="${pb.currentPage != i}">
<a href="${pageContext.request.contextPath}/pageServlet?currentPage=${i}&rows=5&username=${map.name[0]}&nationality=${map.nationality[0]}">${i}</a>
</c:if>
</c:forEach>
<c:if test="${pb.currentPage == pb.totalPage}">
</c:if>
<c:if test="${pb.currentPage != pb.totalPage}">
</c:if>
<a href="${pageContext.request.contextPath}/pageServlet?currentPage=${pb.currentPage + 1}&rows=5&username=${map.name[0]}&nationality=${map.nationality[0]}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
<c:if test="${pb.currentPage == pb.totalPage}">
</c:if>
<c:if test="${pb.currentPage != pb.totalPage}">
</c:if>
<a href="${pageContext.request.contextPath}/pageServlet?currentPage=${pb.totalPage}&rows=5&username=${map.name[0]}&nationality=${map.nationality[0]}" aria-label="Next">
<span aria-hidden="true">尾页</span>
</a>
<span style="font-size: 15px;">
共${pb.totalCount}条记录,共${pb.totalPage}页
</span>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
layui.use(['laydate','form'], function(){
var laydate = layui.laydate;
var form = layui.form;
// 监听全选
form.on('checkbox(checkall)', function(data){
if(data.elem.checked){
$('tbody input').prop('checked',true);
}else{
$('tbody input').prop('checked',false);
}
form.render('checkbox');
});
//执行一个laydate实例
laydate.render({
elem: '#start' //指定元素
});
//执行一个laydate实例
laydate.render({
elem: '#end' //指定元素
});
});
/*用户-停用*/
function member_stop(obj,id){
layer.confirm('确认要停用吗?',function(index){
if($(obj).attr('title')=='启用'){
//发异步把用户状态进行更改
$(obj).attr('title','停用')
$(obj).find('i').html('');
$(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
layer.msg('已停用!',{icon: 5,time:1000});
}else{
$(obj).attr('title','启用')
$(obj).find('i').html('');
$(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
layer.msg('已启用!',{icon: 5,time:1000});
}
});
}
/*用户-删除*/
function member_del(obj,id){
layer.confirm('确认要删除吗?',function(index){
//发异步删除数据
$(obj).parents("tr").remove();
layer.msg('已删除!',{icon:1,time:1000});
});
}
function delAll (argument) {
var ids = [];
// 获取选中的id
$('tbody input').each(function(index, el) {
if($(this).prop('checked')){
ids.push($(this).val())
}
});
layer.confirm('确认要删除吗?'+ids.toString(),function(index){
//捉到所有被选中的,发异步进行删除
layer.msg('删除成功', {icon: 1});
$(".layui-form-checked").not('.header').parents('tr').remove();
});
}
</script>
</html>