目录
1.将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导入进去
选择file文件后点击下一步

命名完后点击下一步
jar包打出来后将其导入项目即可


《代码演示book》
- package com.dengxiyan.entity;
-
- public class Book {
-
- private int bid;
- private String bname;
- private float 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() {
- // TODO Auto-generated constructor stub
- }
-
- public Book(int bid, String bname, float price) {
- super();
- this.bid = bid;
- this.bname = bname;
- this.price = price;
- }
-
- @Override
- public String toString() {
- return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
- }
-
-
- }
《代码演示bookDao》
以下方法既节省代码,更是便捷了程序人员
- package com.dengxiyan.dao;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- import com.dengxiyan.entity.Book;
- import com.dengxiyan.util.BaseDao;
- import com.dengxiyan.util.DBAccess;
- import com.dengxiyan.util.PageBean;
- import com.dengxiyan.util.StringUtils;
-
- public class BookDao extends BaseDao<Book>{
-
- //查询
- public List<Book> list(Book book,PageBean pageBean) throws Exception{
- String sql = "select * from tb_book where 1=1 ";
- String bname = book.getBname();
- if(StringUtils.isNotBlank(bname)) {
- sql+= " and bname like '%"+bname+"%'";
- }
- int bid = book.getBid();
- //前台传递到后台,只要传了就有值,没传就是默认值,默认值为0
- if(bid!=0) {
- sql+= " and bid = "+bid;
- }
- return super.executeQuery(sql, pageBean, rs ->{
- List<Book> ls = new ArrayList<Book>();
- try {
- while(rs.next()) {
- ls.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return ls;
- });
- }
-
- //增加
- /*public int add(Book b) throws Exception {
- Connection con = DBAccess.getConnection();
- String sql = "insert into tb_book values(?,?,?)";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setObject(1, b.getBid());
- ps.setObject(2, b.getBname());
- ps.setObject(3, b.getPrice());
- return ps.executeUpdate();
- }*/
-
- public int add(Book b) throws Exception {
- String sql = "insert into tb_book values(?,?,?)";
- return super.executeUpdate(sql, b, new String[] {"bid","bname","price"});
- }
-
-
- //删除
- /*public int del(Book b) throws Exception {
- Connection con = DBAccess.getConnection();
- String sql = "delete from tb_book where bid=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setObject(1, b.getBid());
- return ps.executeUpdate();
- }*/
-
- public int del(Book b) throws Exception {
- String sql = "delete from tb_book where bid=?";
- return super.executeUpdate(sql, b, new String[] {"bid"});
- }
-
-
- //修改
- /*public int update(Book b) throws Exception {
- Connection con = DBAccess.getConnection();
- String sql = "update tb_book set bname=?,price=? where bid=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setObject(1, b.getBname());
- ps.setObject(2, b.getPrice());
- ps.setObject(3, b.getBid());
- return ps.executeUpdate();
- }*/
-
-
- public int update(Book b) throws Exception {
- String sql = "update tb_book set bname=?,price=? where bid=?";
- return super.executeUpdate(sql, b, new String[] {"bname","price","bid"});
- }
-
-
-
-
- }
《代码演示》
在bookDao里面继承了改类,并实现了以下方法
- //将以下代码不一样的提出来作为参数放到方法里,以及对象不能是定死的,所以将它换为泛型
- public int executeUpdate(String sql,T t,String [] attrs) throws Exception {
- Connection con = DBAccess.getConnection();
- PreparedStatement ps = con.prepareStatement(sql);
- //将 t 的某一个属性对应的值加到ps对象中
- for (int i = 0; i < attrs.length; i++) {
- Field f = t.getClass().getDeclaredField(attrs[i]);
- f.setAccessible(true);
- ps.setObject(i+1, f.get(t));
- }
- return ps.executeUpdate();
- }
在book子控制器里调用方法
《代码演示》
方法里的返回值为xml配置文件里的name值
- package com.dengxiyan.servlet;
-
- import java.util.List;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.dengxiyan.dao.BookDao;
- import com.dengxiyan.entity.Book;
- import com.dengxiyan.framework.ActionSupport;
- import com.dengxiyan.framework.ModelDriven;
- import com.dengxiyan.util.PageBean;
-
- public class BookAction extends ActionSupport implements ModelDriven<Book> {
-
- private Book book = new Book();
- private BookDao bookDao = new BookDao();
-
-
- @Override
- public Book getModel() {
- // TODO Auto-generated method stub
- return book;
- }
-
-
- //增加
- public String add(HttpServletRequest req, HttpServletResponse resp) {
- try {
- bookDao.add(book);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //表示跳到查询界面
- return "toList";
- }
-
-
- //删除
- public String del(HttpServletRequest req, HttpServletResponse resp) {
- try {
- bookDao.del(book);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //表示跳到查询界面
- return "toList";
- }
-
-
- //修改
- public String update(HttpServletRequest req, HttpServletResponse resp) {
- try {
- bookDao.update(book);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //表示跳到查询界面
- return "toList";
- }
-
-
- //查询
- public String list(HttpServletRequest req, HttpServletResponse resp) {
- try {
- PageBean pageBean = new PageBean();
- //初始化
- pageBean.setRequest(req);
- List<Book> list = bookDao.list(book,pageBean);
- req.setAttribute("list", list);
- req.setAttribute("pageBean", pageBean);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //执行查询
- return "list";
- }
-
-
-
- //跳转到新增或修改界面
- public String preEdit(HttpServletRequest req, HttpServletResponse resp) {
- try {
- int bid = book.getBid();
- if(bid != 0) {
- //传递bid到后台,且只能查出一条数据,那么list集合里只有一条数据
- List<Book> list = bookDao.list(book, null);
- //id对应的值list.get(0)
- req.setAttribute("b", list.get(0));
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //表示跳到查询界面
- return "toEdit";
- }
-
-
-
- }
xml配置文件
提示:true是重定向,false是转发
- <?xml version="1.0" encoding="UTF-8"?>
- <config>
- <action path="/book" type="com.dengxiyan.servlet.BookAction">
- <forward name="list" path="/booklist.jsp" redirect="false" />
- <forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
- <forward name="toList" path="/book.action?methodName=list" redirect="true" />
-
- </action>
- </config>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@taglib uri="http://jsp.veryedu.cn" prefix="z"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <!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">
- <link
- href="css/bootstrap.css"
- rel="stylesheet">
- <script
- src="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="请输入书籍名称">
- </div>
- <button type="submit" class="btn btn-primary mb-2">查询</button>
- <a type="submit" class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=preEdit" method="post">增加</a>
- </form>
-
- <table class="table table-striped">
- <thead>
- <tr>
- <th scope="col">书籍ID</th>
- <th scope="col">书籍名</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=preEdit&bid=${b.bid}" method="post">编辑</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid}" method="post">删除</a>
- </td>
- </tr>
- </c:forEach>
- </tbody>
- </table>
-
- <z:page pagebean="${pageBean}"></z:page>
-
- </body>
- </html>
增加和修改的界面,在action里面判断一下如果是增加就跳增加,不是则跳修改
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'update'}" method="post">
- bid:<input type="text" name="bid" value="${b.bid}"><br>
- bname:<input type="text" name="bname" value="${b.bname}"><br>
- price:<input type="text" name="price" value="${b.price}"><br>
- <input type="submit">
- </form>
- </body>
- </html>
效果图如下
修改界面

模糊查询

提示:当效果出不来时请尝试手动输入地址栏