* 思路:
* 1、从传进来的t中读取属性值
* 2、往预定义对象中设置了值
* @param sql
* @param t
* @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 ps=con.prepareStatement(sql);
for (int i = 0; i < attrs.length; i++) {
Field f = t.getClass().getDeclaredField(attrs[i]);
// 打开访问权限
// t–>book,f–>bid
f.setAccessible(true);
ps.setObject(i+1, f.get(t));
}
ps.executeUpdate();
}
}
Dao方法:
package com.lv.dao;
import java.util.List;
import com.lv.entity.Book;
import com.lv.util.BaseDao;
import com.lv.util.PageBean;
import com.lv.util.StringUtils;
public class BookDao extends BaseDao{
/**
* 增删改查的通用套路
* 1、建立连接
* 2、预定义对象PreparedStatement
* 3、设置占位符
* 4、ps.executeUpdate()
* @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”});
}
// 修改
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”});
}
// 删除
public void delete(Book book) throws Exception {
String sql=“delete from t_mvc_book where bid=?”;
super.executeUpdate(sql, book, new String[] {“bid”});
}
// 查看
public List list(Book book,PageBean pageBean) throws Exception {
String sql=“select * from t_mvc_book where 1=1”;
String bname=book.getBname();
int bid=book.getBid();
if(StringUtils.isNotBlank(bname)) {
sql+=" and bname like ‘%“+bname+”%’";
}
if(bid!=0) {
sql+=" and bid = "+bid;
}
return super.executeQuery(sql, Book.class, pageBean);
}
}
子控制器BookAction:
类需要****extends ActionSupport implements ModelDriver
package com.lv.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lv.dao.BookDao;
import com.lv.entity.Book;
import com.lv.framework.ActionSupport;
import com.lv.framework.ModelDriver;
import com.lv.util.PageBean;
public class BookAction extends ActionSupport implements ModelDriver{
private Book book=new Book();
private BookDao bd=new BookDao();
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
// 增删改最终都要跳回查询页面
/**
-
分析增删改查一共有多少结果集的配置
-
查询:BookList.jsp 返回值:list
-
增删改确定:book.action?methodName=list 返回值:toList
-
增删改跳转对应界面:bookEdit.jsp 返回值:toEdit
-
@param req
-
@param resp
-
@return
*/
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.add(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return “toList”;
}
public String list(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List list = bd.list(book, pageBean);
req.setAttribute(“books”, list);
req.setAttribute(“pageBean”, pageBean);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return “list”;
}
public String delete(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.delete(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return “toList”;
}
public String edit(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.edit(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return “toList”;
}
// 跳转到新增修改页面
public String toEdit(HttpServletRequest req, HttpServletResponse resp) {
try {
// 如果是跳转修改页面,那么需要做bid条件的精准查询
if(book.getBid()!=0) {
List list=bd.list(book, null);
req.setAttribute(“b”, list.get(0));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return “toEdit”;
}
}
主界面:
<%@ page language=‘java’ contentType=‘text/html; charset=UTF-8’
pageEncoding=‘UTF-8’%>
<%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core”%>
<%@ taglib prefix=“z” uri=“http://jsp.veryedu.cn”%>
href=‘https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css’
rel=‘stylesheet’>
src=‘https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js’>
书籍列表
.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;
}
<form class=“form-inline”
action=“${pageContext.request.contextPath }/book.action?methodName=list” method=‘post’>
<input type=‘text’ class=‘form-control-plaintext’ name=‘bname’
placeholder=‘请输入书籍名称’>
查询
书籍ID
书籍名
价格
操作
<c:forEach var=“b” items=“${books }”>
${b.bid }
${b.bname }
${b.price }