其实这次写这个增删改查,我的收获很大,在同学的帮助下和老师的推动下,我也是学会了很多的技能点。

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>

增删改查Spring+MyBatis的更多相关文章

  1. commons-dbutils实现增删改查(spring新注解)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  2. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

  3. Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询

    1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...

  4. ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)

    在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,以下就仅仅贴出各层实现功能的代码: Jsp页面实现功能的js代码例如以下: <script ...

  5. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。

    1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...

  6. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查

    使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...

  7. Spring Boot入门系列(六)如何整合Mybatis实现增删改查

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  8. Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查

    之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...

  9. ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查

    三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...

随机推荐

  1. win10修改TXT文件的关联软件

    打开注册表,按下面路径找: HKEY_CLASSES_ROOT -> txtfile -> shell -> open -> command 在右边可以看到一个默认文件,原来的 ...

  2. 杨其菊/常惠琢《面向对象程序设计(java)》第十一周学习总结

    <面向对象程序设计>第十一周学习总结 第一部分:理论知识 JAVA的集合框架 JAVA的集合框架实现对各种数据结构的封装,以降低对数据管理与处理的难度. 所谓框架就是一个类库的集合,框 ...

  3. liunx一键安装禅道

    一定要把压缩吧放在/opt下面,然后tar解压,启动 https://blog.csdn.net/sinat_23957257/article/details/82697458

  4. sha1withRSA算法

    RAS_USE_PRIVATE_ENCRYPT(3021300906052b0e03021a05000414 + SHA1(DATA))

  5. EL和JSTL笔记

    1.EL表达式 [1] 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > ...

  6. python3 tkinter添加图片和文本

    在前面一篇文章基础上,使用tkinter添加图片和文本.在开始之前,我们需要安装Pillow图片库. 一.Pillow的安装 1.方法一:需要下载exe文件,根据下面图片下载和安装       下载完 ...

  7. thinkphp 5 使用oss

    简单的tp5中上传到 图片到oss我本地开发环境为:WAMP;php版本:5.6.19TP版本:5.1.13 1.使用composer 安装 composer require aliyuncs/oss ...

  8. 常用API String

    Java的API以及Object类 Java的API Java的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JD ...

  9. idea取消vim模式

    在安装idea时选择了vim编辑模式,但是用习惯了eclipse,总是要拷贝粘贴,在idea中一直按ctrl+c和ctrl+v不起总用.于是想把vim模式关闭掉.方法:菜单栏:tools->vi ...

  10. temp--贵州银行

    -------住宿----泊乐酒店----8905----与朱聿一起住 2018年  1月3日晚 1月4日晚  1月5日晚 1月6日晚  1月7日晚 1月8日晚  1月9日晚 已结清! ======= ...