一、导入jar包
二、web.xml做配置
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--
在这里每加一个配置,就相当于actions.put("/goods", new GoodsAction());
这样就解决了代码灵活性的问题
-->
<action path="/book" type="com.lxy.web.BookAction">
<forward name="list" path="/bookList.jsp" redirect="false" />
<forward name="toList" path="/book.action?methodName=list" redirect="true" />
<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
</action>
</config>
三、正式开发
实体类:book
所有dao类的父类
public class BaseDao<T> {
/**
* 通用的增删改方法
* @param book
* @throws Exception
*/
public void executeUpdate(String sql, T t, String[] attrs) throws Exception {
// String[] attrs = new String[] {"bid", "bname", "price"};
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(1, book.getBid());
// pst.setObject(2, book.getBname());
// pst.setObject(3, book.getPrice());
/*
* 思路:
* 1.从传进来的t中读取属性值
* 2.往预定义对象中设置了值
*
* t->book
* f->bid
*/
for (int i = 0; i < attrs.length; i++) {
Field f = t.getClass().getDeclaredField(attrs[i]);
f.setAccessible(true);
pst.setObject(i+1, f.get(t));
}
pst.executeUpdate();
}
dao方法
package com.wmy.dao;
import java.util.List;
import com.wmy.book.Book;
import com.wmy.util.BaseDao;
import com.wmy.util.PageBean;
import com.wmy.util.StringUtils;
public class BookDao extends BaseDao<Book>{
/*
* public void add(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
pst.setObject(1, book.getBid());
pst.setObject(2, book.getBname());
pst.setObject(3, book.getPrice());
pst.executeUpdate();
}
增删改查的通用套路
1.建立链接
2.预定义对象PreparedStatement
3.设置占位符的?的值
4.pst.executeUpdate();
1.
*/
/**
* 增
* @param book
* @throws Exception
*/
public void add(Book book)throws Exception{
String sql="insert into t_mvc_book values(?,?,?)";
super.executeUpdate(sql, book,new String[] {"bid","bname","price"});
}
/**
* 修
* @param book
* @throws Exception
*/
public void edit(Book book)throws Exception{
String sql="update t_mvc_book set bname=?,price=?where bid=?";
super.executeUpdate(sql, book,new String[] {"bname","price","bid"});
}
/**
* 删
* @param book
* @throws Exception
*/
public void delete(Book book)throws Exception{
String sql="delete from t_mvc_book where bid=?";
super.executeUpdate(sql, book,new String[] {"bid"});
}
/**
* 查
* @param book
* @param pageBean
* @return
* @throws Exception
*/
public List<Book> list(Book book,PageBean pageBean)throws Exception{
String sql="select * from t_mvc_book where 1=1";
String bname=book.getBname();
if(StringUtils.isNotBlank(bname)) {
sql+="and bname like'%"+bname+"%'";
}
return super.executeQuery(sql, Book.class,pageBean);
}
子控制器
package com.wmy.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wmy.dao.BookDao;
import com.wmy.book.Book;
import com.wmy.util.PageBean;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
/**
* 目标:
* 利用自己做的自定义mvc框架完成增删改查
* 通用的分页查询
* 通用的增删改
* 步骤
* 1.导入框架的jar包
* 2.做好框架的使用配置
* 3.一切照旧
* JSP、servlet/BookAction、Dao层、entity
* @author Administrator
*
*/
public class BookAction extends ActionSupport implements ModelDriver<Book>{
private Book book = new Book();
private BookDao bookDao = new BookDao();
@Override
public Book getModel() {
return book;
}
/*
* 增删改最终都要跳回查询界面
* 分析增删改查一共有多少结果集的配置
* 查询:BookList.jsp 返回值:list
* 增删改确定:book.action?methodName=list 返回值:toList
* 增加修改跳转对应界面:bookEdit.jsp 返回值:toEdit
*/
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.add(book);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
public String list(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Book> list = bookDao.list(book,pageBean);
req.setAttribute("books", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
public String delete(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.delete(book);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
public String edit(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.edit(book);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 跳转到新增修改页面
* @param req
* @param resp
* @return
*/
public String toEdit(HttpServletRequest req, HttpServletResponse resp) {
try {
/*
* 如果是跳转修改页面,那么需要做bid条件的精准查询
*/
if(book.getBid() != 0) {
List<Book> list = bookDao.list(book, null);
req.setAttribute("b", list.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
return "toEdit";
}
}
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.lxy.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="请输入书籍名称">
<!-- <input name="rows" value="20" type="hidden"> -->
<!-- 不想分页 -->
<input name="pagination" value="false" type="hidden">
</div>
<button type="submit" class="btn btn-primary mb-2">查询</button>
<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=toEdit">新增</a>
</form>
<table class="table table-striped bg-success">
<thead>
<tr>
<th scope="col">书籍id</th>
<th scope="col">书籍名</th>
<th scope="col">价格</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="b" items="${books }">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
<td>
<a href="${pageContext.request.contextPath }/book.action?methodName=toEdit&bid=${b.bid}">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=delete&bid=${b.bid}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
修改/增加是同一个界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.lxy.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>
</head>
<body>
<form class="form-inline"
action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'edit'}" method="post">
书籍ID:<input type="text" name="bid" value="${b.bid }"><br>
书籍名称:<input type="text" name="bname" value="${b.bname }"><br>
书籍价格:<input type="text" name="price" value="${b.price }"><br>
<input type="submit">
</form>
</body>
</html>
下面是效果