增删改查Spring+MyBatis
其实这次写这个增删改查,我的收获很大,在同学的帮助下和老师的推动下,我也是学会了很多的技能点。
1.显示数据
显示数据对我而言可以说很好做,因为我以前增删改查做了有N遍,但是我却每次都是无功而返,半途而废。
查询为什么简单,因为查询不需要传入参数,sql语句不需要进行判断,只需要把所有数据显示出来就好了。
关键代码:
servlet:
List<Book> list = null;
list = bookService.selectAll();
request.setAttribute("bookN",list);
request.getRequestDispatcher("book.jsp").forward(request,response);
页面:
<body>
<table class="providerTable" >
<tr class="firstTr">
<th>图书编码</th>
<th >图书名称</th>
<th >作者</th>
<th >价钱</th>
</tr>
<%
List<Book> list2 = (List<Book>)request.getAttribute("bookN");
for (Book item:list2){%>
<tr>
<td name="bid"><%=item.getBookId()%></td>
<td><%=item.getBookName()%></td>
<td><%=item.getBookPc()%></td>
<td><%=item.getBookMy()%></td>
<td>
<a href="insertBook.jsp" >添加</a>
<a href="${pageContext.request.contextPath}/bookServlet?action=deleteOne&id=<%=item.getBookId()%>">删除</a>
<a href="${pageContext.request.contextPath}/bookServlet?action=updateOne&id=<%=item.getBookId()%>" >修改</a>
</td>
</tr>
<%
}
%>
<br>
<br>
</table> <br>
</body>
2.添加数据
添加数据返回值是一个int类型或者double类型,对我而言也是简单,不需要参数,而我也是在以前每次写完查询和添加应该就不会写了。
关键代码:
servlet:
if ("insertOne".equals(action)){
Book book=new Book();
book.setBookName(request.getParameter("NameOne"));
book.setBookPc( request.getParameter("PcOne"));
book.setBookMy(Integer.valueOf(request.getParameter("MyOne")));
int count=0;
try {
count = bookService.insertOne(book);
if (count>0){
request.getRequestDispatcher("/bookServlet?action=login").forward(request,response);
}else {
response.sendRedirect("/insertBook.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
页面:
<body>
<form action="/bookServlet?action=insertOne" method="post">
图书名称<input type="text" name="NameOne"><br>
图书作者<input type="text" name="PcOne"><br>
图书价格<input type="text" name="MyOne" onkeyup='this.value=this.value.replace(/\D/gi,"")'>
<input type="submit" value="提交">
</form>
</body>
3.删除数据
删除数据需要传入一个参数用于作为删除的条件,返回也是int或double,对我而言删除看似不难,其难。因为删除需要获取到页面上动态数据的某一列用来作为条件。所有怎么获取一列对我而言是个难点,但今天我明白了。
关键代码:
servlet:
if ("deleteOne".equals(action)){
System.out.println("进入删除的方法");
Book book=new Book();
int id = Integer.valueOf(request.getParameter("id"));
book.setBookId(id);
try {
int count = bookService.deleteOne(book);
if (count>0){
request.getSession().setAttribute("ids",id);
request.getRequestDispatcher("/bookServlet?action=login").forward(request,response);
}
else {
response.sendRedirect("book.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
页面:
<a href="${pageContext.request.contextPath}/bookServlet?action=deleteOne&id=<%=item.getBookId()%>">删除</a>//标红的代码是用来获取网页上的动态数据的关键代码。
4.修改数据
关键代码:
servlet:
if ("updateOne".equals(action)){ Book book=new Book(); idupdate= Integer.valueOf(request.getParameter("id")); try { Book bookId = bookService.getBookId(idupdate); request.setAttribute("bookId", bookId); if(bookId!=null){ request.getRequestDispatcher("updateBook.jsp").forward(request,response); } } catch (Exception e) { e.printStackTrace(); } } if ("updateTwo".equals(action)){ Book book=new Book(); int bbid = (int)request.getSession().getAttribute("bbid"); book.setBookId(bbid); book.setBookName(request.getParameter("NameTwo")); book.setBookPc(request.getParameter("PcTwo")); book.setBookMy(Integer.valueOf(request.getParameter("MyTwo"))); try { int count = bookService.updateOne(book); if (count>0){ request.getRequestDispatcher("/bookServlet?action=login").forward(request,response); } else { response.sendRedirect("updateBook.jsp"); } } catch (Exception e) { e.printStackTrace(); } }
页面:
<%@ page import="cn.happy.entity.Book" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: 秀清风 Date: 2018/12/16 Time: 20:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <form action="/bookServlet?action=updateTwo" method="post"> 图书名称<input type="text" name="NameTwo" value="<%=session.getAttribute("bbname")%>"><br> 图书作者<input type="text" name="PcTwo" value="<%=session.getAttribute("bbpc")%>"><br> 图书价格<input type="text" name="MyTwo" value="<%=session.getAttribute("bbmy")%>" onkeyup='this.value=this.value.replace(/\D/gi,"")'> <input type="submit" value="提交"> </form> </body> </html>