一.前节回顾
在前一节中,我们了解了:
1.将中央控制器中的Action容器,变成可控制!
2.针对于反射调用业务代码,最终页面跳转
3.jsp页面参数传递后台的代码优化字段太多有影响!
二.项目部署前期准备工作
1.项目运行环境配置
1.1.首先新建一个web项目,完成xml构建
然后命名,点击next点到底直到这个出现
将其勾选,next!
1.2加载jar
将我们的jar放在web项目的安全目录下
然后在进行 add buth将我们的jar加入项目
1.3准备工作结束
新建一个utils包,将我们的数据连接驱动,以及过滤器,连接保护文件,分页代码,以及通用的方法basedao加入进utils包中
三.正式启动项目
1.创建实体包
定义需要的属性,提供get,set方法;tostring,,有参,无参数方法
2.建立通用查询方法
2.1.继承BaseDao<Book>实现通用查询
public List<Book> 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);}
3.老版本增删改,和新增删改
3.1老版本增删改
// 增加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());
3.2新版本增删改
3.2.1在basedao对于重复代码进行封装
通过一个对象集合来存储实体的属性,然后再通过一个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();}
3.2.2新版本
只需要调用那个方法,根据需求编写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"});}
4.juin测试
选择类目,crtl+n 建立一个类Juint test case给代码进行测试
4.1方法测试
实现思路:
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();@Testpublic void testList() throws Exception {Book book=new Book();book.setBname("圣墟");PageBean pageBean=new PageBean();//pageBean.setPage(3);pageBean.setPagination(false);List<Book> list = bk.list(book, pageBean);for (Book b : list) {System.out.println(b);}}@Testpublic void testAdd() throws Exception {Book book=new Book(16,"圣墟嘿嘿嘿",12f);bk.add(book);}@Testpublic void testDel() throws Exception {Book book=new Book();book.setBid(16);bk.del(book);}@Testpublic void testEdit() throws Exception {Book book=new Book(16,"圣墟嘿嘿嘿",12f);bk.edit(book);}}
4.2方法调用
选择方法
当出现绿色时就代表方法没有问题
5.配置xml文件,通过反射来实现一些方法
5.1.配置mvc.xml
name属性为:我们在Bookaction中 return方法的返回值
path为:跳转路径
5.2配置tid文件
tag-class属性为:我们配置的分页代码路径
6.实现web界面
6.1数据显示界面
<%@ 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" %>
<!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">
<linkhref="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"rel="stylesheet">
<scriptsrc="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>书籍列表</title>
<style type="text/css">
.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;
}
</style>
</head>
<body><form class="form-inline"action="${pageContext.request.contextPath }/book.action?methodName=list" method="post"><div class="form-group mb-2"><input type="text" class="form-control-plaintext" name="bname"placeholder="请输入书籍名称"><a href="${pageContext.request.contextPath }/book.action?methodName=toEdit&bid=${b.bid }">增加</a></div><button type="submit" class="btn btn-primary mb-2">查询</button></form><table class="table table-striped "><thead><tr><th scope="col">书籍ID</th><th scope="col">书籍名</th><th scope="col">价格</th></tr></thead><tbody><c:forEach items="${list }" var="b"><tr><td>${b.bid }</td><td>${b.bname }</td><td>${b.price }</td><td><a href="${pageContext.request.contextPath }/book.action?methodName=toEdit&bid=${b.bid }">修改</a><a href="${pageContext.request.contextPath }/book.action?methodName=tdel&bid=${b.bid }">删除</a></td></tr></c:forEach></tbody></table><z:page pageBean="${pageBean }"></z:page>
</body>
</html>
6.1.2效果图
6.2增加,修改
6.2.1代码
由于增加,修改共用一个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"%>
<!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 class="form-inline"action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'edit'}" method="post">书籍ID:<input type="text" name="bid" value="${b.bid }"><br>书籍名称:<input type="text" name="bname" value="${b.bname }"><br>书籍价格:<input type="text" name="price" value="${b.price }"><br><input type="submit"></form>
</body>
</html>
6.2.2效果图