2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统

11/13
记录之前的SSM+Tomcat搭建简单的图书管理系统实现增删改查
本小项目参考狂神说的博客,自己写代码实现

工具:Navicat Premium 15
IDEA
Tomcat 8.5
Mysql
Maven
首先是数据库建表:

CREATE DATABASE `ssmbuild`;
 
USE `ssmbuild`;
 
DROP TABLE IF EXISTS `books`;
 
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` VARCHAR(200) NOT NULL COMMENT '描述',
  KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
 
INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES 
(1,'Java',1,'从入门到放弃'),
(2,'Linux',5,'从进门到进牢'),
(3,'jdk1.8',3,'jvm是人间痛苦'),
(4,'数据结构与算法基础',5,'我起了一题秒了有什么好说的');

实现如下:
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第1张图片
接下来是导入项目依赖,通过创建maven项目的pom.xml文件来导入

<dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
    
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.18.12version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>0.2.15version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.47version>
    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>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.17version>
    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>

编写资源过滤

<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>
写出项目的主题框架!
controller层
dao层
pojo层
service层
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020111314543837.png#pic_center)然后建立存储资源空间resources

resources里面有mybatis的核心文件,spring的核心文件,实现日志的核心文件
分别是mybatis-config.xml
applicationContext.xml
log4j.properties
database.properties(数据库资源路径)
先写出database.properties

jdbc.drvier=com.mysql.jdbc.Driver(jdbc驱动加载)
jdbc.url=jdbc:mysql://localhost:xxxx(你的端口,一般为3306)/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=数据库登录名
jdbc.password=你自身的数据库连接密码

然后是log4j.properties的编写

#将等级得debug的日志信息输出到console和file中两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#文件输出的相关设置
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/lzf.log
#文件的地址
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

编写mybati的核心配置文件(模板)



applicationContext.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">
beans>

写完这些后,目录架构应该是这样的:
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第2张图片
然后接下来写各种框架需求,首先是mybatis框架
大体思路:先编写实体类,然后dao层的mapper接口和mapper.xml,最后编写mybatis的核心配置文件
实体类pojo:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
     
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;

}
//使用lombok注解

mapper接口

import java.util.List;

public interface BookMapper {
     
    //增加
    int addBook(Books book);
    //删除
    int deleteBookById(int id);
    //更新
    int updateBook(Books books);
    //查询
    Books queryBookById(int id);
    //查询全部
    List<Books> queryAllBook();
}

mapper接口的实现类BookMapperImpl

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BookMapperImpl implements BookMapper{
     

    @Autowired
    @Qualifier("sqlSession")
    private SqlSessionTemplate sqlSession;


    @Override
    public int addBook(Books book) {
     
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.addBook(book);
    }

    @Override
    public int deleteBookById(int id) {
     
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.deleteBookById(id);
    }

    @Override
    public int updateBook(Books books) {
     
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.updateBook(books);
    }

    @Override
    public Books queryBookById(int id) {
     
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.queryBookById(id);
    }

    @Override
    public List<Books> queryAllBook() {
     
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.queryAllBook();
    }
}

编写BookMapper.xml,这个是重点内容



<mapper namespace="com.lzf.dao.BookMapper">
    <insert id="addBook" parameterType="Books">
        insert into ssmbuild.books(bookName,bookCounts,detail)
        value (#{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="queryAllBook" resultType="Books">
        select * from ssmbuild.books
    select>
mapper>

注意传入参数和返回参数类型,还有对应好mapper接口的方法名
到这里,再写完mybatis-config.xml,mybatis的框架就写完了



<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J"/>//日志存放类型
    settings>
    <typeAliases>
        <package name="com.lzf.pojo"/>//实体类的路径
    typeAliases>
    <mappers>
        <mapper class="com.lzf.dao.BookMapper"/>//Mapper接口路径
    mappers>
configuration>

写完mybatis框架之后接下来就是spring的框架,首先从spring-service实现业务开始。
也就是从service层开始
编写业务的接口,存放业务的抽象方法

import org.springframework.stereotype.Service;

import java.util.List;


public interface BookService {
     
    //增加一个Book
    int addBook(Books book);

    //根据id删除一个Book
    int deleteBookById(int id);

    //更新Book
    int updateBook(Books books);

    //根据id查询,返回一个Book
    Books queryBookById(int id);

    //查询全部Book,返回list集合
    List<Books> queryAllBook();
}

写出接口的实现类,实现业务

import com.lzf.dao.BookMapperImpl;
import com.lzf.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BookServiceImpl implements BookService{
     

    @Autowired
    @Qualifier("bookMapperImpl")
    private BookMapperImpl bookMapper;

    @Override
    public int addBook(Books book) {
     
        return bookMapper.addBook(book);
    }

    @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> queryAllBook() {
     
        return bookMapper.queryAllBook();
    }
}

这里注意的是,service层实现的业务也与dao层中的mapper功能有所联系
写完service层之后,接下来就要给dao层和service层注入bean
首先在spring-dao.xml和spring-service.xml中编写
spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       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:annotation-config/>
    <context:component-scan base-package="com.lzf.dao"/>

    <!--DateSource:使用spring的数据源替换mybatis的设置:c3p0、dbcp、druid-->
    <!--spring自带数据源-->
    <!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
    <!--        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
    <!--        <property name="url" value="jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>-->
    <!--        <property name="username" value="root"/>-->
    <!--        <property name="password" value="123456"/>-->
    <!--    </bean>-->
    <!-- JNDI DataSource for J2EE environments -->

    <!--读取数据库配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--druid数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <property name="maxActive" value="20"/>
        <property name="initialSize" value="1"/>
        <property name="maxWait" value="60000"/>
        <property name="minIdle" value="1"/>

        <property name="timeBetweenEvictionRunsMillis" value="3000"/>
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="validationQuery" value="SELECT * FROM ssmbuild.books"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <!-- mysql 不支持 poolPreparedStatements-->
        <!--<property name="poolPreparedStatements" value="true" />-->
        <!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />-->
        <!-- 开启Druid的监控统计功能 -->
        <property name="filters" value="stat"/>

    </bean>


    <!--C3P0数据源-->
    <!--    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
    <!--        &lt;!&ndash; 配置连接池属性 &ndash;&gt;-->
    <!--        <property name="driverClass" value="${jdbc.driver}"/>-->
    <!--        <property name="jdbcUrl" value="${jdbc.url}"/>-->
    <!--        <property name="user" value="${jdbc.username}"/>-->
    <!--        <property name="password" value="${jdbc.password}"/>-->

    <!--        &lt;!&ndash; c3p0连接池的私有属性 &ndash;&gt;-->
    <!--        <property name="maxPoolSize" value="30"/>-->
    <!--        <property name="minPoolSize" value="10"/>-->
    <!--        &lt;!&ndash; 关闭连接后不自动commit &ndash;&gt;-->
    <!--        <property name="autoCommitOnClose" value="false"/>-->
    <!--        &lt;!&ndash; 获取连接超时时间 &ndash;&gt;-->
    <!--        <property name="checkoutTimeout" value="10000"/>-->
    <!--        &lt;!&ndash; 当获取连接失败重试次数 &ndash;&gt;-->
    <!--        <property name="acquireRetryAttempts" value="2"/>-->
    <!--    </bean>-->


    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--mybatis的核心配置文件,但是可以不写-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--相当于mappe package 添加mybatis.xml配置文件-->
        <!--mybatis配置文件中能配置的在这都能配置-->
    </bean>

    <!--SqlSessionTemplate:就是SqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为SqlSessionTemplate没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

</beans>

这里有一个spring的高频面试题,顺带记忆一下!

BeanFactory:

BeanFactory是接口,给具体的IOC容器的实现提供了规范,它是负责创建和管理bean的一个工厂。

FactoryBean:

FactoryBean也是接口,主要用于返回BeanFactory生产好的bean对象,如果要获取FactoryBean对象,请在id前面加一个&符号来获取。

BeanFactory和ApplicationContext的区别是什么:

BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责bean的声明周期。

然后是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.lzf.service"/>
    
    <context:annotation-config/>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource" />
    bean>


beans>

然后就是springmvc的框架
先写web层


<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>DispatcherServletservlet-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>DispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>
      org.springframework.web.filter.CharacterEncodingFilter
    filter-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>


  <servlet>
    <servlet-name>DruidStatViewservlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>DruidStatViewservlet-name>
    <url-pattern>/druid/*url-pattern>
  servlet-mapping>

web-app>

spring-mvc.xml编写


<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"
       xmlns="http://www.springframework.org/schema/beans"
       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
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    
    <mvc:annotation-driven/>
    
    <mvc:default-servlet-handler/>
    
    <context:component-scan base-package="com.lzf.controller"/>


    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>


beans>

整合spring的bean
applicationContext文件编写


<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">
    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
beans>

编写controller层

import com.lzf.service.BookServiceImpl;
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 {
     
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookServiceImpl bookService;

    @RequestMapping("/allBook")
    public String allBook(Model model){
     
        List<Books> books1 = bookService.queryAllBook();
        System.out.println("查询书籍列表");
        for (Books books : books1) {
     
            System.out.println(books);
        }
        model.addAttribute("list",books1);
        return "allBook";
    }
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
     
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addPaper(Books books) {
     
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
     
        Books books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("book",books );
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model, Books book) {
     
        System.out.println(book);
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
     
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
}

接下来就是页面的可视化实现
首先是首页index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
    <title>首页</title>
    <style type="text/css">
        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: 4px;
        }
    </style>
</head>
<body>

<h3>
    <a href="${pageContext.request.contextPath}/book/allBook">点击进入列表页</a>
</h3>
</body>
</html>

然后就是各种功能的视图实现
添加:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>新增书籍</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <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/addBook" method="post">
        书籍名称:<input type="text" name="bookName"><br><br><br>
        书籍数量:<input type="text" name="bookCounts"><br><br><br>
        书籍详情:<input type="text" name="detail"><br><br><br>
        <input type="submit" value="添加">
    </form>

</div>

更新:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <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">
        <input type="hidden" name="bookID" value="${book.getBookID()}"/>
        书籍名称:<input type="text" name="bookName" value="${book.getBookName()}"/>
        书籍数量:<input type="text" name="bookCounts" value="${book.getBookCounts()}"/>
        书籍详情:<input type="text" name="detail" value="${book.getDetail() }"/>
        <input type="submit" value="提交"/>
    </form>

</div>

查询:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书籍列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <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">
            <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 class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>书籍编号</th>
                    <th>书籍名字</th>
                    <th>书籍数量</th>
                    <th>书籍详情</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody>
                <c:forEach var="book" items="${requestScope.get('list')}">
                    <tr>
                        <td>${
     book.getBookID()}</td>
                        <td>${
     book.getBookName()}</td>
                        <td>${
     book.getBookCounts()}</td>
                        <td>${
     book.getDetail()}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}">更改</a> |
                            <a href="${pageContext.request.contextPath}/book/del/${book.getBookID()}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>

至此,SSM就搭建完成了,总体来说就是比较繁琐,都是按部就班的执行流程,SSM的缺点也应该是这样吧,不过这样也有好处,就是思路比较清晰,最后操作就是在idea中链接数据库和启动Tomcat。
链接数据库:
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第3张图片
选择mysql
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第4张图片
接下来就是启动Tomcat
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第5张图片在deployment中选择添加新的war包之后,命名路径,就可以启动Tomcat测试项目成果了!
测试如下:

首页:
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第6张图片
查询功能:
2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第7张图片
更新功能:2020,基于狂神说的SSM+TOMCAT整合实现简单的图书管理系统_第8张图片
在这里为止,这个SSM搭建算是完成了。这次是我除了pom文件和模板是复制之外,其余的都是自己手敲的。怎么说呢,中间也有报过错,例如找不到bean,其实是我粗心大意,把bean的返回类型填写错误,如果tomcat启动网页有报错的话,还是参考报错内容一一排查,不要嫌麻烦,排查也是一种学习。

你可能感兴趣的:(学习,java)