2024.3.20 Wednesday
接上文【WEEK4】 【DAY2】整合SSM框架之功能实现—总览、添加数据【中文版】
目录
- 7.6.修改功能
- 7.6.1.修改BookController.java
- 7.6.2.修改allBook.jsp
- 7.6.3.新建updateBook.jsp
- 7.6.4.修改MyBatis-config.xml
- 7.6.5.运行
- 7.7.删除功能
- 7.7.1.修改BookController.java
- 7.7.2.修改allBook.jsp
- 7.7.3.修改BookDao.xml
- 7.7.4.运行
7.6.修改功能
7.6.1.修改BookController.java
(加上删除功能的完整BookController.java代码位于7.7.1.)
//跳转到修改页面
@RequestMapping("/toUpdate")
public String toUpdatePaper(int id,Model model){Books books = bookService.queryBookById(id); //返回一本书model.addAttribute("QBook",books);return "updateBook";
}//修改书籍
@RequestMapping("updateBook")
public String updateBook(Books books){System.out.println("updateBook=>"+books);bookService.updateBook(books);return "redirect:/book/allBook";
}
7.6.2.修改allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>书籍展示页</title><%--BootStrap美化页面--%><%--导入在线BootStrap--%><link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container"> <%--容器--%><div class="row clearfix"> <%--标记栏,清除浮动--%><div class="col-md-12 column"><div class="page-header"> <!--为标签为h1的内容增加空间(和其他部分分隔)--><h1><small>书籍列表——显示所有书籍</small></h1></div></div><div class="row"><div class="col-md-4 column"><%--应该添加一个请求->由BookController.java进行处理--%><a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a><%--建立一个按钮--%></div></div></div><div class="row clearfix"><div class="col-md-12 column"><table class="table table-hover table-striped"> <%--鼠标漂浮在某行上方时加深颜色--%><thead><tr> <%--表格抬头--%><th>书籍编号</th><th>书籍名称</th><th>书籍数量</th><th>书籍详情</th><th>操作</th></tr></thead><%--书籍从数据库中查询出来,从这个list中遍历出来,使用foreach--%><tbody><%--c:forEach会自动导入标签(第一行)--%><c:forEach var="book" items="${list}"><tr><td>${book.bookID}</td><td>${book.bookName}</td><td>${book.bookCounts}</td><td>${book.detail}</td><td> <%--操作, 为空格。修改时希望能够看到原本的数据,故需要先获取到相关数据(这里是传入原书籍的id)--%><a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a> | <%--“#”号用于占位,在这里页面上虽然出现“删除”字样但是没用任何实现功能--%><a href="#">删除</a></td></tr></c:forEach></tbody></table></div></div></div></body>
</html>
7.6.3.新建updateBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>修改书籍信息</title><link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
</head><body><div class="container"> <%--容器--%><div class="row clearfix"> <%--标记栏,清除浮动--%><div class="col-md-12 column"><div class="page-header"> <!--为标签为h1的内容增加空间(和其他部分分隔)--><h1><small>修改书籍</small></h1></div></div></div><%--书籍表单--%><form action="${pageContext.request.contextPath}/book/updateBook" method="post"><%--value后面的表示默认值--%><input type="hidden" name="bookID" value="${QBook.bookID}"/> <%--这行掌管修改的成功与否,是前端使用隐藏域返回id--%>书籍名称:<input type="text" name="bookName" value="${QBook.bookName}"/>书籍数量:<input type="text" name="bookCounts" value="${QBook.bookCounts}"/>书籍详情:<input type="text" name="detail" value="${QBook.detail}"/><input type="submit" value="修改"/></form></div></body>
</html>
隐藏域
https://baike.baidu.com/item/%E9%9A%90%E8%97%8F%E5%9F%9F/249616?fr=ge_ala
jsp页面表单提交后mysql未更新原因
https://www.cnblogs.com/Meng2113/p/13511800.html
7.6.4.修改MyBatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--加上以下三行配置->每一步操作都会在server显式打印--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><!--配置数据源,移交到Spring完成--><typeAliases><package name="P17.project"/></typeAliases><mappers><!--如果dao中两个文件名称不同,则使用mapper resource--><!--以下三行任选一行即可--><mapper resource="P17/dao/BookDao.xml"/>
<!-- <mapper class="P17.dao.BookDao"/>-->
<!-- <package name="P17.dao"/>--></mappers>
</configuration>
7.6.5.运行
http://localhost:8080/SSMbuild_war_exploded/(主页没变化)
http://localhost:8080/SSMbuild_war_exploded/book/allBook
http://localhost:8080/SSMbuild_war_exploded/book/toUpdate?id=1
已修改
http://localhost:8080/SSMbuild_war_exploded/book/allBook
7.7.删除功能
7.7.1.修改BookController.java
package P17.controller;import P17.project.Books;
import P17.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;import java.awt.print.Book;
import java.util.List;@Controller
@RequestMapping("/book")
public class BookController {
//controller调用service层@Autowired@Qualifier("BookServiceImplement")private BookService bookService;//查询全部的书籍,并返回到一个书籍展示页面@RequestMapping("/allBook")public String list(Model model){List<Books> list = bookService.queryAllBook(); //调用业务层的方法,查询到所有的书籍model.addAttribute("list",list); //返回前端查询return "allBook"; //返回到allBook.jsp}//跳转到增加书籍页面@RequestMapping("/toAddBook")public String toAddPaper(){return "addBook";}//添加书籍的请求@RequestMapping("/addBook")public String addBook(Books books){System.out.println("addBook=>"+books); //打印日志bookService.addBook(books);return "redirect:/book/allBook"; //重定向到@RequestMapping("/allBook")请求}//跳转到修改页面@RequestMapping("/toUpdate")public String toUpdatePaper(int id,Model model){Books books = bookService.queryBookById(id); //返回一本书model.addAttribute("QBook",books);return "updateBook";}//修改书籍@RequestMapping("updateBook")public String updateBook(Books books){System.out.println("updateBook=>"+books);bookService.updateBook(books);return "redirect:/book/allBook";}//删除书籍,使用RestFul风格@RequestMapping("/deleteBook/{bookId}")public String deleteBook(@PathVariable("bookId") int id){bookService.deleteBookById(id);return "redirect:/book/allBook";}
}/*
这是一般的解决方法@RequestMapping("/deleteBook")public String deleteBook(int id){bookService.deleteBookById(id);return "redirect:/book/allBook";}*/
7.7.2.修改allBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>书籍展示页</title><%--BootStrap美化页面--%><%--导入在线BootStrap--%><link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container"> <%--容器--%><div class="row clearfix"> <%--标记栏,清除浮动--%><div class="col-md-12 column"><div class="page-header"> <!--为标签为h1的内容增加空间(和其他部分分隔)--><h1><small>书籍列表——显示所有书籍</small></h1></div></div><div class="row"><div class="col-md-4 column"><%--应该添加一个请求->由BookController.java进行处理--%><a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a><%--建立一个按钮--%></div></div></div><div class="row clearfix"><div class="col-md-12 column"><table class="table table-hover table-striped"> <%--鼠标漂浮在某行上方时加深颜色--%><thead><tr> <%--表格抬头--%><th>书籍编号</th><th>书籍名称</th><th>书籍数量</th><th>书籍详情</th><th>操作</th></tr></thead><%--书籍从数据库中查询出来,从这个list中遍历出来,使用foreach--%><tbody><%--c:forEach会自动导入标签(第一行)--%><c:forEach var="book" items="${list}"><tr><td>${book.bookID}</td><td>${book.bookName}</td><td>${book.bookCounts}</td><td>${book.detail}</td><td> <%--操作, 为空格。修改时希望能够看到原本的数据,故需要先获取到相关数据(这里是传入原书籍的id)--%><a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a> | <%--删除--%><a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a></td></tr></c:forEach></tbody></table></div></div></div></body>
</html>
7.7.3.修改BookDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--一个mapper对应一个接口-->
<mapper namespace="P17.dao.BookDao"><!--增加一本书--><insert id="addBook" parameterType="books">
# ssmbuild.books指的是在ssmbuild数据库中的books这张表上进行操作INSERT INTO ssmbuild.books(bookName, bookCounts, detail)VALUES (#{bookName},#{bookCounts},#{detail});</insert><!--根据id删除一本书--><!--这里的id就是BookController中调用bookService.deleteBookById(id);的具体方法名,不能写错,否则报错500--><delete id="deleteBookById" parameterType="int">DELETE FROM ssmbuild.books WHERE bookID = #{bookId}</delete><!--更新Book--><update id="updateBook" parameterType="Books">UPDATE ssmbuild.booksSET bookName = #{bookName}, bookCounts = #{bookCounts}, detail = #{detail}WHERE bookID = #{bookID}</update><!--根据id查询,返回一个Book--><select id="queryBookById" resultType="Books">SELECT * FROM ssmbuild.booksWHERE bookID = #{bookId}</select><!--查询全部Book--><select id="queryAllBook" resultType="Books">SELECT * FROM ssmbuild.books</select></mapper>
7.7.4.运行
http://localhost:8080/SSMbuild_war_exploded/(主页没变化)
http://localhost:8080/SSMbuild_war_exploded/book/allBook
点击“删除”后