• SSM框架速成4:图书馆管理系统(SSM实战)


    学完了三个框架,现在来整合一下SSM,并且实现一个图书馆增删改查的简单项目。
    这个整合就是实现简单的CRUD,整合的过程中多复习有关于之前的一些知识和思想,本身并不难,只是可能再配置的过程中会出现很多问题,在SSM框架学完就立刻整合项目也算是锻炼自己的能力了。

    之后还会再整合一个搜索的功能,这个功能会在本次的实战基础上修改代码,待更新中…

    数据库环境

    创建一个存放书籍数据的数据库表:

    CREATE DATABASE `ssmbuild`;
     
    USE `ssmbuild`;
     
    CREATE TABLE `books` (
      `bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
      `bookName` VARCHAR(100) NOT NULL COMMENT '书名',
      `bookCounts` INT(11) NOT NULL COMMENT '数量',
      `detail` VARCHA`books`R(200) NOT NULL COMMENT '描述',
      KEY `bookID` (`bookID`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
     
    INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES 
    (1,'Java',1,'从入门到放弃'),
    (2,'MySQL',10,'从删库到跑路'),
    (3,'Linux',5,'从进门到进牢');
    
    DROP TABLE IF EXISTS `books`;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    除了最后一句其他都运行,成功建表:
    在这里插入图片描述

    基本环境搭建

    1、新建一个普通的maven项目
    2、添加web的支持,且给web手动添加lib依赖
    3、导入相关的pom依赖

    <dependencies>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.11version>
        dependency>
        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5.2version>
        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.2version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.2version>
        dependency>
     
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.1.9.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.1.9.RELEASEversion>
        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
    • 60
    • 61

    4、添加Maven的资源过滤设置

    <build>
            <resources>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                resource>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
            resources>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    5、连接数据库(记得Driver调整为5.0版本的,具体看之前的有关于mybatis的文章)
    在这里插入图片描述
    6、构建基本结构和框架:
    (1)载java包下创建4个package
    com.wang.pojo
    com.wang.service
    com.wang.dao
    com.wang.controller

    (2)在resources下创建applicationContext.xml、mybatis-config.xml并配置
    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">
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    mybatis-config.xml:

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Mybatis层

    1、在resources包下创建数据库配置文件database.properties:

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    jdbc.username=root
    jdbc.password=123456
    
    • 1
    • 2
    • 3
    • 4

    如果你的MySQL是5.7版本的,那么就需要把cj给去掉

    2、在pojo包下新建实体类,这个实体类对照数据库,即Books,里面的属性要和数据库上的属性名完全一样,否则有时候可能会报空指针异常

    当然,类里面也是需要构造函数的,可以自己写构造方法,也可以利用Lombok,然后用注解进行参数的构造,先导入相应的pom依赖:

    lombok依赖

    <dependency>
     	<groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.18.10version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Books类:

    package com.wang.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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3、接下来该写接口了,在dao层下接口BookMapper,分别封装增删改查的方法:

    package com.wang.dao;
    
    import com.wang.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface BookMapper {
        //增加一本书
        int addBook(Books books);
        //删除一本书
        int deleteBookById(@Param("bookID") int id);
        //更新一本书
        int updateBook(Books books);
        //查询一本书
        Books queryBookById(@Param("bookID") int id);
        //查询全部的书
        List<Books> queryAllBooks();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    4、学了mybatis以后,实现这些接口的方法直接用Mapper.xml来做,在dao包下面新建BooksMapper.xml:

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.wang.dao.BookMapper">
    
        <insert id="addBook" parameterType="Books">
            insert into ssmbuild.books (bookName, bookCounts, detail)
            values (#{bookName},#{bookCounts},#{detail})
        insert>
        
        <delete id="deleteBookById" 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="queryAllBooks" resultType="Books">
            select * from ssmbuild.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
    • 32
    • 33

    5、编写mybatis的核心配置文件,取别名,并进行mapper的注册:

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        
        <typeAliases>
            <package name="com.wang.pojo"/>
        typeAliases>
        <mappers>
            <mapper class="com.wang.dao.BookMapper"/>
        mappers>
    
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    6、开始写service层,新建接口和Impl实现类,把dao注入到其中,然后调用方法,具体如下:
    (1)BookService接口:

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

    (2)BookServiceImpl类:

    package com.wang.service;
    
    import com.wang.dao.BookMapper;
    import com.wang.pojo.Books;
    
    import java.util.List;
    
    public class BookServiceImpl implements BookService{
    
        //service层调dao层,组合dao
        private BookMapper bookMapper;
    
        public void setBookMapper(BookMapper bookMapper) {
            this.bookMapper = bookMapper;
        }
    
        @Override
        public int addBook(Books books) {
            return bookMapper.addBook(books);
        }
    
        @Override
        public int deleteBookById(int id) {
            return bookMapper.deleteBookById(id);
        }
    
        @Override
        public int updateBook(Books books) {
            return bookMapper.updateBook(books);
        }
    
        @Override
        public Books queryBookById(int id) {
            return bookMapper.queryBookById(id);
        }
    
        @Override
        public List<Books> queryAllBooks() {
            return bookMapper.queryAllBooks();
        }
    }
    
    
    • 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

    到这里,终于把Mybatis层配完了

    Spring层

    1、在resources下新建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:database.properties"/>
    
        
        <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}"/>
    
            
            <property name="maxPoolSize" value="30"/>
            <property name="minPoolSize" value="10"/>
            
            <property name="autoCommitOnClose" value="false"/>
            
            <property name="checkoutTimeout" value="10000"/>
            
            <property name="acquireRetryAttempts" value="2"/>
    
        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.wang.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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    2、整合service层,因为service层有个很重要的东西:事务。
    在resources下新建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.wang.service"/>
    
        
        
        <bean id="BookServiceImpl" class="com.wang.service.BookServiceImpl">
            <property name="bookMapper" ref="bookMapper"/>
        bean>
    
        
        <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
    • 21
    • 22
    • 23
    • 24
    • 25

    3、最后再applicationContext.xml中导入两个配置:

    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    
    • 1
    • 2

    SpringMVC层

    1、首先这已经是一个有web框架支持的项目了,那么我们就直接在web.xml中配置DispatchServlet以及解决乱码的过滤:

    
    <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>encodingFilterfilter-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>encodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
        
        <session-config>
            <session-timeout>15session-timeout>
        session-config>
    
    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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    2、在resources下新建spring-mvc.xml:

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        
        <mvc:annotation-driven/>
        
        <mvc:default-servlet-handler/>
        
        <context:component-scan base-package="com.wang.controller"/>
        
        <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

    最后记得在applicationContext.xml中导入这个resource:

    <import resource="classpath:spring-mvc.xml"/>
    
    • 1

    3、最后一步只需要在WEB-INF下面新建一个jsp的包即可,整个项目的配置到现在构建完毕。

    这个最好能够保存下来,以后这个就可以作为做项目的底层了!

    测试

    我认为这是很重要的一部分,要锻炼自己的纠错能力,那么我们需要进行一下测试:
    (1)在controller包下新建BookController类:

    package com.wang.controller;
    
    import com.wang.pojo.Books;
    import com.wang.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.queryAllBooks();
            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

    (2)直接在WEB-INF下新建jsp的包,新建allBook.jsp:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>书籍展示页面title>
    head>
    <body>
    <h1>书籍展示h1>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (3)修改一下index.jsp的内容,这个页面即为首页,我们用页面跳转来访问allBook.jsp的内容:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>首页title>
      head>
      <body>
      <h3>
        <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面a>
      h3>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    正确的运行结果应该为:
    在这里插入图片描述
    点击链接后,页面成功跳转:
    在这里插入图片描述

    报错解决思路

    1、查看有没有lib包且导入全部依赖
    2、查看bean是否注入成功
    3、Junit测试单元,看我们的代码能否查询出结果:
    在test-java中新建MyTest类:

    import com.wang.pojo.Books;
    import com.wang.service.BookService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest{
        @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            BookService bookServiceImpl = (BookService) context.getBean("BookServiceImpl");
            for (Books books : bookServiceImpl.queryAllBooks()) {
                System.out.println(books);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    4、那么可以知道出问题的是Spring,因为底层的东西并没有报错,正常返回正确的结果,接下来只需要检查web.xml文件中的servlet初始化是否有问题,init-param标签内的value值改为:classpath:applicationContext.xml即可。

    查询书籍功能

    我们接着要开始写各个功能,并且要返回出好看的让客户看的可视化界面,那么首先先修改一下首页的样式,我们可以修改h3和a的style样式:

    <style>
          a{
            text-decoration: none;
            color:black;
            font-size:18px;
          }
          h3{
            width: 180px;
            height: 38px;
            margin: 200px auto;
            text-align: center;
            line-height: 38px;
            background: deepskyblue;
            border-radius: 5px;
          }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    接着我们要开始编写allBook.jsp页面,利用BootStrap来优化界面,并且真正实现显示全部书籍。

    该代码为:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>书籍展示页面</title>
    
        <%--BootSrap美化界面--%>
        <link href="https://cdn.bootcss.com/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="clo-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
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    添加书籍功能

    后序的功能和之前的原理差不多,首先我们的首页都必须含有各功能的链接的跳转,要跳转的内容我们在controller层进行编程,即面向controller层编程。

    (1)先向allBook.jsp内加上代码:

    <div class="row">
            <div class="col-md-4 column">
                <%--toAddBook--%>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
            </div>
    </div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、直接面向controller层编程,既要能够实现新增书籍,还需要增加书籍以后要能够跳转回全部书籍页面,利用重定向即可实现:

    //跳转到增加书籍页面:
        @RequestMapping("/toAddBook")
        public String toAddPaper(){
            return "addBook";
        }
    
        //添加书籍的请求
        @RequestMapping("/addBook")
        public String addBook(Books books){
            //System.out.println();
            bookService.addBook(books);
            return "redirect:/book/allBook";//重定向到我们的@RequestMapping("/allBook")
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3、我们只需要在addBook.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>
    
        <%--BootSrap美化界面--%>
        <link href="https://cdn.bootcss.com/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">
            <div class="col-md-4 column">
                <%--toAddBook--%>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
            </div>
        </div>
    
        <div class="row clearfix">
            <div class="clo-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
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    功能简单,也就是加了一本书而已,页面重定向成功且数据库中的表确实有新增的书籍,成功。

    修改删除书籍

    也就是在总的页面的table控件中新增一下修改和删除的选项,但是我们需要注意的是,当我们在修改额删除书籍的时候,由于书的ID是隐藏的,也就是说并没有这么一个传值的过程,那么我们就需要利用到隐藏域的知识,用隐藏域的手段来实现传入ID值,最后实现功能。

    BookController.java:

    	//跳转到修改页面
        @RequestMapping("/toUpdateBook")
        public String toUpdateBook(Model model, int id) {
            Books books = bookService.queryBookById(id);
            //System.out.println(books);
            model.addAttribute("QBook",books);
            return "updateBook";
        }
    
        //响应修改的请求
        @RequestMapping("/updateBook")
        public String updateBook(Books books) {
            //System.out.println(book);
            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";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    allbook.jsp的table将会写入操作的连接:

    <td>
       <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">更改</a> |
       <%--这里将使用RestFul风格--%>
       <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a>
    </td>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    别忘了表头tr也得在前面写上。

    updateBook.jsp:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>修改书籍</title>
    
        <%--BootSrap美化界面--%>
        <link href="https://cdn.bootcss.com/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 action="${pageContext.request.contextPath}/book/updateBook" method="post">
    
            <%-- 可能出现的问题是即便提交了修改的SQL请求,但是修改失败,可以先考虑是事务的问题,配置完毕后依旧失败 --%>
            <%-- 那么看一下SQL语句能否执行成功,发现无法成功,因为没传入ID --%>
            <%-- 那么我们利用前端来传递隐藏域 --%>
            <input type="hidden" name="bookID" value="${QBook.bookID}">
    
            <div class="form-group">
                <label>书籍名称:</label>
                <input type="text" name="bookName" class="form-control" value="${QBook.bookName}" required>
            </div>
            <div class="form-group">
                <label>书籍数量:</label>
                <input type="text" name="bookCounts" class="form-control" value="${QBook.bookCounts}" required>
            </div>
            <div class="form-group">
                <label>书籍描述:</label>
                <input type="text" name="detail" class="form-control" value="${QBook.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
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    增删改查视频效果

    图书馆系统增删改查

  • 相关阅读:
    C++11之智能指针
    MySQL初始化之后启动报错(mysqld: Table ‘mysql.plugin‘ doesn‘t exist)
    【MMCV】MMCV安装与踩坑
    以赛促练-力扣第86场双周赛反思(持续更新中)
    DoLa:对比层解码提高大型语言模型的事实性
    第7/100天 阅读笔记
    ERROR: [Place 30-675]
    Linux 进程管理之current
    视频监控智能识别
    递归查询实现
  • 原文地址:https://blog.csdn.net/m0_52380556/article/details/127759554