用mvc增删改查先把架包导进去
然后代码
book
package com.lj.entity;
public class Book {
private int bid;
private String bname;
private float price;
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
super();
}
}
BookDao这个是写增删改查的方法类
package com.lj.dao;
import java.util.List;
import com.lj.entity.Book;
import com.lj.util.BaseDao;
import com.lj.util.PageBean;
import com.lj.util.StringUtils;
public class BookDao extends BaseDao<Book>{
public List<Book> list(Book book,PageBean pageBean) throws Exception{
String sql = "select * from t_mvc_book where true";
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);
}
/**
* 增
*
*/
public int add(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, new String[]{"bid","bname","price"}, book) ;
}
/**
* 修改
*
*/
public int edit(Book book) throws Exception {
String sql = "update t_mvc_book set bname=? , price=? where bid=?";
return super.executeUpdate(sql, new String[]{"bname","price","bid"}, book) ;
}
/**
* 删除
*
*/
public int del(Book book) throws Exception { String sql = "delete from t_mvc_book where bid=?";
return super.executeUpdate(sql, new String[]{"bid"}, book);
}
}
}
BaseDao
package com.lj.util;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BaseDao<T> {
public List<T> executeQuery(String sql, Class clz, PageBean pagebean) throws Exception {
List<T> list = new ArrayList<>();
Connection con = DBAccess.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
if(pagebean !=null && pagebean.isPagination()) {
String countSql=getCountSql(sql);
ps=con.prepareStatement(countSql);
rs=ps.executeQuery();
if(rs.next()) {
pagebean.setTotal(rs.getInt(1));
}
String pageSql=getPageSql(sql,pagebean);
ps = con.prepareStatement(pageSql);
rs =ps.executeQuery();
}else {
ps = con.prepareStatement(sql);
rs =ps.executeQuery();
}
T t = null;
while(rs.next()) {
t=(T) clz.newInstance();
Field [] fields = clz.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
DBAccess.close(con, ps, rs);
return list;
}
public int executeUpdate(String sql,String []attrs,T t) throws Exception {
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
// 获得类属性
Field f = null;
for (int i =0 ;i < attrs.length; i++) {
f = t.getClass().getDeclaredField(attrs[i]);
f.setAccessible(true);
ps.setObject(i+1, f.get(t));
System.out.println(attrs[i]);
}
int n = ps.executeUpdate();
DBAccess.close(con, ps, null);
return n;
}
private String getPageSql(String sql,PageBean pagebean) {
return sql + " limit "+pagebean.getStartIndex()+","+pagebean.getRows() ;
}
private String getCountSql(String sql) {
return "select count(1) from ("+sql+") t";
}
}
bookAction
package com.lj.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lj.eneity.Book;
import com.lj.eneity.BookDao;
import com.lj.framework.ActionSupport;
import com.lj.framework.ModelDriven;
import com.lj.util.PageBean;public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book=new Book();
private BookDao bookDao=new BookDao();public String list(HttpServletRequest req,HttpServletResponse resp ) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = this.bookDao.list(book, pageBean);
req.setAttribute("bookList", list);
req.setAttribute("pageBean", pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "list";
}
public String preSave(HttpServletRequest req,HttpServletResponse resp) {
if(book.getBid()!=0) {
try {
//数据回显的数据
Book b = this.bookDao.list(book, null).get(0);
req.setAttribute("book", b);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "edit";
}
public String add(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.add(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toList";
}
public String edit(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.edit(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
public String del(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.del(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toList";
}
@Override
public Book getModel() {
return book;
}}
bookEdit.jsp
<%@ 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>书籍的编辑页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/book.action?methodName=${book.bname==null ? 'add' : 'edit'}" method="post">
bid:<input type="text" value="${book.bid }" name="bid" ><br>
bname:<input type="text" value="${book.bname }" name="bname" ><br>
price:<input type="text" value="${book.price }" name="price" ><br>
<input type="submit" value="ok" >
</form>
</body>
</html>
bookList.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="/onlyK" 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>书籍主页</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/book.action?methodName=list"
method="post">
<!-- 每页20行数据 -->
<!-- <input type="hidden" name='rows' value="20" > -->
<!-- 不分页 -->
<input type="hidden" name='pagination' value="false" >
书名:<input type="text" name="bname">
<!-- 书籍的类别:<input type="checkbox" name="bookType" value="1">玄幻
<input type="checkbox" name="bookType" value="2">都市
<input type="checkbox" name="bookType" value="3">言情 -->
<input type="submit"
value="确定">
</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:page pageBean="${pageBean }"></z:page>
<%-- <form id='pageBeanForm' action=' ${pageBean.url }' method='post'>
<input type='hidden' name=''> <input type='hidden' name='page'>
</form>
<div style='text-align: right; font-size: 12px;'>
每页N条,共N条,第N页,共N页 <a href='javascript:gotoPage(?)'>首页</a> <a
href='javascript:gotoPage(?)'>上一页</a> <a
href='javascript:gotoPage(?)'>下一页</a> <a
href='javascript:gotoPage(?)'>尾页</a> <input type='text'
id='skipPage'
style='text-align: center; font-size: 12px; width: 50px;'> <a
href='javascript:skipPage()'>Go</a>
</div> --%>
<!-- <script type='text/javascript'>
function gotoPage(page) {
document.getElementById('pageBeanForm').page.value = page;
document.getElementById('pageBeanForm').submit();
}
function skipPage() {
var page = document.getElementById('skipPage').value;
if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>maxPage){
alert('请输入1~N的数字');
return;
}
gotoPage(page);
}
</script> --></body>
</html>