package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import entity.Book;
import util.BaseDao;
import util.PageBean;
import util.StringUtils;
public class BookDao extends BaseDao
public List
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 SQLException, IllegalArgumentException, IllegalAccessException {
String sql = "insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, new String[] {"bid","bname","price"}, book);
}
public int edit(Book book) throws SQLException, IllegalArgumentException, IllegalAccessException {
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 SQLException, IllegalArgumentException, IllegalAccessException {
String sql = "delete from t_mvc_book where bid = ?";
return super.executeUpdate(sql, new String[] {"bid"}, book);
}
public static void main(String[] args) {
BookDao bookDao = new BookDao();
Book book = new Book(141313, "xxx", 56f);
try {
bookDao.del(book);
} catch (IllegalArgumentException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
}
}
package 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;
import util.DBAccess;
import util.PageBean;
/**
* 通用分页查询(带条件查询)
*/
public class BaseDao
/**
*
* sql 由于不同的表查询语句不一样,所以靠子类写好来传递
* cls 需要返回不同的对象集合 查询的对象
* bean 可能要分页
* Exception
*/
public List
List
Connection con = DBAccess.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
//决定是否分页:
if(bean != null && bean.isPagination()) {//判读条件
//分页
/**
* 1.分页是与分页助手类pagebean中的total有关,需要查询数据库得到total赋值给bean
* 2.查询出符合条件的某一页的数据
*/
String countsql = getCountsql(sql);
ps = con.prepareStatement(countsql);
rs = ps.executeQuery();
/**
* 符合条件的记录数
* select count(1) from (select * from t_mvc_book where bname like '%斗破%') t;
*/
if(rs.next()){
bean.setTotal(rs.getObject(1).toString());//查询多少条数据
}
/**
* 分页的查询语句
*select * from t_mvc_book where bname like '%斗破%' limit 起始下标(第几页),变量(每页显示几条)
*/
String pagesql = getPagesql(sql, bean);
ps = con.prepareStatement(pagesql);
rs = ps.executeQuery();
}else {
//不分页
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
}
T t = null;
while (rs.next()) {
/**
* 1. 实例化一个你要查询的对象 (比如book)
* 2. 通过对象的属性名去游标中取出属性值(通过反射来完成)
* 2.1 获取到类对象中的属性
* 2.2 给属性赋值
* 3. 已经赋值好的实例对象放入到list集合当中
*/
t = (T) cls.newInstance();//反射获取对象开始
Field [] fields = cls.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;
}
/**
*
* @param sql
* @param bean 获取查询的起始下标和变量
* @return
*/
private String getPagesql(String sql , PageBean bean) {
// TODO 分页查询语句
return sql + " limit " +bean.getStartIndex()+","+bean.getRows();
}
private String getCountsql(String sql) {
// TODO 用来查询对应条件有多少条数据
return "select count(1) from ("+sql+") t";
}
/**
* 封装增删改
* sql c传过来的查询语句
* t 实体类
* attrs 属性名数组
* Exception
*/
public int executeUpdate (String sql,T t,String [] attrs) throws Exception {
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
Field fields = null;
for (int i = 0; i < attrs.length; i++) {
/**
* 通过属性名以反射的方法获取到对应的值
* 比如 attrs[0] = bid fields.get(t)就是bid的值
*/
fields = t.getClass().getDeclaredField(attrs[i]);
fields.setAccessible(true);//打开权限
ps.setObject(i+1, fields.get(t));
}
return ps.executeUpdate();
}
}
package web;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lww.framework.ActionSopport;
import com.lww.framework.ModelDriven;
import dao.BookDao;
import entity.Book;
import util.PageBean;
public class BookAction extends ActionSopport implements ModelDriven
private Book book = new Book();
private BookDao bookDao = new BookDao();
public String list(HttpServletRequest req,HttpServletResponse resp) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);//把请求的地址、参数存到pageBean
try {
List
req.setAttribute("bookList", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
/**
* 跳 新增修改页面(同一个)
*/
public String preSave(HttpServletRequest req,HttpServletResponse resp) {
if(book.getBid() != 0) {//判断是否有值 有就是修改 没有就是新增
try {
//数据回显的数据 根据bid查到数据回显
Book b = this.bookDao.list(book, null).get(0);
req.setAttribute("book", b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "edit";
}
/**
* 新增 新增数据
*/
public String add(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.add(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* 修改 修改数据
*/
public String edit(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.edit(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* 删除 删除数据
*/
public String del(HttpServletRequest req,HttpServletResponse resp) {
try {
this.bookDao.del(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Book getModel() {
return book;
}
}
<%@ 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="/lww" prefix="w"%>
编号 | 名称 | 价格 | 操作 |
${b.bid } | ${b.bname } | ${b.price } | 修改 删除 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
目录:
增和修改:
删:
之前:
之后: