SSM整合案例:图书管理系统
SSM整合案例:图书管理系统
Spring + SpringMVC + MyBatis+JSP+Servlet+简单前端知识
环境要求:
- IDEA
- MySQL 5.7.19
- Tomcat 9
- Maven 3.6
1、搭建数据库环境
创建一个存放书籍数据的数据库表:
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books`(
	`bookID` INT(10) PRIMARY KEY AUTO_INCREMENT COMMENT '书id',
	`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
	`bookCounts` INT(11) NOT NULL COMMENT '数量',
	`detail` VARCHAR(200) NOT NULL COMMENT '描述'
);
INSERT INTO `books`(`bookID`, `bookName`, `bookCounts`, `detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
SELECT * FROM books;
数据库连接 url:
jdbc:mysql:/主机:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
注:如果使用的是MySQL8.0+,url连接时增加一个时区的配置:serverTimezone=Asia/Shanghai
2、基本环境搭建
2.1、新建一个Maven项目,起名为:ssmbuild,添加web的支持
2.2、导入pom的相关依赖
<!--导入依赖 junit 数据库连接 数据库连接池 c3p0 Spring MyBatis mybatis-spring servlet jsp jstl-->
<dependencies>
    <!--junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!--数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <!-- 数据库连接池 -->
    <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.5</version>
    </dependency>
    <!--Spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.3.RELEASE</version>
    </dependency>
    <!--MyBatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.3</version>
    </dependency>
    <!--Servlet JSP-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
    </dependency>
</dependencies>
2.3、Maven静态资源过滤设置
<!--静态资源过滤问题-->
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
2.4、建立基本结构和配置框架!
java包:
- com.rainszj.pojo
- com.rainszj.dao
- com.rainszj.service
- com.rainszj.controller
resources包:
- mybatis-config.xml - <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration> </configuration>
 
- applicationContext.xml - <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
 
- database.properties 
3、MyBatis层编写
3.1、pojo包
- Books
package com.rainszj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}
3.2、dao包
- BookMapper
package com.rainszj.dao;
import com.rainszj.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
    /**
     * 增加一本书
     *
     * @param books
     * @return
     */
    int addBook(Books books);
    /**
     * 删除一本书
     *
     * @param id
     * @return
     */
    int deleteBook(@Param("bookId") int id);
    /**
     * 修改一本书
     *
     * @param book
     * @return
     */
    int updateBook(Books book);
    /**
     * 根据 id 查询一本书
     *
     * @param id
     * @return
     */
    Books queryBookById(@Param("bookId") int id);
    /**
     * 查询所有的书
     *
     * @return
     */
    List<Books> queryAllBook();
}
- BookMapper.xml
<?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.rainszj.dao.BookMapper">
    <insert id="addBook" parameterType="Books">
        insert into ssmbuild.books (bookName, bookCounts, detail)
        values (#{bookName}, #{bookCounts}, #{detail})
    </insert>
    <delete id="deleteBook" parameterType="int">
        delete from ssmbuild.books where bookID = #{bookId}
    </delete>
    <update id="updateBook" parameterType="Books">
        update ssmbuild.books
        set bookName = #{bookName}, bookCounts = #{bookCounts}, detail = #{detail}
        where bookID = #{bookID}
    </update>
    <select id="queryBookById" resultType="Books">
        select * from ssmbuild.books where bookID = #{bookId}
    </select>
    <select id="queryAllBook" resultType="Books">
        select * from ssmbuild.books
    </select>
</mapper>
3.3、resources
- mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--配置数据源,交给Spring-->
    <!--其别名-->
    <typeAliases>
        <package name="com.rainszj.pojo"/>
    </typeAliases>
    <!--注册Mapper-->
    <mappers>
        <mapper class="com.rainszj.dao"/>
    </mappers>
</configuration>
- database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
3.4、service包
- BookService
package com.rainszj.service;
import com.rainszj.pojo.Books;
import java.util.List;
public interface BookService {
    /**
     * 增加一本书
     *
     * @param books
     * @return
     */
    int addBook(Books books);
    /**
     * 删除一本书
     *
     * @param id
     * @return
     */
    int deleteBook(int id);
    /**
     * 修改一本书
     *
     * @param book
     * @return
     */
    int updateBook(Books book);
    /**
     * 根据 id 查询一本书
     *
     * @param id
     * @return
     */
    Books queryBookById(int id);
    /**
     * 查询所有的书
     *
     * @return
     */
    List<Books> queryAllBook();
}
- BookServiceImpl
package com.rainszj.service;
import com.rainszj.dao.BookMapper;
import com.rainszj.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService {
    // service层调dao层,使用组合dao
    private BookMapper bookMapper;
    // 使用Spring管理,实现set注入
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }
    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }
    public int deleteBook(int id) {
        return bookMapper.deleteBook(id);
    }
    public int updateBook(Books book) {
        return bookMapper.updateBook(book);
    }
    public Books queryBookById(int id) {
        return queryBookById(id);
    }
    public List<Books> queryAllBook() {
        return queryAllBook();
    }
}
4、Spring层编写
4.1、spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--1.关联数据库配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--2.连接池
        dbcp:半自动化操作(不能自动加载文件)
        c3p0:自动化操作(自动加载配置文件,并且自动设置到对象中)
        druid,hikari
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--配置连接池的属性-->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!--3.SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置mybatis配置文件:mybatis-config.xml-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.rainszj.dao"/>
    </bean>
</beans>
4.2、spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 1.扫描service相关的bean -->
    <context:component-scan base-package="com.rainszj.service"/>
    <!--2.将我们所有的业务类,注入到Spring中,可以通过配置或者注解实现-->
    <bean id="BookServiceImpl" class="com.rainszj.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
    <!--3.声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--4.aop事务支持-->
</beans>
5、Spring MVC层编写
5.1、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--注册DispatchServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--Spring MVC内置的过滤器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--Session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>
5.2、spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--1.注解驱动-->
    <mvc:annotation-driven/>
    <!--2.静态资源过滤-->
    <mvc:default-servlet-handler/>
    <!--3.自动扫描包:controller-->
    <context:component-scan base-package="com.rainszj.controller"/>
    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
底层代码编写完毕,下面只需要编写 Controller 层和 视图层。
6、查询书籍功能
6.1、BookController 类编写 , 方法一:查询全部书籍
@Controller
@RequestMapping("/book")
public class BookController {
    // Controller 层调 Service 层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    /**
     * 查询所有书籍
     * @param model
     * @return
     */
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }
}
6.2、编写首页 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
    <style>
        a {
            text-decoration: none;
            color: #000;
            font-size: 18px;
        }
        h3 {
            width: 180px;
            height: 38px;
            margin: 200px auto;
            text-align: center;
            line-height: 38px;
            background-color: deepskyblue;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<h3>
    <a href="${pageContext.request.contextPath}/book/allBook">点击到书籍列表</a>
</h3>
</body>
</html>
6.3、添加书籍列表页面 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.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
    </style>
</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">
                <thread>
                    <tr>
                        <th>书籍编号</th>
                        <th>书籍名称</th>
                        <th>书籍数量</th>
                        <th>书籍详情</th>
                    </tr>
                </thread>
                <tbody>
                    <c:forEach var="book" items="${requestScope.get('list')}">
                        <tr>
                            <td>${book.bookID}</td>
                            <td>${book.bookName}</td>
                            <td>${book.bookCounts}</td>
                            <td>${book.detail}</td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
7、添加书籍
7.1、BookController 类编写 , 方法二:添加书籍
/**
 * 跳转到修改页面,并根据 id回显数据
 *
 * @param id    Book id
 * @param model 传给前端的数据
 * @return
 */
@RequestMapping("/toUpdate/{bookId}")
public String toUpdatePaper(@PathVariable("bookId") int id, Model model) {
    Books book = bookService.queryBookById(id);
    model.addAttribute("QBook", book);
    System.out.println(book);
    return "updateBook";
}
/**
 * 处理修改请求
 *
 * @param book 前端传递的对象
 * @return
 */
@RequestMapping("/updateBook")
public String updateBook(Books book) {
    int res = bookService.updateBook(book);
    System.out.println(res);
    if (res > 0) {
        System.out.println("updateBook=>执行成功" + book);
    }
    return "redirect:/book/allBook";
}
7.2、在allBook.jsp中添加新增连接
<div class="row">
    <div class="col-md-4 column">
        <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
    </div>
</div>
7.3、添加书籍页面:addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加书籍</title>
    <%--BootStrap 美化界面--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <%--row clearfix 清除浮动--%>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>添加书籍</small>
                </h1>
            </div>
        </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="detail">书籍描述</label>
            <input type="text" name="detail" class="form-control" id="detail" required>
        </div>
        <input type="submit" class="form-control" value="添加">
    </form>
</div>
</body>
</html>
8、修改和删除书籍
8.1、BookController 类编写 , 方法三:修改书籍和删除书籍
    /**
     * 跳转到修改页面,并根据 id回显数据
     *
     * @param id    Book id
     * @param model 传给前端的数据
     * @return
     */
    @RequestMapping("/toUpdate/{bookId}")
    public String toUpdatePaper(@PathVariable("bookId") int id, Model model) {
        Books book = bookService.queryBookById(id);
        model.addAttribute("QBook", book);
        System.out.println(book);
        return "updateBook";
    }
    /**
     * 处理修改请求
     *
     * @param book 前端传递的对象
     * @return
     */
    @RequestMapping("/updateBook")
    public String updateBook(Books book) {
        int res = bookService.updateBook(book);
        System.out.println(res);
        if (res > 0) {
            System.out.println("updateBook=>执行成功" + book);
        }
        return "redirect:/book/allBook";
    }
    /**
     * 删除一本书
     * @param id
     * @return
     */
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBook(id);
        return "redirect:/book/allBook";
    }
8.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 美化界面--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <%--row clearfix 清除浮动--%>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>书籍列表 —————— 显示所有书籍</small>
                </h1>
            </div>
        </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 class="row clearfix">
        <div class="col-md-12 column">
            <%--
            table-hover:    鼠标滑过时,变色
            table-striped:  每一行显示不同的颜色
            --%>
            <table class="table table-hover table-striped">
                <thread>
                    <tr>
                        <th>书籍编号</th>
                        <th>书籍名称</th>
                        <th>书籍数量</th>
                        <th>书籍详情</th>
                        <th>操作</th>
                    </tr>
                </thread>
                <tbody>
                <c:forEach var="book" items="${requestScope.get('list')}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.detail}</td>
                        <td>
                            <%--传统方式--%>
<%--
                            <a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a>
                              |  
                            <a href="${pageContext.request.contextPath}/book/deleteBook?id=${book.bookID}">删除</a>
--%>
                            <%--RestFul风格--%>
                            <a href="${pageContext.request.contextPath}/book/toUpdate/${book.bookID}">修改</a>
                                  |  
                            <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
8.3、添加修改书籍页面 updateBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改书籍</title>
    <%--BootStrap 美化界面--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <%--row clearfix 清除浮动--%>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>修改书籍</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="${pageContext.request.contextPath}/book/updateBook" method="post">
        <%--提交隐藏域,用于提交要修改书的 id--%>
        <input type="hidden" name="bookID" value="${QBook.bookID}">
        <div class="form-group">
            <label for="bookName">书籍名称</label>
            <input type="text" name="bookName" class="form-control" id="bookName" value="${QBook.bookName}">
        </div>
        <div class="form-group">
            <label for="bookCounts">书籍数量</label>
            <input type="text" name="bookCounts" class="form-control" id="bookCounts" value="${QBook.bookCounts}">
        </div>
        <div class="form-group">
            <label for="detail">书籍描述</label>
            <input type="text" name="detail" class="form-control" id="detail" value="${QBook.detail}">
        </div>
        <input type="submit" class="form-control" value="修改">
    </form>
</div>
</body>
</html>
9、搜索功能
9.1、在allBook.jsp中添加查询书籍功能
    <div class="row">
        <div class="col-md-4 column">
            <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
            <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">查询所有书籍</a>
        </div>
        <div class="col-md-8 column">
            <%--查询书籍--%>
            <form action="${pageContext.request.contextPath}/book/queryBook" method="post" class="form-inline" style="float: right;">
                <span style="color: red;font-weight: bold;">${error}</span>
                <input type="text" class="form-control" name="queryBookName" placeholder="请输入要查询的书籍名称"
                       style="width: 300px;">
                <input type="submit" class="btn btn-primary" value="查询">
            </form>
        </div>
    </div>
9.2、在BookController类中编写处理查询书名的请求
/**
 * 根据书名查询一本书
 *
 * @param queryBookName
 * @param model
 * @return
 */
@RequestMapping("/queryBook")
public String queryBook(String queryBookName, Model model) {
    List<Books> list = bookService.queryBookByName(queryBookName);
    // System.err.println(list);
    if (list.isEmpty()) {
        list = bookService.queryAllBook();
        model.addAttribute("error", "未找到!");
    }
    model.addAttribute("list", list);
    return "allBook";
}
9.3、编写BookMapper接口
/**
 * 根据书名查询一本书
 *
 * @param bookName
 * @return
 */
List<Books> queryBookByName(@Param("bookName") String bookName);
9.4、在BookMapper.xml编写Sql
<select id="queryBookByName" resultType="Books">
    select * from ssmbuild.books where bookName = #{bookName}
</select>
9.5、编写BookServicer接口
/**
 * 根据书名查询一本书
 *
 * @param bookName
 * @return
 */
List<Books> queryBookByName(String bookName);
9.6、编写BookServicerImpl
public List<Books> queryBookByName(String bookName) {
    return bookMapper.queryBookByName(bookName);
}
10、项目结构:

11、注意事项
在 web.xml 中使用总的Spring配置文件!Spring MVC的内置过滤器要设置它的 encoding 属性!

CharacterEncodingFilter 源码中的 encoding 属性

注意静态资源过滤问题!

在项目的发布环境中添加lib依赖!

确保Spring Spring MVC 的配置文件在一个上下文中,将他们关联在一起!

MapperScannerConfigurer
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

SSM整合案例:图书管理系统的更多相关文章
- 08 SSM整合案例(企业权限管理系统):07.订单操作
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 07.订单操作 SSM订单操作 ... 
- 08 SSM整合案例(企业权限管理系统):05.SSM整合案例的基本介绍
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 05.SSM整合案例的基本介绍 ... 
- 08 SSM整合案例(企业权限管理系统):06.产品操作
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 06.产品操作 SSM 环境搭 ... 
- 08 SSM整合案例(企业权限管理系统):09.用户和角色操作
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 09.用户和角色操作 1. 用 ... 
- 08 SSM整合案例(企业权限管理系统):08.权限控制
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户操作 10.权限关联与控制 11.AOP日志 08.权限控制 SSM权限操作 ... 
- 08 SSM整合案例(企业权限管理系统):10.权限关联与控制
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户操作 10.权限关联与控制 11.AOP日志 10.权限关联与控制 1.用户 ... 
- 08 SSM整合案例(企业权限管理系统):11.AOP日志
		04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 11.AOP日志 1.数据库与 ... 
- SSM整合案例
		使用IDEA整合SSM spring核心配置文件:beans_core.xml/applicationContext.xml <?xml version="1.0" enco ... 
- SSM整合案例--用户登录
		实现用户登录案例,并进行非法拦截 实现当用户未登录时,无法跳转到出登录页面以外的任何页面,拦截用户仍在登陆页面:当用户登录成功即可跳转到其他页面 (1)导入依赖 <!-- https://mvn ... 
随机推荐
- AJ学IOS(02)UI之按钮操作 点击变换 移动 放大缩小 旋转
			不多说,先上图片看效果,AJ分享,必须精品 这个小程序主要实现点击方向键可以让图标上下左右动还有放大缩小以及旋转的功能,点击图片会显示另一张图片. 点击变换 其实用到了按钮的两个状态,再State C ... 
- python初学(三)
			1.以软科中国最好大学排名为分析对象,基于requests库和bs4库编写爬虫程序,对2015年至2019年间的中国大学排名数据进行爬取,并按照排名先后顺序输出不同年份的前10位大学信息,要求对输出结 ... 
- 手把手教Extjs-简单GridField示例讲解二
			使用的Extjs版本为4.2,示例是官方的版本,对里面的语法进行一句一句的学习研究.可以方便他人,又可以提升自己的理解.里面存在的问题,后期会一步一步改进.也欢迎各位指出. /* Extjs具有很庞大 ... 
- 记一个openwrt reboot异步信号处理死锁问题
			写在前面 觉得本页面排版单调的话,可以尝试到这里看. 问题背景 在 openwrt 上碰到了一个偶现的 reboot 失效问题.执行 reboot 之后系统并没有重启,此时控制台还能工作. 初步排查 ... 
- D. Points in rectangle
			D. Points in rectangle 单点时限: 2.0 sec 内存限制: 512 MB 在二维平面中有一个矩形,它的四个坐标点分别为(0,a),(a,0),(n,n−a),(n−a,n). ... 
- 不同目录有同名proto文件情况下,protoc生成.cc/.h
			首先先参考一下别人的博客,看完了,看懂了,再回过头来看我下面说的情况. 链接 https://blog.csdn.net/CAir2/article/details/78201572 但是这个也就是基 ... 
- CSRF(跨站请求伪造)学习总结
			前言 参考大佬的文章,附上地址 https://www.freebuf.com/articles/web/118352.html 什么是CSRF? CSRF,中文名字,跨站请求伪造,听起来是不是和XS ... 
- vue2.x学习笔记(一)
			使用vue开发项目已经过了一段时间了,对其中的很多东西还是一知半解,于是想要系统学习一下.主要内容是参照官方中文网站https://cn.vuejs.org/v2/guide/,然后加上一些自己的理解 ... 
- NCTF2019 小部分题解
			前言 礼拜五领航杯打的比较累,做不出WEB,D3CTF没用,做了NJCTF的一些题目(懒,睡觉到12点起) Misc 第一次比赛先去做misc,以前一直做WEB,以后要WEB+MISC做.礼拜六下午做 ... 
- SQL Server 之T-SQL基本语句 (3)
			继续来用例子总结sql基本语句用法. 在这里在建一个表:课 课程名 上课时间 数学 周一 数学 周二 数学 周三 语文 周一 语文 周二 英语 周一 数据分组:GROUP BY select 课程 ... 
