• SSM整合(详解)


    环境要求

    环境:

    • IDEA
    • MySQL 5.7.19
    • Tomcat 9
    • Maven 3.6

    要求:

    • 需要熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识;

    数据库环境

    DROP DATABASE
    IF EXISTS ssm;
    
    CREATE DATABASE ssm;
    USE ssm;
    
    CREATE TABLE books (
    	bookId INT (10) PRIMARY KEY auto_increment COMMENT "书id",
    	bookName VARCHAR (100) NOT NULL COMMENT '书名',
    	bookCounts INT (10) NOT NULL COMMENT '数量',
    	detail VARCHAR (200) NOT NULL COMMENT '描述'
    )ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    #ENGINE=InnoDB  数据库存储引擎
    #DEFAULT 默认
    #CHARSET=utf8 数据库字符编码
    
    INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
    (1,'Java',1,'从入门到放弃'),
    (2,'MySQL',10,'从删库到跑路'),
    (3,'Linux',5,'从进门到进牢');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    基本环境搭建

    一、导入相关的pom依赖!

    
        <dependencies>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.28version>
            dependency>
            
             <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.2.8version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.22version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>servlet-apiartifactId>
                <version>2.5version>
            dependency>
            <dependency>
                <groupId>javax.servlet.jspgroupId>
                <artifactId>jsp-apiartifactId>
                <version>2.2version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jstlartifactId>
                <version>1.2version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.7version>
            dependency>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>2.0.7version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>5.3.22version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    
        <build>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    二、完成MVC

    1、添加web程序

    右键项目–> 点击Add Framework Support(框架支持)–> 勾选第一个选项,点击ok!

    2、配置web.xml
    
    <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">
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
            
                <param-value>classpath:applicationContext.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <filter>
            <filter-name>encodingfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>utf-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    3、在资源包下创建spring-mvc.xml
    
    <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
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
    
        
        <context:component-scan base-package="com.jin.controller"/>
        
        <mvc:default-servlet-handler/>
        
        <mvc:annotation-driven/>
    
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    4、建立基本结构

    在这里插入图片描述.

    1)实体类(pojo)

    public class Book {
    
        private Integer bookID;
        private String bookName;
        private Integer bookCounts;
        private String detail;
     
    //            ..(有参无参)..
    //    ..(Setter和Getter方法)..
    //        ..(toString方法)..
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2)数据持久层(dao)

    //接口BookMapper
    public interface BookMapper {
        //增加一本书
        int addBook(Book book);
        //删除一本书
        int deleteBookById(int id);
    
        //更新一本书
        int updateBook(Book book);
    
        //查询一本书
        Book queryBookById(int id);
    
        //查询全部的书
        List<Book> queryBook();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    BookMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.jin.dao.BookMapper">
        <insert id="addBook" parameterType="Book">
            insert into ssm.books (bookName, bookCounts, detail)
            values (#{bookName},#{bookCounts},#{detail});
        insert>
    
        <delete id="deleteBookById" parameterType="int">
            delete
            from ssm.books
            where bookId = #{bookId};
        delete>
    
        <update id="updateBook" parameterType="Book">
            update ssm.books
            set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail}
            where bookId=#{bookId};
        update>
    
        <select id="queryBookById" resultType="Book" parameterType="int">
            select *
            from ssm.books where bookId = #{bookId};
        select>
    
        <select id="queryBook" resultType="Book">
            select *
            from ssm.books;
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    3)服务层 (service)

    package com.jin.service;
    
    import com.jin.pojo.Book;
    
    import java.util.List;
    
    public interface BookService {
        //增加一本书
        int addBook(Book book);
        //删除一本书
        int deleteBookById(int id);
    
        //更新一本书
        int updateBook(Book book);
    
        //查询一本书
        Book queryBookById(int id);
    
        //查询全部的书
        List<Book> queryBook();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    访问层接口实现(BookServiceImpl)

    package com.jin.service;
    
    import com.jin.dao.BookMapper;
    import com.jin.pojo.Book;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class BookServiceImpl implements BookService{
    
        //service层调dao层,组合Dao
      @Autowired
      private BookMapper bookMapper;
    
        @Override
        public int addBook(Book book) {
            return bookMapper.addBook(book);
        }
    
        @Override
        public int deleteBookById(int id) {
            return bookMapper.deleteBookById(id);
        }
    
        @Override
        public int updateBook(Book book) {
            return bookMapper.updateBook(book);
        }
    
        @Override
        public Book queryBookById(int id) {
            return bookMapper.queryBookById(id);
        }
    
        @Override
        public List<Book> queryBook() {
            return bookMapper.queryBook();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    4)业务层(controller)

    package com.jin.controller;
    
    import com.jin.pojo.Book;
    import com.jin.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;
    
    //@RestController = @ResponseBody + @Controller
    @Controller
    
    public class BookController {
        //controller 调 service 层
        @Autowired
        private BookService bookService;
    
        //查询全部的书籍,并且返回到一个书籍展示页面
        @RequestMapping("/a1")
        public String test(Model model){
    
            List<Book> list = bookService.queryBook();
            model.addAttribute("list", list);
            return "allBook";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    以上我们就完成了SpringMVC的基本操作!

    以下就是关于我们整合Mybatis-Spring-SpringMVC(SSM)框架

    三、整合SSM

    1、Mybatis框架(mybatis-config.xml)
    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        
        
        <typeAliases>
            <package name="com.jin.pojo"/>
        typeAliases>
    
        
        <mappers>
            <mapper class="com.jin.dao.BookMapper"/>
        mappers>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    2、Spring整合MyBatis (spring-dao.xml)
    
    <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">
    
        
        <context:property-placeholder location="classpath:db.properties"/>
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}">property>
            <property name="url" value="${jdbc.url}">property>
            <property name="username" value="${jdbc.username}">property>
            <property name="password" value="${jdbc.password}">property>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        bean>
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            
            <property name="basePackage" value="com.jin.dao"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    3、数据库配置文件 (db.properties)
    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/one_class?useSSL=false&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=123456
    
    • 1
    • 2
    • 3
    • 4
    4、spring-service.xml
    
    <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">
    
        
        <context:component-scan base-package="com.jin.service"/>
    
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            
            <property name="dataSource" ref="dataSource"/>
        bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    5、Spring配置整合文件 (applicationContext.xml)
    
    <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">
    
        <import resource="classpath:spring-dao.xml"/>
        <import resource="classpath:spring-mvc.xml"/>
        <import resource="classpath:spring-service.xml"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    四、显示页面

    1、首页(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: 280px;
                height: 48px;
                margin: 200px auto;
                text-align: center;
                line-height: 48px;
                background: deepskyblue;
                border-radius: 5px;
            }
        style>
    head>
    <body>
     <h3>
         <a href="${pageContext.request.contextPath}/a1">进入书籍页面a>
     h3>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    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>
        <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/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.detail}td>
                        tr>
                    c:forEach>
                    tbody>
                table>
    
            div>
        div>
    div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    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>
        <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/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}/toAdd">新增书籍a>
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/toAll">显示所有书籍a>
                div>
                <div class="col-md-8 column">
                    <form class="form-inline" action="${pageContext.request.contextPath}/queryBook" method="post" style="float: right">
    
                        <input type="text" class="form-control" name="bookName" placeholder="请输入你要查询的书籍名称" required>
                        <input type="submit" value="查询" class="btn btn-primary">
                    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.detail}td>
                            <td>
    
                                <a href="${pageContext.request.contextPath}/toUpdate?id=${book.bookID}">修改a>
                                 | 
                                <a href="${pageContext.request.contextPath}/toDelete?id=${book.bookID}">删除a>
                            td>
                        tr>
                    c:forEach>
                    tbody>
                table>
    
            div>
        div>
    div>
    
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    addBook.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>新增书籍title>
        <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/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>
    
            <form method="post" action="${pageContext.request.contextPath}/addBook">
                <div class="form-group">
                    <label>书籍名称:label>
                    <input type="text" class="form-control" name="bookName"  required>
                div>
                <div class="form-group">
                    <label>书籍数量:label>
                    <input type="text" class="form-control" name="bookCounts"  required>
                div>
                <div class="form-group">
                    <label>书籍详情:label>
                    <input type="text" class="form-control" name="detail" required>
                div>
                <div class="form-group">
                    <input type="submit" class="form-control" value="添加">
                div>
    
    
        form>
    div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    updateBook.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>修改书籍title>
        <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/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>
    
        <form method="post" action="${pageContext.request.contextPath}/updateBook">
            <div class="form-group">
                <input type="hidden"  name="bookID" value="${qBook.bookID}">
                <label>书籍名称:label>
                <input type="text" class="form-control" value="${qBook.bookName}" name="bookName" required>
            div>
            <div class="form-group">
                <label>书籍数量:label>
                <input type="text" class="form-control"  value="${qBook.bookCounts}" name="bookCounts"  required>
            div>
            <div class="form-group">
                <label>书籍详情:label>
                <input type="text" class="form-control"  value="${qBook.detail}" name="detail" required>
            div>
            <div class="form-group">
                <input type="submit" class="form-control" value="修改">
            div>
    
        form>
    div>
    
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    业务层 (BookController.java)

    @Controller
    public class BookController {
        //controller 调 service 层
        @Autowired
        private BookService bookService;
    
    
        //查询全部的书籍,并且返回到一个书籍展示页面
        @RequestMapping("/toAll")
        public String test(Model model) {
    
            List<Book> list = bookService.queryBook();
            model.addAttribute("list", list);
            return "allBook";
        }
    
        //跳转到增加书籍页面
        @RequestMapping("/toAdd")
        public String test2() {
            return "addBook";
        }
    
        //添加书籍的请求
        @RequestMapping("/addBook")
        public String test3(Book book) {
            bookService.addBook(book);
            return "redirect:/toAll";//重定向到 @RequestMapping("/toAll")
        }
    
        //跳转到修改书籍页面
        @RequestMapping("/toUpdate")
        public String test4(int id, Model model) {
            Book book = bookService.queryBookById(id);
            model.addAttribute("qBook", book);
            return "updateBook";
        }
    
        //修改书籍的请求
        @RequestMapping("/updateBook")
        public String test5(Book book) {
            System.out.println("update=>" + book);
            bookService.updateBook(book);
    
            return "redirect:/toAll";
        }
    
        //删除书籍
        @RequestMapping("/toDelete")
        public String test6(int id) {
            bookService.deleteBookById(id);
            return "redirect:/toAll";
        }
    
        //查询书籍
        @RequestMapping("/queryBook")
        public String test7(String bookName, Model model) {
            Book book = bookService.queryBookName(bookName);
           List<Book> list = new ArrayList<>();
            list.add(book);
            model.addAttribute("list", list);
            return "allBook";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    一文彻底解析数据库设计思路
    目标检测算法——工业缺陷数据集汇总2(附下载链接)
    代码随想录31——贪心:贪心算法理论基础、455分发饼干、376摆动序列
    Jmeter接口自动化(七)后置处理器
    在数字海洋中驶向成功:公众号关键词排名优化的旅程
    Java实现从Redis中批量读取数据
    安装DevEco Studio 3.0 Beta2
    [附源码]计算机毕业设计JAVA放肆游旅游网
    深入剖析Spring框架:循环依赖的解决机制
    Altium Designer如何查看制定了哪些快捷键?
  • 原文地址:https://blog.csdn.net/weixin_45737330/article/details/126896775