其他分享
首页 > 其他分享> > SSM整合

SSM整合

作者:互联网

SSM整合

1、整合部分

1.1、Mybatis层

首先建立对应的数据库和表

-- 创建数据库ssmbuild
CREATE DATABASE ssmbuild

-- 使用数据库ssmbuild
USE ssmbuild

-- 检查数据库中是否存在表books, 如果存在就删除
DROP TABLE IF EXISTS books

-- 新建表books bookID为主键
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

-- 向books表中插入三条数据
INSERT INTO books(bookID,bookName,bookCounts,detail) VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

搭建基本环境

  1. 创建一个新的Maven项目ssmbuild

  2. 导入依赖与解决静态资源导出问题

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hassder</groupId>
    <artifactId>ssmbuild</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依赖问题: junit, 数据库驱动, 连接池 , servlet , jsp , mybatis , mybatis-spring , spring-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.19.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.19.RELEASE</version>
        </dependency>
        
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

    </dependencies>

    <!--静态资源导出问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>
  1. 连接数据库与完成基本结构:

在这里插入图片描述

  1. 关联数据库 创建database.properties文件:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username=root
password=123456

本来在创建database.properties文件后, 应该在mybatis-config.xml中配置数据源, 但是这一部分可以交给Spring去做

  1. 在mybatis-config.xml开启别名处理器(自动取别名)
<typeAliases>
    <package name="com.hassder.pojo"/>
</typeAliases>
  1. 编写实体类Books
package com.hassder.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. 编写mapper(dao)层接口:
package com.hassder.dao;

import com.hassder.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. 为mapper(dao)层接口编写xml文件(以往的实现类)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hassder.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 bookCounts = #{bookCounts} , bookName = #{bookName} ,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. 在mybatis-config.xml文件中注册mapper
<!--注册mapper-->
<mappers>
    <mapper class="com.hassder.dao.BookMapper"/>
</mappers>
  1. 编写service层接口:
package com.hassder.service;

import com.hassder.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BooksService {

    // 增加一本书
    int addBook(Books books) ;

    // 删除一本书
    int deleteBookById(int id) ;

    // 更新一本书
    int updateBook(Books books) ;

    // 查询一本书
    Books queryBookById(int id) ;

    // 查询全部的书
    List<Books> queryAllBooks() ;

}
  1. 编写service层实现类
package com.hassder.service;

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

import java.util.List;

public class BookServiceImpl implements BooksService{

    // service层调dao层: 组合dao层
    private BookMapper bookMapper ;

    // 提供set方法
    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();
    }
}

到这里, Mybatis层基本搭建完毕

1.2、Spring层

1.2.1 Spring整合dao层

在resources中新建一个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. 连接池-->
    <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的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--4. 配置dao接口扫描包, 动态实现Dao接口注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入SqlSessionFactory value值与上面SqlSessionFactory配置的bean的id值相同即可-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!--要扫描的dao包-->
        <property name="basePackage" value="com.hassder.dao"/>
    </bean>

</beans>

1.2.2 Spring整合service层

在resources中新建一个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下的包-->
    <context:component-scan base-package="com.hassder.service"/>

    <!--2. 将我们的所有业务类, 注入到Spring,可以通过配置,或者注解来实现-->
    <bean id="BookServiceImpl" class="com.hassder.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>

注意:

<?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">

    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    
</beans>

我们可以使用import标签, 通过设置resource值的方式来将其手动关联起来

至此, Spring层整合基本结束

1.3、SpringMVC层

  1. 添加Web支持, 使这个项目变成web项目
  2. 配置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>

    <!--乱码过滤-->
    <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>
  1. 配置spring-mvc.xml, 并在WEB-INF下建立jsp包
<?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: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
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!--<import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>-->

    <!--1. 注解驱动-->
    <mvc:annotation-driven/>

    <!--2. 静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--3. 扫描包controller-->
    <context:component-scan base-package="com.hassder.controller"/>

    <!--4. 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

至此, 所有整合完毕 现结构如图:

在这里插入图片描述

2、项目部分(包含前端页面)

2.1、查询书籍功能

2.1.1 查询所有书籍

新建BookController类 如下:

package com.hassder.controller;

import com.hassder.pojo.Books;
import com.hassder.service.BooksService;
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层
    // 注入service
    @Autowired
    @Qualifier("BookServiceImpl")
    private BooksService booksService ;

    // 查询全部的数据, 并返回到一个书籍页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = booksService.queryAllBooks() ;
        model.addAttribute("list",list) ;

        return "allBook" ;
    }
}

后台操作完毕, 下面就是关于查询所有书籍的前端页面编写了

首先将首页index.jsp进行美化一下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>首页</title>
    <style>
      a{
        text-decoration: none;
        color: black;
        font-size: 18px;
      }
      h3{
        width: 180px;
        height: 38px;
        margin: 100px auto;
        text-align: center;
        line-height: 38px;
        background: deepskyblue;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
  <h3>
    <%--${pageContext.request.contextPath}表示绝对地址, 如果前面内容无误, 则在这后面输入/b后会有后续提示--%>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍展示页面</a>
  </h3>
  </body>
</html>

展示所有书籍的页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>s书籍展示</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 colum">
            <div class="page-header">
                <h1>
                    <small>书籍列表----------显示所有书籍</small>
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 colum">
            <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>

至此, 查询所有书籍的功能就完成了, 启动Tomcat 效果如下:

首页:

在这里插入图片描述

展示所有书籍页面:

在这里插入图片描述

2.2、添加书籍功能

我们要在查询所有书籍的页面上添加一个超链接, 用于跳转

<div class="col-md-4 column">
    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook" >新增书籍</a>
</div>

然后我们需要将接收/book/toAddBook请求的方法写出来, 这个方法不需要做任何事情, 只需要指定跳转页面就可以了 :

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

addBook页面还没有写, 所以我们还需要编写assBook页面, 页面内容如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <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 colum">
            <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" class="form-control" id="bookname" name="bookName" required>
        </div>
        <div class="form-group">
            <label for="booknum">书籍数量: </label>
            <input type="text" class="form-control" id="booknum" name="bookCounts" required>
        </div>
        <div class="form-group">
            <label for="bookdesc">书籍描述: </label>
            <input type="text" class="form-control" id="bookdesc" name="detail" required>
        </div>
        <div class="form-group">
            <br>
            <input type="submit" class="form-control">
        </div>
    </form>

</div>

</body>
</html>

在这个页面中, 表单提交到/book/addBook, 我们需要把处理这个请求的方法写出来, 如下:

// 添加书籍的请求
@RequestMapping("/addBook")
public String addBook(Books books){
    booksService.addBook(books) ;
    return "redirect:/book/allBook" ; // 重定向到/book/allBook请求
}

使用这个方法就可以完成向数据库中添加一本书籍的操作, 并且在完成操作之后, 直接跳转到展示所有书籍的页面上

启动Tomcat, 进行测试

首先进入书籍展示页面:

在这里插入图片描述

点击超链接按钮, 跳转到添加书籍页面, 输入我们要添加的书籍信息

在这里插入图片描述

点击提交后:

在这里插入图片描述

书籍信息添加成功

2.3、修改和删除功能

一般来讲 我们的修改和删除功能是希望能够达到如下图所示的效果:

在这里插入图片描述

2.3.1 修改书籍信息

我们只需要在allBook页面上进行部分修改即可:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <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 colum">
            <div class="page-header">
                <h1>
                    <small>书籍列表----------显示所有书籍</small>
                </h1>
            </div>
        </div>

        <div class="">
            <div class="col-md-4 column">
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook" >新增书籍</a>
            </div>

        </div>

    </div>

    <div class="row clearfix">
        <div class="col-md-12 colum">
            <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}/book/toUpdateBook?id=${book.bookID}">修改</a>&nbsp;|&nbsp;
                                <a href="">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

传入id作为参数的原因是, 我们想要在修改书籍信息的界面上可以看到原来的书籍信息, 接下来我们写跳转的请求

// 跳转到增加书籍页面
@RequestMapping("/toUpdateBook")
public String toUpdatePaper(int id ,Model model){
    Books books = booksService.queryBookById(id);
    model.addAttribute("books",books);
    return "updateBook" ;
}

我们接收id参数, 并且将此id对应的书籍信息查询出来, 使用model将其传递给前端, 接下来我们该写updateBook的前端页面了

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <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 colum">
            <div class="page-header">
                <h1>
                    <small>修改书籍</small>
                </h1>
            </div>
        </div>
    </div>

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

        <%--使用隐藏域 修改书籍信息 是根据书籍id来修改的 务必将书籍id传递到后台!--%>
        <input type="hidden" name="bookID" value="${books.bookID}">
        <div class="form-group">
            <label for="bookname">书籍名称: </label>
            <input type="text" class="form-control" id="bookname" name="bookName" value="${books.bookName}" required>
        </div>
        <div class="form-group">
            <label for="booknum">书籍数量: </label>
            <input type="text" class="form-control" id="booknum" name="bookCounts" value="${books.bookCounts}" required>
        </div>
        <div class="form-group">
            <label for="bookdesc">书籍描述: </label>
            <input type="text" class="form-control" id="bookdesc" name="detail" value="${books.detail}" required>
        </div>
        <div class="form-group">
            <br>
            <input type="submit" class="form-control" value="修改">
        </div>
    </form>

</div>

</body>
</html>

在前端页面中, 我们将原来的书籍信息,作为输入框的默认值, 供修改者参考

值得注意的是: 由于在这里犯了一个错误, 为了排错,我们开启了日志, 使用自带的日志, 在mybatis-config.xml文件中添加如下信息即可:

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

我们遇到的问题是, 进入修改页面后, 输入修改后的信息, 点击修改后, 返回到显示所有书籍信息页面的时候发现书籍信息未修改成功, 经过查找后发现, 是因为没有将书籍id值传回后台, 导致SQL语句找不到我们想要修改的数据,也就无法修改了, 所以我们使用了一个隐藏域来将书籍id信息传递到后台

至此, 修改书籍信息功能完成

2.3.2 删除书籍信息

删除书籍信息的操作甚至连一个单独的页面都不需要, 只要点击链接提交请求,数据, 完成删除操作,返回首页即可

首先完善请求路径:

<td>
    <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">修改</a>&nbsp;|&nbsp;
    <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a>
</td>

在controller层编写处理方法:

// 删除书籍的请求
@RequestMapping("/deleteBook/{bookID}")
public String deleteBook(@PathVariable("bookID") int id){
    booksService.deleteBookById(id) ;
    return "redirect:/book/allBook" ;
}

删除书籍功能完成

2.4、扩展: 新增搜索功能

我们想添加一个新功能, 希望可以通过书籍名来搜索到书籍, 那么我们应该在页面上增加一个搜索框, 搜索完之后应该还要有一个能回到主界面(显示所有书籍的界面)的办法,并且,在搜索发生错误的时候,应该还能发出一些提示, 那么, 我们先完成前端的页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <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 colum">
            <div class="page-header">
                <h1>
                    <small>书籍列表----------显示所有书籍</small>
                </h1>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4 column">
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook" >新增书籍</a>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook" >显示全部书籍</a>
            </div>
            <div class="col-md-8 column">
                <%--查询书籍--%>
                <form class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="" style="float: right">
                    <span style="color: red;font-weight: bold" >${error}</span>
                    <input type="text" name="queryBookName" class="form-control" placeholder="请输入要查询的书籍名称">
                    <input type="submit" value="查询" class="btn btn-primary">
                </form>
            </div>
        </div>

    </div>



    <div class="row clearfix">
        <div class="col-md-12 colum">
            <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}/book/toUpdateBook?id=${book.bookID}">修改</a>&nbsp;|&nbsp;
                                <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
    </div>

</div>

</body>
</html>

页面效果如下:

在这里插入图片描述

接下来我们需要在后台系统中增加功能, 我们需要先创建一个根据书籍名从数据库中查询书籍的方法, 也就是在dao层定义, 然后要在service层调用, 再到controller层,逐层传递数据, 最后传递到前端

首先在dao层定义方法,编写sql, 进行数据库查询的操作:

BookMapper接口:

// 根据书籍名称查询书籍
Books  queryBookByName(@Param("bookName") String bookName) ;

BookMapper.xml:

<select id="queryBookByName" resultType="Books">
    select *
    from ssmbuild.books
    where bookName = #{bookName} ;
</select>

BooksService接口:

// 根据书名查询书
Books queryBookByName(String bookName) ;

BooksServiceImpl:

@Override
public Books queryBookByName(String bookName) {
    return bookMapper.queryBookByName(bookName);
}

BookController:

// 查询书籍的请求
@RequestMapping("/queryBook")
public String queryBook(String queryBookName , Model model){
    Books books = booksService.queryBookByName(queryBookName);
    List<Books> list = new ArrayList<>() ;
    list.add(books);
    if (books == null){
        list = booksService.queryAllBooks();
        model.addAttribute("error","未查询到书籍") ;
    }
    model.addAttribute("list",list) ;
    return "allBook" ;
}

END

标签:bookID,SSM,books,整合,import,Books,书籍,页面
来源: https://blog.csdn.net/Hassder/article/details/116779203