1 查询所有商品流程分析
2 删除所选商品流程分析
3 修改所选商品流程分析
4 添加商品流程分析
(插播:反射与工具类抽取)
反射
工具类抽取
工具类抽取举例:
MyBaseSerlvet.java
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 这个类就是我们要抽取的工具类,专门用于反射子类中的方法,并执行子类中的方法;
*/
public class MyBaseSerlvet extends HttpServlet{
//tocmat会调用子类的service方法,如果子类中没有service,自动会找到这个类中的service方法,并执行
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1:获取浏览器传递过来的方法名
String methodName = req.getParameter("m");
//2:获取子类的字节码文件对象
//System.out.println(this);
Class c = this.getClass();
//3:从c中获取methodName方法
try {
Method m=c.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//4:让m执行起来
String s=(String) m.invoke(this,req,resp);
//5:判断子类是否返回了一个要转发的路径,如果有路径,帮子类转发,否则什么也不做
if(s!=null){
req.getRequestDispatcher(s).forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("工具类运行错误,请检查是否传递了方法名!!!或其他异常!");
}
}
}
AServlet.java
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AServlet
*/
public class AServlet extends MyBaseSerlvet {
public void a(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("子类的a方法执行了...");
}
public void b(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("子类的b方法执行了...");
}
public void c(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("子类的c方法执行了...");
}
}
BServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
*/
public class BServlet extends MyBaseSerlvet {
public String abc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("bservlet的abc方法执行了...");
//转发的index.jsp
return "/index.jsp";
}
}
6 批量删除流程分析
7 多条件查询流程分析
多条件查询dao层的固定套路:
//7:多条件查询商品的方法
public List<Product> findProductByTiaoJian(String pn, String pdesc) throws SQLException {
//1:创建sql执行者
QueryRunner q = new QueryRunner(MyC3P0Utils.getDataSource());
//2:编写sql语句
String sql = "select * from product where 1=1 ";
//3:准备参数与结果集;
List<String> params = new ArrayList<>();
if(pn!=null&&pn.trim().length()>0){
sql+=" and pname like ?";
params.add("%"+pn+"%");
}
if(pdesc!=null&&pdesc.trim().length()>0){
sql+=" and pdesc like ?";
params.add("%"+pdesc+"%");
}
BeanListHandler rsh = new BeanListHandler(Product.class);
//4:执行sql处理结果
return q.query(sql, rsh, params.toArray());
}
8 分页查询
8.1 分页PageBean工具类:类的抽取与分页关键字介绍
MySQL方言:limit关键字
#举例:每页显示5条,显示第3页;
SELECT * FROM product LIMIT 10,5;
#说明:5表示每页显示5条,10表示第3页的第一条数据的索引为10(第一页索引从0开始)
自己写一个分页工具类MyPageBean.java
import java.util.List;
/*
* 分页工具类,可以进行任意数据的分页查询时,使用
*/
public class MyPageBean<T>{
private int pageNumber;// 当前页码值,需要用户在浏览器传递;默认1;
private int pageSize;// 每页显示条数,可以让用户在浏览器传递;也可以程序员直接写死,开发中常用的是浏览器传递;
private int totalCount;//数据库中数据的总数量;从数据库查询;
private int totalPage;//总页数,直接使用总数量和每页显示数量,计算即可得到;
private int startIndex;//数据库中数据的起始索引,根据当前页和每页显示数量,计算得到;
//当前页中的数据
private List<T> list;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return this.totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
//计算总页数
public int getTotalPage() {
return totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getStartIndex() {
return (pageNumber-1)*pageSize;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public MyPageBean(int pageNumber, int pageSize) {
super();
this.pageNumber = pageNumber;
this.pageSize = pageSize;
}
public MyPageBean() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "MyPageBean [pageNumber=" + pageNumber + ", pageSize=" + pageSize + ", totalCount=" + totalCount
+ ", totalPage=" + totalPage + ", startIndex=" + startIndex + ", list=" + list + "]";
}
}