1. 完成t_mvc_book表的增删改查
(一)增删改查Dao反法
分页查询和原始的增删改
package com.qukang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import com.qukang.entity.Book;
import com.qukang.util.BaseDao;
import com.qukang.util.DBAccess;
import com.qukang.util.PageBean;
import com.qukang.util.StringUtils;
public class BookDao extends BaseDao<Book> {
public List<Book> list(Book book,PageBean paBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_mvc_book where true";
String bname=book.getBname();
int bid=book.getBid();
if(StringUtils.isNotBlank(bname)) {
sql+=" and where bname like '%"+bname+"%'";
}
if(bid!=0) {
sql+=" and where bid="+bid;
}
return super.executeQuery(sql, Book.class, paBean);
}
public int edit(Book book) throws SQLException {
String sql=" update t_mvc_book set bname=?,price=?,bid=?";
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,book.getBname());
ps.setFloat(2,book.getPrice());
ps.setInt(3,book.getBid());
int n=ps.executeUpdate();
return n;
}
public int add(Book book) throws SQLException {
String sql="insert into t_mvc_book values(?,?,?)";
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,book.getBid());
ps.setString(2,book.getBname());
ps.setFloat(3,book.getPrice());
int n=ps.executeUpdate();
DBAccess.close(con, ps, null);
return n;
}
public int del(Book book) throws SQLException {
String sql="delete from t_mvc_book where bid=?";
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,book.getBid());
int n=ps.executeUpdate();
DBAccess.close(con, ps, null);
return n;
}
}
因为以下代码过于重复,我们可以直接拿处理写成一个增删改同用的赋值方法
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,book.getBid());
ps.setString(2,book.getBname());
ps.setFloat(3,book.getPrice());
在BaseDao中定义一个增删改通用反法
public int executeQuery(String sql,String []attrs,T t) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
// ps.setString(1,book.getBname());
// ps.setFloat(2,book.getPrice());
// ps.setInt(3,book.getBid());
for (int i=1; i < attrs.length; i++) {
Field field = t.getClass().getDeclaredField(attrs[i-1]);
ps.setObject(i,field.get(t));
}
int n=ps.executeUpdate();
return n;
}
简化后的增删改反法,查询没变,还是通用分页
package com.qukang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import com.qukang.entity.Book;
import com.qukang.util.BaseDao;
import com.qukang.util.DBAccess;
import com.qukang.util.PageBean;
import com.qukang.util.StringUtils;
public class BookDao extends BaseDao<Book> {
/**
* 分页查询
* @param book
* @param paBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Book> list(Book book,PageBean paBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_mvc_book where true";
String bname=book.getBname();
int bid=book.getBid();
if(StringUtils.isNotBlank(bname)) {
sql+=" and where bname like '%"+bname+"%'";
}
if(bid!=0) {
sql+=" and where bid="+bid;
}
return super.executeQuery(sql, Book.class, paBean);
}
/**
* 修改
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int edit(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql=" update t_mvc_book set bname=?,price=?,bid=?";
return executeQuery(sql, new String[] {"banem" ,"price","bid"},book);
}
/**
* 增加
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int add(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="insert into t_mvc_book values(?,?,?)";
return executeQuery(sql, new String[] {"bid","bname","price"},book);
}
/**
* 删除
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int del(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql="delete from t_mvc_book where bid=?";
return executeQuery(sql, new String[] {"bid"},book);
}
}
根据返回的结果码进行页面跳转
package com.qukang.framework;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qukang.dao.BookDao;
import com.qukang.entity.Book;
import com.qukang.util.PageBean;
public class BookAction extends ActionSupport implements ModelDrivern<Book> {
private Book book=new Book();
private BookDao bookdao=new BookDao();
/**
* 分页查询
* @param resp
* @param req
* @return
*/
public String list(HttpServletResponse resp,HttpServletRequest req) {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = this.bookdao.list(book, pageBean);
req.setAttribute("pageBean", pageBean);
req.setAttribute("Booklist", list);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "list";
}
/**
* 根据传来的参数来判断是增加还是修改
* @param resp
* @param req
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* @throws InstantiationException
*/
public String preSavva(HttpServletResponse resp,HttpServletRequest req) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException {
if(book.getBid()==0) {
System.out.println("增加逻辑");
}else {
Book b = this.bookdao.list(book,null).get(0);
req.setAttribute("book",b);
}
return "edit";
}
/**
* 新增
* @param resp
* @param req
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* @throws InstantiationException
*/
public String add(HttpServletResponse resp,HttpServletRequest req) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException {
this.bookdao.add(book);
return "toList";
}
/**
* 修改
* @param resp
* @param req
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* @throws InstantiationException
*/
public String edit(HttpServletResponse resp,HttpServletRequest req) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException {
this.bookdao.add(book);
return "toList";
}
/**
* 删除
* @param resp
* @param req
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* @throws InstantiationException
*/
public String del(HttpServletResponse resp,HttpServletRequest req) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException {
this.bookdao.del(book);
return "toList";
}
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
}
mvc.xml
增删改一定要重定向,要不然刷新界面可能报错,并且你不重定向可能每次刷新界面只能显示你刚刚处理的那一条数据
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/Cal" type="com.qukang.web.CalAction">
<forward name="rs" path="/Demo1.jsp" redirect="false" />
</action>
<action path="/book" type="com.qukang.web.BookAction">
<forward name="list" path="/bookList.jsp" redirect="false" />
<forward name="edit" path="/bookEdit.jsp" redirect="false" />
<forward name="toList" path="/book.action?methodName=list"/>
</action>
</config>
(二)增删改查JSP页面
查询删除界面
<%@ 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="/zking" 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">
<title>Insert title here</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/book.actio "
method="post">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
<!-- <input type="hidden" name="pagination" value="true">
<input type="hidden" name="rows" value="20"> -->
</form>
<a href="${pageContext.request.contextPath}/book.action?methodName=preSave">增加</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>操作</td>
</tr>
<c:forEach items="${bookList }" var="b">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
<td><a href="${pageContext.request.contextPath}/book.action?methodName=preSave&&bid=${b.bid}">修改</a>
<a href="${pageContext.request.contextPath}/book.action?methodName=del&&bid=${b.bid}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<z:demo pageBean="${pageBean }"></z:demo>
</body>
</html>
增加修改
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/book.action" method="post">
<input type="hidden" name="methodName" value="${book.bname == null ? 'add' : 'edit' }">
书籍ID:<input type="text" name="bid" value="${book.bid }" >
书籍名称:<input type="text" name="bname" value="${book.bname }" >
书籍价格:<input type="text" name="price" value="${book.price }" >
<input type="submit" >
</form>
</body>
</html>
主界面
增加界面
修改界面