新建一个实体类,用来存储每页数据,数据量,页码,页数,下一页,上一页的相关信息。
@Data
public class PageModel {
//本页数据
private List pageData;
//最大数量
private Integer pageSize=2;
//第几页
private Integer pageNo;
//总记录数
private Integer count;
//获取总页数
public Integer getAllPage(){
return count%pageSize==0?count/pageSize:count/pageSize+1;
}
//判断是否有下一页
public Integer getNextPage(){
if(pageNo1){
return pageNo-1;
}else{
return 1;
}
}
分页展示代码:
分页展示
<%-- --%>
<%-- --%>
共${pageModel.count}条记录 当前第${pageModel.pageNo}页 一共${pageModel.allPage}页 每页展示${pageModel.pageSize}条数据
在dao层学生类中新建两个方法,一个用来获取总记录数,一个用来获取本页数据(使用limit实现),再通过service层调用方法。
@Override
public int getCount() {
int count = Integer.parseInt( new JDBCUtils().queryOneVal("select count(1) from student").toString());
return count;
}
@Override
public List getPageData(int pageNo, int pageSize) {
return new JDBCUtils().executeQuery(Student.class,"select * from student limit ?,?",(pageNo-1)*pageSize,pageSize);
}
servlet层有个list方法,页码初始值为1,随着获取页面传的页码改变,然后调用service层获取该页数据信息,传递到页面进行展示。
public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int pageNo = 1;//页码初始值为1
if(null != req.getParameter("pageNO")){
pageNo = Integer.parseInt(req.getParameter("pageNO"));//获取页面传来的页码改变
}
PageModel pageModel = new StudentService().getpageModel(pageNo);
List list = pageModel.getPageData();//获取页面数据
List getall = new GradeService().getall();//获取年级信息
req.setAttribute("GAll",getall);
req.setAttribute("list",list);
req.setAttribute("pageModel",pageModel);
req.getRequestDispatcher("pageList.jsp").forward(req,resp);
}
点击添加按钮,转入后端servlet代码的preadd方法中,查询出年级信息并传到前端页面通过下拉列表展示,然后填写信息,点击提交,
public void preadd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
GradeService gradeService = new GradeService();
List all = gradeService.getall();
// System.out.println(all);
req.setAttribute("list",all);
req.getRequestDispatcher("addStu.jsp").forward(req,resp);
}
添加页面代码
Title
学生添加
页面提交表单,调用servlet层的add方法,该方法获取到所有参数信息,生成一个学生对象,然后调用dao层的添加方法添加学生信息,并调用list方法转到分页展示页面。
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数
// System.out.println(request.getParameter("StudentNo"));
StudentDao studentDao = new StudentDao();
String studentNo = request.getParameter("stuno");
String loginPwd = request.getParameter("stupassword");
String studentName = request.getParameter("stuname");
String sex = request.getParameter("stusex");
String gradeId = request.getParameter("stugradeid");
String phone = request.getParameter("stuphone");
String address = request.getParameter("stuaddress");
String bornDate = request.getParameter("stuborn");
String email = request.getParameter("stuemail");
Student student = new Student(studentNo,loginPwd,studentName,sex,Integer.parseInt(gradeId),phone,address,bornDate,email);
//插入数据
studentDao.save(student);
// System.out.println(update);
//响应页面
response.sendRedirect("student.do?method=list");
}
点击页面学生信息后面的删除,调用servlet层的delete方法并将学生学号传递过来,delete方法调用service层的delete方法通过学号删除该学生信息,并重新调用list方法转到数据分页展示页面。
public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String studentNo = req.getParameter("studentNo");
StudentService service = new StudentService();
service.delete(studentNo);
resp.sendRedirect("student.do?method=list");
}
点击页面学生信息后的修改,将学生学号传递过来调用servlet层的preupdate方法,preupdate方法通过学号查询出该学生相关信息,并通过GradeService查询出所有的年级信息,传输该数据到数据修改页面,默认值为修改前的值。然后用户修改完后调用service层的update方法通过学号修改该学生信息,并重新调用list方法转到数据分页展示页面。
public void preupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数
String studentNo = request.getParameter("studentNo");
// System.out.println(studentNo);
StudentService studentService = new StudentService();
Student student = studentService.selectOne(studentNo);
GradeService gradeService = new GradeService();
List getall = gradeService.getall();
// System.out.println(getall);
// request.setAttribute("gradename",gradename);
request.setAttribute("GAll",getall);
request.setAttribute("student",student);
// System.out.println(student);
//响应页面
request.getRequestDispatcher("update.jsp").forward(request,response);
}
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数
Student student = new Student();
student.setStudentno(request.getParameter("stuno"));
student.setStudentname(request.getParameter("stuname"));
student.setLoginpwd(request.getParameter("stupassword"));
student.setSex(request.getParameter("stusex"));
student.setPhone(request.getParameter("stuphone"));
student.setAddress(request.getParameter("stuaddress"));
student.setBorndate(request.getParameter("stuborn"));
student.setEmail(request.getParameter("stuemail"));
student.setGradename(request.getParameter("gradename"));
String name = request.getParameter("gradename");
Grade grade = new GradeService().findOneByName(name);
student.setGradeid(grade.getGradeid());
System.out.println(student);
StudentService studentService = new StudentService();
studentService.update(student);
//响应页面
response.sendRedirect("student.do?method=list");
}
修改页面代码:
更新
信息修改
数据查询(条件查询):
通过输入查询条件然后点击查询后服务端执行servlet层的select方法,该方法调用service层的getall、getpagedata方法来获取符合条件的记录数及相关数据,保存并发送到页面。后续点击切换页码也执行该方法查询符合条件的数据并传输展示。
//条件查询
//获取符合条件的总记录数
@Override
public int getCount(stuCondition stuCondition) {
StringBuilder stringBuilder = new StringBuilder("select count(1) from student s left join grade g on s.GradeId = g.GradeId where 1 = 1");
List
public void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
// System.out.println("select方法进入!");
stuCondition condition = new stuCondition();
// System.out.println(req.getParameter("stuname"));
condition.setStuname(req.getParameter("stuname"));
condition.setStuadd( req.getParameter("stuadd"));
condition.setStusex(req.getParameter("stusex"));
condition.setGradeName(req.getParameter("gradename"));
System.out.println(req.getParameter("gradename"));
System.out.println(condition);
int pageNo=1;
String pageNoStr = req.getParameter("pageNo");
if(StringUtils.Stringjudge(pageNoStr)){
pageNo = Integer.parseInt(pageNoStr);
}
System.out.println("当前页码"+pageNo);
PageModel pageModel = new StudentService().getPageModel(condition,pageNo);
// System.out.println(pageModel);
List list = pageModel.getPageData();
List getall = new GradeService().getall();
req.setAttribute("GAll",getall);
req.setAttribute("list",list);
req.setAttribute("pageModel",pageModel);
req.getRequestDispatcher("pageList.jsp").forward(req,resp);
}
查询:
增加:
删除:(name=人工费)
修改:(学号为s2004的学生)