1、在之前的MVC(2)里把
Action.java
ActionModel.java
ActionSupport.java
ConfigModel.java
ConfigModelFactory.java
DispatcherServlet.java
ForwardModel.java
ModelDriven.java
这几个类导成mvc.jar包,在使用的把此mvc.jar包之前jar包导进去

2、把通用分页的助手类也拿过来

3、共用类(在之前的basedao里添加)
//将以下代码不一样的提出来作为参数放到方法里,以及对象不能是定死的,所以将它换为泛型
public int executeUpdate(String sql,T t,String [] attrs) throws Exception {
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
//将 t 的某一个属性对应的值加到ps对象中
for (int i = 0; i < attrs.length; i++) {
Field f = t.getClass().getDeclaredField(attrs[i]);
f.setAccessible(true);
ps.setObject(i+1, f.get(t));
}
return ps.executeUpdate();
}
}
方法层:
//增加
public int add(Book b) throws Exception {
String sql = "insert into tb_book values(?,?)";
return super.executeUpdate(sql, b, new String[] {"bid","bname"});
}
}
servlect层:(在之前的BookAction里写以下代码)
//增加
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.add(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//表示跳到查询界面
return "toList";
}
方法层:
//删除
public int del(Book b) throws Exception {
String sql = "delete from tb_book where bid=?";
return super.executeUpdate(sql, b, new String[] {"bid"});
}
servlect层:
//删除
public String del(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.del(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//表示跳到查询界面
return "toList";
}
方法层:
//修改
public int update(Book b) throws Exception {
String sql = "update tb_book set bname=? where bid=?";
return super.executeUpdate(sql, b, new String[] {"bname","bid"});
}
servlet层:
//查询
public String list(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean = new PageBean();
//初始化
pageBean.setReq(req);
List<Book> list = bookDao.list(book,pageBean);
req.setAttribute("books", list);
req.setAttribute("pb", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
//执行查询
return "list";
}
//跳转到新增或修改界面
public String preEdit(HttpServletRequest req, HttpServletResponse resp) {
try {
int bid = book.getBid();
if(bid != 0) {
//传递bid到后台,且只能查出一条数据,那么list集合里只有一条数据
List<Book> list = bookDao.list(book,null);
//id对应的值list.get(0)
req.setAttribute("book", list.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
//表示跳到查询界面
return "toEdit";
}
方法层:
//查询
public List<Book> list(Book book,PageBean pageBean) throws Exception{
String sql = "select * from tb_book where 1=1 ";
String bname = book.getBname();
System.out.println(book.getBname());
if(StringUtils.isNotBlank(bname)) {
sql += " and bname like '%"+bname+"%'";
}
int bid = book.getBid();
//前台传递到后台,只要传了就有值,没传就是默认值,默认值为0
if(bid!=0) {
sql += " and bid = "+bid;
}
return super.list(sql, pageBean, rs ->{
List<Book> ls = new ArrayList<Book>();
try {
while(rs.next()) {
ls.add(new Book(rs.getInt("bid"), rs.getString("bname")));
}
} catch (Exception e) {
e.printStackTrace();
}
return ls;
});
}
servlet层:
//查询
public String list(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean = new PageBean();
//初始化
pageBean.setReq(req);
List<Book> list = bookDao.list(book,pageBean);
req.setAttribute("books", list);
req.setAttribute("pb", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
//执行查询
return "list";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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.action?methodName=list" 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>
<a type="submit" class="btn btn-primary mb-2"
href="${pageContext.request.contextPath }/book.action?methodName=preEdit">增加</a>
</form>
<table class="table table-striped ">
<thead>
<tr>
<th scope="col">书籍ID</th>
<th scope="col">书籍名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${books }" var="b">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>
<a href="${pageContext.request.contextPath }/book.action?methodName=preEdit&bid=${b.bid}" >编辑</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid}" >删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<z:page pagebean="${pb }"></z:page>
</body>
</html>
界面展示:
