MVC增删查改(三)

MVC增删查改(三)

原理这一次我们需要做的就是把增删查改打包成架包
让他如何避免刷新重复提交

如何打架包

1:右键点击你要打成架包文件你将会看到一个Export
2:再Export里面再点击Export
3:找到JAR file 点击next
在这里插入图片描述
4.点中你需要达成架包的文件 点击finish 就可以了

首先我们先写一个***BookDaoTest***类
里面包含增删查改的方法

public List<Book> list(Book book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_mvc_book where 1=1";
		if(util.StringUtils.isNotBlank(book.getBname())) {
			sql+=" and bname like '%"+book.getBname()+"%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}

	//增加
	public int add(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="insert into t_mvc_book values(?,?,?)";
		return super.executeUpdate(sql, new String[] {"bid","bname","price"}, book);
	}
	//删除
	public int delete(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="delete from t_mvc_book where bid=?";
		return super.executeUpdate(sql, new String[] {"bid"}, book);
	}
	//修改
	
	public int update(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="update t_mvc_book set bname=?,price=? where bid=?";
		return super.executeUpdate(sql, new String[] {"bname","price","bid"}, book);
	}
	
}

写一个测试类
BookDaoTest

public class BookDaoTest {

	private BookDao bookDao=new BookDao();
	private Book book=null;
	@Before
	public void setUp() throws Exception {
		book=new Book();
	}

	/**
	 * 测试查询
	 */
	@Test
	public void testList() {
		try {
			List<Book> list = this.bookDao.list(book, null);
			System.out.println(list);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 测试增加
	 */
	@Test
	public void testAdd() {
		book.setBid(154);
		book.setBname("斗破");
		book.setPrice(10);
		try {
			this.bookDao.add(book);
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 测试删除
	 */

	@Test
	public void testDelete() {
		book.setBid(154);
		try {
			this.bookDao.delete(book);
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 测试修改
	 */
	@Test
	public void testUpdate() {
		book.setBid(154);
		book.setBname("遮天");
		book.setPrice(100);
		try {
			this.bookDao.update(book);
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

如何来测试我们写的代码是否正确呢:
第一步:选中项目右键找到Run As
第二部:然后我们会看到一个JUtil Test 或者是快捷键 (Alt+Shift+X,T),点击
第三步:出现绿色的横杆就代表我们写对了如果出现红色的横条就代表是是错误的
在这里插入图片描述
mvc.xml

增删改用重定向 查询用转发
1:我们先找到list 找到book.jsp 他不可以转发
2:我们再找到toList 找到它的路径利用隐藏域传值 单个查再用转发的方式

<action path="/bookAction" type="web.BookAction">
		<forward name="list" path="/book.jsp" redirect="false" />
		<forward name="toList" path="bookAction.action?methodName=list" redirect="true" />
		<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
	</action>

web.xml

文件过滤器
 <filter>
  			<filter-name>encodingFiter</filter-name>
            <filter-class>util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
            <filter-name>encodingFiter</filter-name>
            <url-pattern>/*</url-pattern>
  </filter-mapping>
  
   <servlet>
   			<servlet-name>dispatherServlet</servlet-name>
            <servlet-class>com.liuting.framework.DispatherServlet </servlet-class>
            <init-param>
                      <param-name>xmlPath</param-name>
                      <param-value>/mvc.xml</param-value>
            </init-param>
   </servlet>
   <servlet-mapping>
   		<servlet-name>dispatherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
   </servlet-mapping>
   

book(主页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
    <%@ taglib uri="/zking" prefix="z" %>
<!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>
<script type="text/javascript">
		function add(){
			window.location.href="bookEdit.jsp";
		}
		function update(bid){
			window.location.href="${pageContext.request.contextPath}/bookAction.action?methodName=load&&bid="+bid;
		}
		function del(bid){
			window.location.href="${pageContext.request.contextPath}/bookAction.action?methodName=delete&&bid="+bid;
		}


</script>
</head>
<body>
<h2>小说目录</h2> 
	

	<form action="${pageContext.request.contextPath}/bookAction.action?methodName=list"
		method="post">
		书名:<input type="text" name="bname"> <input type="submit"
			value="确定">
	</form>
	<button onclick="add();">新增</button>
	<table border="1" width="100%">
		<tr>
			<td>编号</td>
			<td>名称</td>
			<td>价格</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${bookList }" var="b">
			<tr>
				<td>${b.bid }</td>
				<td>${b.bname }</td>
				<td>${b.price }</td>
				<td>
					<button onclick="update(${b.bid });">修改</button>&nbsp;
					<button onclick="del(${b.bid });">删除</button>
				</td>
			</tr>
		</c:forEach>
	</table>
	
	
	${pageBean}
	<z:page pageBean="${pageBean }"></z:page>
	
</body>
</html>

bookEdit

<%@ 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>
<script type="text/javascript">
		function doSubmit(bid){
			var bookForm =document.getElementById("bookForm");
			if(bid){
				//修改时执行
				bookForm.action='${pageContext.request.contextPath}/bookAction.action?methodName=update';
			}else{
				//新增时候执行
				bookForm.action='${pageContext.request.contextPath}/bookAction.action?methodName=add';
			}
			bookForm.submit();
		}

</script>
</head>
<body>
	<form id="bookForm" method="post" action="${pageContext.request.contextPath}/bookAction.action?methodName=list">
	      bid:<input name="bid" value="${book.bid }"><br>
	      bname:<input name="bname" value="${book.bname }"><br>
	      price:<input name="price" value="${book.price }"><br>
	      <input type="submit" value="提交" onclick="doSubmit('${book.bid }');"><br>
	
	</form>
</body>
</html>

好啦这就是我们今天的增删查改啦!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值