前期回顾:
1.s阶段做查询:
1:获取链接对象.定义对象.预定义对象.结果集对象.代码重复
2.将结果集中的表列段对应的值封装到实体类中,然后添加到集合,这个过程是相似的
2.反射做通用查询
1.将公共的代码写在父类
2.反射实例化对象,获取类类对应的所以属性,遍历属性每个单个对象属性反射赋值,最终添加到集合中
3.通用查询融入分页的元素
sql=select * from t_mvc_book where bname like
countsql=select counet(1) as n from (sql) t
pageSQL= sql limit 起始下标,偏移量
起始下=(当前页-1)*偏移量
4.junit
在一个类中可以同时测试多个方法
通用分页前端
1.简化前端分页代码
目标:
1.理解分页思想
http://localhost:8080/mvc/user.action?bname=圣虚
原因:pagebean page=1,rows=10,pagination=true
http://localhost:8080/mvc/user.action?bname=圣虚
原因:pagebean page=2,rows=10,pagination=true
http://localhost:8080/mvc/user.action?bname=圣虚
原因:pagebean page=3,rows=10,pagination=true
结论:分页就是将上一次请求再发一次,只不过页码变了
2.优化pagebean
增加一个属性url,保留上一次发送的请求地址
增加一个属性paraMap,保留上一次发送的请求携带 req.getParmeterMap();
增加一个最大页的方法
增加一个下一页的方法
增加一个上一页的方法
初始化pagebean的方法
3.分页自定义jsp标签
HTML 分页条 js form
<z:page pageBean="${pageBean }">z:page tld 助手类
private PageBean Pagebean;
4.如何debug调试代码
1.debug启动项目
2.在将要调试的代码上打上断点
以上所以说代码如下:
Dao:
package com.zhanghao.tag; import java.io.IOException; import java.util.List; 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 com.zhanghao.dao.BookDao; import com.zhanghao.entity.Book; import com.zhanghao.uilt.PageBean; @WebServlet("/book.do") public class BookServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BookDao bookDao = new BookDao(); PageBean pageBean = new PageBean(); pageBean.setRequest(req); Book book = new Book(); book.setBname(req.getParameter("bname")); try { List<Book> list = bookDao.BaseDaoText(book,pageBean); req.setAttribute("books", list); req.setAttribute("pageBean", pageBean); req.getRequestDispatcher("/index.jsp").forward(req, resp); } catch (Exception e) { e.printStackTrace(); } } }
package com.zhanghao.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.zhanghao.entity.Book; import com.zhanghao.uilt.DBAccess; import com.zhanghao.uilt.PageBean; import com.zhanghao.uilt.StringUtils; /** * @author Cloud.Jun * @com.CloudJun.dao * @BookDao(说明):Book的模糊查询方法类继承通用分页方法类 */ public class BookDao extends BaseDao<Book>{ public List<Book> getlist(Book b, PageBean pb) throws Exception { // 实例化集合为容器,装载返回接收的数据 List<Book> list = new ArrayList<Book>(); // 进行mysql数据库连接 Connection conn = DBAccess.getConnection(); // 定义sql语句 String sql = "select * from t_mvc_book where 1=1 "; // 当关键词不为空的时候,拼接模糊查询的sql语句 String bname = b.getBname(); if (StringUtils.isNotBlank(bname)) { sql += " and bname like '%" + bname + "%'"; } // 执行sql语句 PreparedStatement pr = conn.prepareStatement(sql); // 返回所执行后的结果 ResultSet rs = pr.executeQuery(); while (rs.next()) { // 将结果集加入实体 Book book = new Book(rs.getInt(1), rs.getString(2), rs.getFloat(3)); // 将实体加入list集合 list.add(book); } if (list != null) { DBAccess.close(conn, pr, rs); } return list;// 最后返回容器(集合) } public void Text() throws Exception { Book book = new Book(); book.setBname("圣墟"); PageBean pb = new PageBean(); List<Book> list = new BookDao().getlist(book, pb); for (Book bo : list) { System.out.println(bo); } } public List<Book> BaseDaoText(Book book,PageBean pb) throws Exception { // 定义sql语句 String sql = "select * from t_mvc_book where 1=1 "; // 当关键词不为空的时候,拼接模糊查询的sql语句 String bname = book.getBname(); if (StringUtils.isNotBlank(bname)) { sql += " and bname like '%" + bname + "%'"; } return executeQuery(sql, book.getClass(), pb); } public void Text2() throws Exception { Book book = new Book(); book.setBname("圣墟"); PageBean pb = new PageBean(); //测试是否分页 // pb.setPagination(false); //测试显示在页面多少条 // pb.setRows(20); //测试初始页码 pb.setPage(2); List<Book> list = new BookDao().BaseDaoText(book, pb); for (Book bo : list) { System.out.println(bo); } } }
entity:
package com.zhanghao.entity; /** * @author Cloud.Jun * @com.CloudJun.entity * @Book(说明):书(实体类) */ public class Book { private int bid; private String bname; private float price; @Override public String toString() { return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]"; } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public Book(int bid, String bname, float price) { super(); this.bid = bid; this.bname = bname; this.price = price; } public Book() { super(); } }
接口:
package com.zhanghao.tag; import java.io.IOException; import java.util.List; 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 com.zhanghao.dao.BookDao; import com.zhanghao.entity.Book; import com.zhanghao.uilt.PageBean; @WebServlet("/book.do") public class BookServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BookDao bookDao = new BookDao(); PageBean pageBean = new PageBean(); pageBean.setRequest(req); Book book = new Book(); book.setBname(req.getParameter("bname")); try { List<Book> list = bookDao.BaseDaoText(book,pageBean); req.setAttribute("books", list); req.setAttribute("pageBean", pageBean); req.getRequestDispatcher("/index.jsp").forward(req, resp); } catch (Exception e) { e.printStackTrace(); } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!-- 引入标签库 --> <%@ taglib uri="http://mylxtag" prefix="z"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script> <title>书籍列表</title> <style type="text/css"> .page-item input { padding: 0; width: 40px; height: 100%; text-align: center; margin: 0 6px; } .page-item input, .page-item b { line-height: 38px; float: left; font-weight: 400; } .page-item.go-input { margin: 0 10px; } </style> </head> <body> <form class="form-inline" action="${pageContext.request.contextPath }/book.do" method="post"> <div class="form-group mb-2"> <input type="text" class="form-control-plaintext" name="bname" placeholder="请输入书籍名称"> </div> <button type="submit" class="btn btn-primary mb-2">查询</button> </form> <!-- books中为空进行跳转Servlet中调取方法获取数据在回来 --> <c:if test="${empty books }"> <jsp:forward page="book.do"></jsp:forward> </c:if> <table class="table table-striped"> <thead> <tr> <th scope="col">书籍编号</th> <th scope="col">书籍名称</th> <th scope="col">书籍价格</th> </tr> </thead> <tbody> <!-- books中不为空进行循环遍历 --> <c:if test="${not empty books }"> <c:forEach items="${books }" var="b"> <tr> <td>${b.bid }</td> <td>${b.bname }</td> <td>${b.price }</td> </tr> </c:forEach> </c:if> </tbody> </table> <z:page pageBean="${pageBean }"></z:page> </body> </html>
。。。tid:
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag zhanghao 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <!-- 标签库描述符 --> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Simple Tags</short-name> <uri>http://mylxtag</uri> <tag> <!-- 标签名 --> <name>page</name> <!-- 标签助手类 --> <tag-class>com.CloudJun.utils.PageTag</tag-class> <!-- 标签的内容类型:empty表示空标签,JSP表示可以为任何合法的JSP元素 --> <body-content>JSP</body-content> <attribute> <!-- 属性名, PageTag类中的pageBean属性相匹配 --> <name>pageBean</name> <!-- 表示该属性为必要的属性 --> <required>true</required> <!-- 该属性可以接受EL表示式的值 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>