自定义MVC增删改查

目录:

一,将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导进去

二,将分页标签相关文件,及相关助手类导入

三,完成book实体类及bookdao的编写

四,完成通用的增删改方法

五,完成BookAction

六,xml的配置

七,前台代码的编写


一,将框架打成jar包,然后导入新工程,并且把框架的依赖jar包导进去

将我们上一期内容中自己搭建的框架导成jar包

1,选中框架的包右键点击export...

2, 搜索框输入jar再搜索,选中jar file,点击next

3. 选择jar包的存放路径,点击finish,之后点击ok即可

我们将导出的框架jar包(mvc.jar)以及所需的依赖jar包导入

 

二,将分页标签相关文件,及相关助手类导入

 当所需的文件以及助手类全部导入时我们的框架的环境就已经搭建完毕了,可以进入到自己的编码开发阶段了

三,完成book实体类及bookdao的编写

book实体类

package com.ljj.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() {
		super();
	}
	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.ljj.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ljj.entity.Book;
import com.ljj.util.BaseDao;
import com.ljj.util.DBAccess;
import com.ljj.util.PageBean;
import com.ljj.util.StringUtils;
import com.mysql.jdbc.Connection;

/**
 * @author Administrator 刘俊杰
 *上午8:53:25
 */
public class BookDao extends BaseDao<Book> {
//	查询
	public List<Book> list(Book book,PageBean pageBean) throws Exception{
		String sql="select * from book where 1=1";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+bname+"%'";
		}
		int bid = book.getBid();
		//前台jsp传递到后台 只要传了就有值 没传就是默认值 0
		if(bid!=0) {
			sql+=" and bid="+bid;
		}
		return super.executeQuery(sql, pageBean, rs->{
			List<Book>list= new ArrayList<>();
			try {
				while(rs.next()) {
					list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return list;
		});
	}
	
//	增
	public int add(Book book) throws Exception {
		Connection con = (Connection) DBAccess.getConnection();
		String sql="insert into book values(?,?,?)";
		PreparedStatement ps = con.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 {
		Connection con = (Connection) DBAccess.getConnection();
		String sql="delete from book where bid=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, book.getBid());
		return ps.executeUpdate();
	}

//	改
	public int edit(Book book) throws Exception {
		Connection con = (Connection) DBAccess.getConnection();
		String sql="update book set bname=?,price=? where bid=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, book.getBname());
		ps.setObject(2, book.getPrice());
		ps.setObject(3, book.getBid());
		return ps.executeUpdate();
	}
	
	
}

四,完成通用的增删改方法

package com.ljj.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ljj.entity.Book;
import com.ljj.util.BaseDao;
import com.ljj.util.DBAccess;
import com.ljj.util.PageBean;
import com.ljj.util.StringUtils;
import com.mysql.jdbc.Connection;

/**
 * @author Administrator 刘俊杰
 *上午8:53:25
 */
public class BookDao extends BaseDao<Book> {
//	查询
	public List<Book> list(Book book,PageBean pageBean) throws Exception{
		String sql="select * from book where 1=1";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+bname+"%'";
		}
		int bid = book.getBid();
		//前台jsp传递到后台 只要传了就有值 没传就是默认值 0
		if(bid!=0) {
			sql+=" and bid="+bid;
		}
		return super.executeQuery(sql, pageBean, rs->{
			List<Book>list= new ArrayList<>();
			try {
				while(rs.next()) {
					list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return list;
		});
	}
	
	public int add(Book book) throws Exception {
		String sql="insert into book values(?,?,?)";
		return super.executeUpdate(sql, book, new String[] {"bid","bname","price"});
}
	public int del(Book book) throws Exception {
		String sql="delete from book where bid=?";
		return super.executeUpdate(sql, book, new String[] {"bid"});
	}
	
	public int edit(Book book) throws Exception {
		String sql="update book set bname=?,price=? where bid=?";
		return super.executeUpdate(sql, book, new String[] {"bname","price","bid"});
	}
	
}

五,完成BookAction

BookAction:

package com.ljj.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ljj.dao.BookDao;
import com.ljj.entity.Book;
import com.ljj.framework.ActionSupport;
import com.ljj.framework.ModelDriven;
import com.ljj.util.PageBean;

public class BookAction extends ActionSupport implements ModelDriven<Book>  {
	private Book book = new Book();
	private BookDao bookDao = new BookDao();
	
	@Override
	public Book getModel() {
		return book;
	}

//	增	
	public String add(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bookDao.add(book);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//代表跳到查询界面
		return "toList";
	}
	
//	删
	public String del(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bookDao.del(book);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//代表跳到查询界面
		return "toList";
	}
	
//	改
	public String edit(HttpServletRequest req, HttpServletResponse resp) {
		try {
			bookDao.edit(book);
		} catch (Exception e) {
			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) {
			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);
				req.setAttribute("b", list.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//代表跳到编辑界面
		return "toEdit";
	}
}

六,xml的配置

wuyanzu.xml:

七,前台代码的编写

bookList.jsp主界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@ taglib uri="http://jsp.veryedu.cn" prefix="l" %>
	<%@ 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 class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=preEdit">新增</a>
	</form>

	<table class="table table-striped bg-success">
		<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}">编辑</a>
				<a href="${pageContext.request.contextPath }/book.action?methodName=del&bid=${b.bid}">删除</a>
				</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<l:page pageBean="${pageBean}"></l:page>
</body>
</html>

增加与修改的通用界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/book.action?methodName=${empty b ? 'add' : 'edit'}" 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>

最终运行结果:

查找效果展示:

 

 新增效果展示:

 

 修改:上图中的书籍

 结果:(选中部分为进制表示)

删除:删除上图中的数据(已经搜索不到数据了)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值