• 自定义MVC框架增删改查(mysql)


    一、搭建环境

    1、在之前的MVC(2)里把

    Action.java
    ActionModel.java
    ActionSupport.java
    ConfigModel.java
    ConfigModelFactory.java
    DispatcherServlet.java
    ForwardModel.java
    ModelDriven.java

    这几个类导成mvc.jar包,在使用的把此mvc.jar包之前jar包导进去

    在这里插入图片描述
    2、把通用分页的助手类也拿过来

    在这里插入图片描述
    3、共用类(在之前的basedao里添加)

    	//将以下代码不一样的提出来作为参数放到方法里,以及对象不能是定死的,所以将它换为泛型
    	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();
    	}
    
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二、增加

    方法层:

    	//增加
    	public int add(Book b) throws Exception {
    		String sql = "insert into tb_book values(?,?)";
    		return super.executeUpdate(sql, b, new String[] {"bid","bname"});
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    servlect层:(在之前的BookAction里写以下代码)

    //增加
    	public String add(HttpServletRequest req, HttpServletResponse resp) {
    		try {
    			bookDao.add(book);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		//表示跳到查询界面
    		return "toList";
    	}
    	
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    三、删除

    方法层:

    //删除
    	public int del(Book b) throws Exception {
    		String sql = "delete from tb_book where bid=?";
    		return super.executeUpdate(sql, b, new String[] {"bid"});
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    servlect层

    //删除
    	public String del(HttpServletRequest req, HttpServletResponse resp) {
    		try {
    			bookDao.del(book);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		//表示跳到查询界面
    		return "toList";
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    四、修改

    方法层:

    	
    	//修改
    	public int update(Book b) throws Exception {
    		String sql = "update tb_book set bname=? where bid=?";
    		return super.executeUpdate(sql, b, new String[] {"bname","bid"});
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    servlet层:

    //查询
    	public String list(HttpServletRequest req, HttpServletResponse resp) {
    		try {
    			PageBean pageBean = new PageBean();
    			//初始化
    			pageBean.setReq(req);
    			List<Book> list = bookDao.list(book,pageBean);
    			req.setAttribute("books", list);
    			req.setAttribute("pb", pageBean);
    		} catch (Exception e) {
    			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("book", list.get(0));
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		//表示跳到查询界面
    		return "toEdit";
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    五、查询

    方法层:

    //查询
    	public List<Book> list(Book book,PageBean pageBean) throws Exception{
    	 String sql = "select * from tb_book where 1=1 ";
    	 String bname = book.getBname();
    	 System.out.println(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.list(sql, pageBean, rs ->{
    			List<Book> ls = new ArrayList<Book>();
    			try {
    				while(rs.next()) {
    					ls.add(new Book(rs.getInt("bid"), rs.getString("bname")));
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    			return ls;
    		});
    	}
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    servlet层:

    //查询
    	public String list(HttpServletRequest req, HttpServletResponse resp) {
    		try {
    			PageBean pageBean = new PageBean();
    			//初始化
    			pageBean.setReq(req);
    			List<Book> list = bookDao.list(book,pageBean);
    			req.setAttribute("books", list);
    			req.setAttribute("pb", pageBean);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		//执行查询
    		return "list";
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    六、使用

    <%@ 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="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
    	rel="stylesheet">
    <script
    	src="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="请输入书籍名称">
    		</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">增加</a>
    	</form>
    
    	<table class="table table-striped ">
    		<thead>
    			<tr>
    				<th scope="col">书籍ID</th>
    				<th scope="col">书籍名</th>
    				<th>操作</th>
    			</tr>
    		</thead>
    		<tbody>
    		<c:forEach items="${books }" var="b">
    			<tr>
    				<td>${b.bid }</td>
    				<td>${b.bname }</td>
    				<td>
    				 <a href="${pageContext.request.contextPath }/book.action?methodName=preEdit&bid=${b.bid}" >编辑</a>
    				 <a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid}" >删除</a>
    					
    				</td>
    			</tr>
    		</c:forEach>
    
    		</tbody>
    	</table>
    
    <z:page pagebean="${pb }"></z:page>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    界面展示:
    在这里插入图片描述

  • 相关阅读:
    考研数学|汤家凤《1800》vs 张宇《1000》怎么选?
    【前端】IOS微信浏览器点击右上角遮罩实现
    分布式前修课:Zookeeper锁实现方式
    ArduPilot开源飞控之Copter任务
    手机网页,输入时 软键盘盖住输入框完整解决方案,兼容安卓、鸿蒙、苹果IOS
    Docker(五)—— 镜像原理、容器快照commit
    渗透流程
    Base64、AES、MD5的区别与应用
    konva 系列教程 1:konva 是什么?
    密码学-SHA-1算法
  • 原文地址:https://blog.csdn.net/m0_62528678/article/details/125518963