在前一节中,我们了解了:
1.将中央控制器中的Action容器,变成可控制!
2.针对于反射调用业务代码,最终页面跳转
3.jsp页面参数传递后台的代码优化字段太多有影响!
然后命名,点击next点到底直到这个出现
将其勾选,next!
将我们的jar放在web项目的安全目录下
然后在进行 add buth将我们的jar加入项目
新建一个utils包,将我们的数据连接驱动,以及过滤器,连接保护文件,分页代码,以及通用的方法basedao加入进utils包中
定义需要的属性,提供get,set方法;tostring,,有参,无参数方法
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);
}
// 增加
public int add(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
Connection conn = DBAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, book.getBid());
ps.setObject(2, book.getBname());
ps.setObject(3, book.getPrice());
return ps.executeUpdate();
}
//删除
public int del(Book book) throws Exception {
String sql = "delete from t_mvc_book where bid=? ";
Connection conn = DBAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, book.getBid());
return ps.executeUpdate();
}
public int edit(Book book) throws Exception {
String sql = " update t_mvc_book set bname=?,price=? where bid=?";
Connection conn = DBAccess.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, book.getBname());
ps.setObject(2, book.getPrice());
ps.setObject(3, book.getBid());
return ps.executeUpdate();
}
缺点:
重复代码:
Connection conn = DBAccess.getConnection();
PreparedStatement ps =conn.prepareStatement(sql);
重复流程:
ps.setObject(1, book.getBid());
ps.setObject(2, book.getBname());ps.setObject(3, book.getPrice());
通过一个对象集合来存储实体的属性,然后再通过一个for循环其下标其目的是:根据操作来遍历出来所需要的属性,然后通过反射来操作,最后加入设置值
* 通用的增删改方法
* @param book
* @throws Exception
* sql:sql语句
* T:实体
* attrs:实体属性
*/
public int executeUpdate(String sql, T t, String[] attrs) throws Exception {
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
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));
}
return pst.executeUpdate();
}
只需要调用那个方法,根据需求编写sql语句,然后在通过return返回,sql语句,实体,集中中的元素
/**
* 通用增删改
*/
//增加
public int add(Book book) throws Exception {
String sql = "insert into t_mvc_book values(?,?,?)";
return super.executeUpdate(sql, book, new String[] {"bid","bname","price"});
}
//删除
public int del(Book book) throws Exception {
String sql = "delete from t_mvc_book where bid=? ";
return super.executeUpdate(sql, book, new String[] {"bid"});
}
//修改
public int edit(Book book) throws Exception {
String sql = "update t_mvc_book set bname=?,price=? where bid=? ";
return super.executeUpdate(sql, book, new String[] {"bname","price","bid"});
}
选择类目,crtl+n 建立一个类Juint test case给代码进行测试
实现思路:
1.首先在最外层:调用私有化的dao方法
2.在方法内部实例化实体
3.然后在通过dao.方法名
4.当然具体情况具体分析
package com.lz.dao;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import com.lz.entity.Book;
import com.lz.utils.PageBean;
public class BookDaoTest {
private BookDao bk=new BookDao();
@Test
public void testList() throws Exception {
Book book=new Book();
book.setBname("圣墟");
PageBean pageBean=new PageBean();
//pageBean.setPage(3);
pageBean.setPagination(false);
List list = bk.list(book, pageBean);
for (Book b : list) {
System.out.println(b);
}
}
@Test
public void testAdd() throws Exception {
Book book=new Book(16,"圣墟嘿嘿嘿",12f);
bk.add(book);
}
@Test
public void testDel() throws Exception {
Book book=new Book();
book.setBid(16);
bk.del(book);
}
@Test
public void testEdit() throws Exception {
Book book=new Book(16,"圣墟嘿嘿嘿",12f);
bk.edit(book);
}
}
选择方法
当出现绿色时就代表方法没有问题
name属性为:我们在Bookaction中 return方法的返回值
path为:跳转路径
tag-class属性为:我们配置的分页代码路径
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="http://jsp.veryedu.cn" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
书籍列表
书籍ID
书籍名
价格
${b.bid }
${b.bname }
${b.price }
修改
删除
由于增加,修改共用一个jsp界面,当我们传参数时要对于参数进行一个判断,使用三元运算符,如果b为就是增加操作,不然就是修改操作
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="http://jsp.veryedu.cn"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
书籍编辑界面