【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

✨SSM整合

页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。


本博客按照【狂神说Java】SSM框架最新整合教学IDEA版_哔哩哔哩_bilibili 进行SSM整合(对应视频 P1-P3 部分)

本篇更新相关功能实现(对应视频 P4-P7 部分)

!部分代码和视频有出入 请自行检查比对


✨配置Tomcat

运行 -> 编辑配置

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

点击 + 后选择 Tomcat Server(本地)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

点击 修正

部署工件后 点击确定

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)


✨项目结构修改

在项目结构设置中选择“工件”

WEB-INF 目录下新建 lib 目录

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

lib目录右键 -> 添加副本 -> 库文件

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

选择添加所有外部库

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)


✨查询书籍功能

Controller

com.example.controller 下新建类 BookController

package com.example.controller;

import com.example.pojo.Books;
import com.example.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.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

//    controller 调 service层

   @Autowired
   @Qualifier("bookServiceImpl")
   private BookService bookService;

//    查询全部书籍并且返回到一个书籍展示页面
   @RequestMapping("/allBook")
   public String list(Model model) {
       List<Books> list = bookService.listBooks();
       model.addAttribute("list", list);
       return "allBook";
  }
}

前端

WEB-INF/jsp 下新建 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美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>书籍列表 —— 显示所有书籍</small>
                </h1>
            </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>
                </tr>
                </thead>
                <%--                书籍从数据库中查询出来 从list中遍历 foreach--%>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookId}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.bookDetail}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

修改 **index.jsp **如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>

    <style>
      a{
        text-decoration: none;
        color: black;
        font-size: 18px;
      }
      h3{
        width: 180px;
        height: 38px;
        margin: 100px auto;
        text-align: center;
        line-height: 38px;
        background: deepskyblue;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>

  <h3>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
  </h3>

  </body>
</html>

启动Tomcat


效果展示

http://localhost:8080/SSM_build_war_exploded/

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

http://localhost:8080/SSM_build_war_exploded/book/allBook

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

✨添加书籍功能

Controller

修改 BookController 如下

package com.example.controller;

import com.example.pojo.Books;
import com.example.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.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

//    controller 调 service层

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

//    查询全部书籍并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.listBooks();
        model.addAttribute("list", list);
        return "allBook";
    }

//    跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

//    添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBooks =>" + books);
        bookService.insertBook(books);

//        重定向到 @RequestMapping("/allBook")请求
        return "redirect:/book/allBook";
    }
}

前端

修改 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美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>书籍列表 —— 显示所有书籍</small>
                </h1>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4 column">
<%--                新增书籍--%>
                <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>
                </tr>
                </thead>
                <%--                书籍从数据库中查询出来 从list中遍历 foreach--%>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookId}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.bookDetail}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

WEB-INF/jsp 下新建 addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增书籍</title>

    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>新增书籍</small>
                </h1>
            </div>
        </div>

        <form action="${pageContext.request.contextPath}/book/addBook" method="post">
            <div class="form-group">
                <label for="bookName">书籍名称</label>
                <input type="text" name="bookName" class="form-control" id="bookName" required>
            </div>
            <div class="form-group">
                <label for="bookCounts">书籍数量</label>
                <input type="text" name="bookCounts" class="form-control" id="bookCounts" required>
            </div>
            <div class="form-group">
                <label for="bookDetail">书籍描述</label>
                <input type="text" name="bookDetail" class="form-control" id="bookDetail" required>
            </div>

            <button type="submit" class="btn btn-default">添加</button>

        </form>
    </div>

</div>

</body>
</html>

效果展示

http://localhost:8080/SSM_build_war_exploded/book/allBook

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

http://localhost:8080/SSM_build_war_exploded/book/toAddBook

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)


✨修改书籍功能

Controller

修改 BookController 如下

package com.example.controller;

import com.example.pojo.Books;
import com.example.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.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

//    controller 调 service层

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

//    查询全部书籍并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.listBooks();
        model.addAttribute("list", list);
        return "allBook";
    }

//    跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

//    添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook =>" + books);
        bookService.insertBook(books);

//        重定向到 @RequestMapping("/allBook")请求
        return "redirect:/book/allBook";
    }

//    跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id, Model model){
        Books bookById = bookService.getBookById(id);
        model.addAttribute("getBookById", bookById);

        return "updateBook";
    }

//    修改书籍的请求
    @RequestMapping("updateBook")
    public String updataBook(Books books){
        System.out.println("updateBook =>" + books);
        bookService.insertBook(books);

        return "redirect:/book/allBook";
    }

}

前端

修改 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美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>书籍列表 —— 显示所有书籍</small>
                </h1>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4 column">
<%--                新增书籍--%>
                <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 var="book" items="${list}">
                    <tr>
                        <td>${book.bookId}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.bookDetail}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/toUpdate/?id=${book.bookId}">修改</a>
                            &nbsp; | &nbsp;
                            <a href="">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

WEB-INF/jsp 下新建 updateBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改书籍</title>

    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>修改书籍</small>
                </h1>
            </div>
        </div>

        <form action="${pageContext.request.contextPath}/book/updateBook" method="post">

<%--            后端需要根据bookId执行SQL--%>
<%--            前端传递隐藏域--%>
            <input type="hidden" name="bookId" value="${getBookById.bookId}">

            <div class="form-group">
                <label for="bookName">书籍名称</label>
<%--                name需要与后端一致--%>
                <input type="text" name="bookName" class="form-control" id="bookName" value="${getBookById.bookName}" required>
            </div>
            <div class="form-group">
                <label for="bookCounts">书籍数量</label>
                <input type="text" name="bookCounts" class="form-control" id="bookCounts" value="${getBookById.bookCounts}" required>
            </div>
            <div class="form-group">
                <label for="bookDetail">书籍描述</label>
                <input type="text" name="bookDetail" class="form-control" id="bookDetail" value="${getBookById.bookDetail}" required>
            </div>

            <button type="submit" class="btn btn-default">修改</button>

        </form>
    </div>

</div>

</body>
</html>

效果展示

http://localhost:8080/SSM_build_war_exploded/book/allBook

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

http://localhost:8080/SSM_build_war_exploded/book/toUpdate/?id=6

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)


✨删除书籍功能

Controller

修改 BookController 如下

package com.example.controller;

import com.example.pojo.Books;
import com.example.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.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

//    controller 调 service层

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

//    查询全部书籍并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.listBooks();
        model.addAttribute("list", list);
        return "allBook";
    }

//    跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

//    添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook =>" + books);
        bookService.insertBook(books);

//        重定向到 @RequestMapping("/allBook")请求
        return "redirect:/book/allBook";
    }

//    跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id, Model model){
        Books bookById = bookService.getBookById(id);
        model.addAttribute("getBookById", bookById);

        return "updateBook";
    }

//    修改书籍的请求
    @RequestMapping("updateBook")
    public String updataBook(Books books){
        System.out.println("updateBook =>" + books);
        bookService.updateBook(books);

        return "redirect:/book/allBook";
    }

//    删除书籍的请求
    @RequestMapping("toDelete/{bookId}")
    public String toDelete(@PathVariable("bookId") int id){
        System.out.println("deleteBook =>" + bookService.getBookById(id));
        bookService.deleteBookById(id);

        return "redirect:/book/allBook";
    }

}

前端

修改 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美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>书籍列表 —— 显示所有书籍</small>
                </h1>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4 column">
<%--                新增书籍--%>
                <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 var="book" items="${list}">
                    <tr>
                        <td>${book.bookId}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.bookDetail}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/toUpdate/?id=${book.bookId}">修改</a>
                            &nbsp; | &nbsp;
                            <a href="${pageContext.request.contextPath}/book/toDelete/${book.bookId}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

效果展示

http://localhost:8080/SSM_build_war_exploded/book/allBook

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

点击删除 可以删除书籍

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)


✨新增搜索功能

新增搜索功能涉及 dao层、service层、controller以及前端 修改

请按照步骤依次修改

本博客实现了模糊查询


dao层

BookMapper 接口下新增方法

package com.example.dao;

import com.example.pojo.Books;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BookMapper {
    int insertBook(Books books);

    int deleteBookById(@Param("bookId") int bookId);

    int updateBook(Books books);

    Books getBookById(@Param("bookId") int bookId);

    @Select("select * from ssm_build.books")
    List<Books> listBooks();

    List<Books> listBookLikeName(@Param("bookName") String bookName);
}

BookMapper.xml 中编写 SQL(或者使用注解)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.BookMapper">
    <insert id="insertBook" parameterType="books">
        insert into ssm_build.books(book_id, book_name, book_counts, book_detail)
        VALUES (#{bookId}, #{bookName}, #{bookCounts}, #{bookDetail})
    </insert>

    <delete id="deleteBookById" parameterType="int">
        delete from ssm_build.books where book_id = #{bookId}
    </delete>

    <update id="updateBook" parameterType="books">
        update ssm_build.books
        set book_name = #{bookName}, book_counts = #{bookCounts}, book_detail = #{bookDetail}
        where book_id = #{bookId}
    </update>

    <select id="getBookById" resultType="books">
        select * from ssm_build.books where book_id = #{bookId}
    </select>

    <select id="listBookLikeName" resultType="books">
        select * from ssm_build.books where book_name like '%${bookName}%'
    </select>

</mapper>

service层

BookService 接口下新增方法

package com.example.service;

import com.example.pojo.Books;

import java.util.List;

public interface BookService {
    int insertBook(Books books);

    int deleteBookById(int bookId);

    int updateBook(Books books);

    Books getBookById(int bookId);

    List<Books> listBooks();

    List<Books> listBookLikeName(String bookName);
}

BookServiceImpl 实现类中实现方法

package com.example.service;

import com.example.dao.BookMapper;
import com.example.pojo.Books;

import java.util.List;

public class BookServiceImpl implements BookService{

//    service层 调 dao层

    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public int insertBook(Books books) {
        return bookMapper.insertBook(books);
    }

    @Override
    public int deleteBookById(int bookId) {
        return bookMapper.deleteBookById(bookId);
    }

    @Override
    public int updateBook(Books books) {
//        System.out.println("BookServiceImpl: updateBook" + books);
        return bookMapper.updateBook(books);
    }

    @Override
    public Books getBookById(int bookId) {
        return bookMapper.getBookById(bookId);
    }

    @Override
    public List<Books> listBooks() {
        return bookMapper.listBooks();
    }

    @Override
    public List<Books> listBookLikeName(String bookName) {
        return bookMapper.listBookLikeName(bookName);
    }
}

Controller

修改 BookController 如下

package com.example.controller;

import com.example.pojo.Books;
import com.example.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.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

//    controller 调 service层

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

//    查询全部书籍并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.listBooks();
        model.addAttribute("list", list);
        return "allBook";
    }

//    跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

//    添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook =>" + books);
        bookService.insertBook(books);

//        重定向到 @RequestMapping("/allBook")请求
        return "redirect:/book/allBook";
    }

//    跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id, Model model){
        Books bookById = bookService.getBookById(id);
        model.addAttribute("getBookById", bookById);

        return "updateBook";
    }

//    修改书籍的请求
    @RequestMapping("updateBook")
    public String updataBook(Books books){
        System.out.println("updateBook =>" + books);
        bookService.updateBook(books);

        return "redirect:/book/allBook";
    }

//    删除书籍的请求
    @RequestMapping("toDelete/{bookId}")
    public String toDelete(@PathVariable("bookId") int id){
        System.out.println("deleteBook =>" + bookService.getBookById(id));
        bookService.deleteBookById(id);

        return "redirect:/book/allBook";
    }

//    查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName, Model model){
        List<Books> booksList = bookService.listBookLikeName(queryBookName);

        if (booksList.isEmpty()){
            booksList  = bookService.listBooks();
            model.addAttribute("error", "未查询到相关书籍!");
        }

        model.addAttribute("list", booksList );

        return "allBook";
    }
}

前端

修改 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美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/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>
                    <small>书籍列表 —— 显示所有书籍</small>
                </h1>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4 column">
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a>
                <%--                新增书籍--%>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
            </div>
            <div class="col-md-4 column"></div>
            <div class="col-md-8 column">
                <%--                查询书籍--%>
                <form class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="post" style="float: right">
                    <span style="color: red; font-weight: bold">${error}&nbsp;</span>
                    <label>
                        <input type="text" name="queryBookName" class="form-control" placeholder="请输入要查询的书籍名称">
                    </label>
                    <button type="submit" class="btn btn-default">查询</button>
                </form>
            </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 var="book" items="${list}">
                    <tr>
                        <td>${book.bookId}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.bookDetail}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/toUpdate/?id=${book.bookId}">修改</a>
                            &nbsp; | &nbsp;
                            <a href="${pageContext.request.contextPath}/book/toDelete/${book.bookId}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

效果展示

模糊查询

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

防止 SQL 注入

(Mapper中的SQL为 select * from ssm_build.books where book_name like '%${bookName}%'

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

通过Mybatis的日志可以发现dao层查询到数据为0

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)

之后 Controller return "allBook"; 再次进行查询在页面展示了所有书籍


Mybatis日志在 mybatis-config.xml 增加 setting即可开启

<setting name="logImpl" value="STDOUT_LOGGING"/>

✨最终项目结构(SSM整合及相关功能实现)

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(二)


✨参考及引用

SSM(SSM 框架集)_百度百科 (baidu.com)

【狂神说Java】SSM框架最新整合教学IDEA版_哔哩哔哩_bilibili

狂神说SpringMVC05:整合SSM框架 (qq.com)


⭐转载请注明出处

本文作者:双份浓缩馥芮白

原文链接:https://www.cnblogs.com/Flat-White/p/15219126.html

版权所有,如需转载请注明出处。

上一篇:SpringMVC--@RequestMapping注解以及SpringMVC获取请求参数


下一篇:SpringMVC中常用注解与使用方法