當前位置:網站首頁>SSM框架整合+書籍頁面管理實現

SSM框架整合+書籍頁面管理實現

2022-01-27 18:43:35 你不必是我的

SSM 整合


設計數據庫


create database `ssmbuild`;
use `ssmbuild`;
create table if not exists `books`(
    `bookID` int not null auto_increment comment '圖書id',
    `bookName` varchar(50) not null comment '書名',
    `bookCount` int not null comment '數量',
    `detail` varchar(200) not null comment '描述',
    key `bookID`(`bookID`)
);
insert into `books`(bookID, bookName, bookCount, detail) VALUES
(1,'java',100,'java從入門到放弃'),
(2,'python',10,'python從入門到放弃'),
(3,'Mysql',50,'Mysql從删庫到跑路');

導入依賴


<dependencies>
    <!--Junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>compile</scope>
    </dependency>
    <!--數據庫驅動-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.25</version>
        <scope>compile</scope>
    </dependency>
    <!--數據庫連接池-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.5</version>
        <scope>compile</scope>
    </dependency>
    <!--Servlet-JSP-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>compile</scope>
    </dependency>
    <!--Mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.9</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
        <scope>compile</scope>
    </dependency>
    <!--Spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.14</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.14</version>
        <scope>compile</scope>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
        <scope>compile</scope>
    </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>

編寫配置文件


mybatis-config.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.yqly.pojo"/>
    </typeAliases>
</configuration>

database.properties 文件

  • username 和 password 為關鍵字,key 值不能取改名字
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=utf8&useSSL=true
jdbc.username=root
jdbc.password=123456

編寫實體類


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

Mybatis 層


Mapper 接口

public interface BookMapper {
    
    //增加一本書
    int addBook(Books books);
    //删除一本書
    @Delete("delete from ssmbuild.books where bookID=#{id};")
    int deleteBook(@Param("id") int id);
    //更新一本書
    int updateBook(Books books);
    //查詢一本書
    @Select("select * from ssmbuild.books where bookID=#{id}")
    Books queryBook(@Param("id") int id);
    //查詢書列錶
    List<Books> queryBookList(String searchBookName);
}

Mapper.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yqly.mapper.BookMapper">
    <insert id="addBook" parameterType="books">
        insert into ssmbuild.books(bookName, bookCount, detail)
        values (#{bookName},#{bookCount},#{detail});
    </insert>
    <update id="updateBook" parameterType="books">
        update ssmbuild.books
        <set>
            <if test="bookName!=null">
                bookName=#{bookName},
            </if>
            <if test="bookCount!=null">
                bookCount=#{bookCount},
            </if>
            <if test="detail!=null">
                detail=#{detail}
            </if>
        </set>
        where bookID=#{bookID}
    </update>
    <select id="queryBookList" resultType="books">
        select *
        from ssmbuild.books
        <where>
            <if test="bookName!=null">
                bookName like "%"#{searchBookName}"%"
            </if>
        </where>
    </select>
</mapper>

注册 xml 文件

  • 在 Mybatis-config.xml 文件中注册
<mappers>
    <package name="com.yqly.mapper"/>
</mappers>

Spring 層


Service 接口

public interface BookService {
    
    //增加一本書
    int addBook(Books books);
    //删除一本書
    int deleteBook(int id);
    //更新一本書
    int updateBook(Books books);
    //查詢一本書
    Books queryBook(int id);
    //查詢書列錶
    List<Books> queryBookList(String searchBookName);
}

Service 實現類

  • Service 層(業務層)調 Mapper 層
  • 可以在業務層通過 AOP 增加其他業務操作
public class BookServiceImpl implements BookService{
    

    private BookMapper bookMapper;
    public void setBookMapper(BookMapper bookMapper) {
    
        this.bookMapper = bookMapper;
    }

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

    @Override
    public int deleteBook(int id) {
    
        return bookMapper.deleteBook(id);
    }

    @Override
    public int updateBook(Books books) {
    
        return bookMapper.updateBook(books);
    }

    @Override
    public Books queryBook(int id) {
    
        return bookMapper.queryBook(id);
    }

    @Override
    public List<Books> queryBookList(String searchBookName) {
    
        return bookMapper.queryBookList(searchBookName);
    }

}

SpringMVC 層


web.xml 文件

  • 配置 DispatcherServlet
  • 配置解决亂碼問題
<?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">
    <!--DispatcherServlet-->
    <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:spring-mvc.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>
</web-app>

三層整合


spring-mapper.xml 文件

  • 關聯數據庫的 database.properties 配置文件
  • 配置連接池
    • dbcp:Spring 原生的連接池,半自動化操作,不能自動連接,需要手動連接
    • c3p0:自動化操作,可以自動化的加載配置文件,並且可以自動設置到對象中
    • druid
    • hikari
  • 配置 sqlSessionFactory
<?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 https://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"/>
    <!--數據庫連接池,此處用c3p0連接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--綁定Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注册sql相關的xml文件-->
        <property name="mapperLocations" value="classpath:com/yqly/mapper/*.xml"/>
    </bean>
    <!--配置Mapper接口掃描包,動態的實現了Mapper接口可以注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要掃描的包-->
        <property name="basePackage" value="com.yqly.mapper"/>
    </bean>
</beans>

spring-service.xml 文件

  • 將業務類注入到spring
  • 聲明式事務配置
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描service包下的類-->
    <context:component-scan base-package="com.yqly.service"/>
    <!--將業務類注入到spring-->
    <bean id="BookServiceImpl" class="com.yqly.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
    <!--聲明式事務配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入數據源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>
  • 將業務類注入到 Spring,可以通過配置,也可以通過注解

通過注解:

@Service
public class BookServiceImpl implements BookService{
    

    private BookMapper bookMapper;
    @Autowired
    public void setBookMapper(BookMapper bookMapper) {
    
        this.bookMapper = bookMapper;
    }
}

spring-mvc.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--注解驅動-->
    <mvc:annotation-driven/>
    <!--靜態資源過濾-->
    <mvc:default-servlet-handler/>
    <!--掃描包-->
    <context:component-scan base-package="com.yqly.controller"/>
    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

applicationContext.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="spring-mapper.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

部分功能實現


查詢書籍列錶

BookController.java

@Controller
@RequestMapping("/book")
public class BookController {
    
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model,String searchBookName){
    
        List<Books> booksList = bookService.queryBookList(searchBookName);
        model.addAttribute("list",booksList);
        return "allBook";
    }
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首頁</title>
    <style>
      a{
        text-decoration: none;
        color: cornflowerblue;
      }
      h3{
        width: 180px;
        height: 38px;
        margin: 100px auto;
        text-align: center;
        line-height: 38px;
        background: beige;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
  <h3>
    <a href="${pageContext.request.contextPath}/book/allBook">進入書籍頁面</a>
  </h3>
  </body>
</html>

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>
    <%--運用Bootstrap美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/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 class="row">
                <div class="col-md-4 column">
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增書籍</a>
                </div>
                <div class="col-md-4 column">
                    <%--查詢書籍--%>
                    <form action="${pageContext.request.contextPath}/book/allBook" method="post" class="form-inline" style="float: right">
                        <input type="text" placeholder="請輸入要查詢的書籍名稱" class="form-control" name="searchBookName">
                        <input type="submit" value="查詢" class="btn btn-primary">
                    </form>
                </div>
            </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>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCount}</td>
                        <td>${book.detail}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

在這裏插入圖片描述

添加書籍信息

BookController.java

//跳轉到添加書籍頁面
@RequestMapping("/toAddBook")
public String addPaper(){
    
    return "addBook";
}
//添加書籍
@RequestMapping("/addBook")
public String addBook(Books books){
    
    bookService.addBook(books);
    return "redirect:/book/allBook";
}

allBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>書籍展示頁面</title>
    <%--運用Bootstrap美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/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 class="row">
                <div class="col-md-4 column">
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增書籍</a>
                </div>
                <div class="col-md-4 column">
                    <%--查詢書籍--%>
                    <form action="${pageContext.request.contextPath}/book/allBook" method="post" class="form-inline" style="float: right">
                        <input type="text" placeholder="請輸入要查詢的書籍名稱" class="form-control" name="searchBookName">
                        <input type="submit" value="查詢" class="btn btn-primary">
                    </form>
                </div>
            </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>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCount}</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?id=${book.bookID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加書籍</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/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 action="${pageContext.request.contextPath}/book/addBook" method="post">
        <div class="form-group">
            <label for="bookName">書籍名稱</label>
            <input type="text" name="bookName" class="form-control" id="bookName" required>
        </div>
        <div class="form-group">
            <label for="bookCount">書籍數量</label>
            <input type="text" name="bookCount" class="form-control" id="bookCount" required>
        </div>
        <div class="form-group">
            <label for="detail">書籍描述</label>
            <input type="text" name="detail" class="form-control" id="detail" required>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-default" value="添加">
        </div>
    </form>
</div>
</body>
</html>

在這裏插入圖片描述

修改書籍信息

BookController.java

//跳轉到修改頁面
@RequestMapping("/toUpdateBook")
public String updatePaper(int id,Model model){
    
    Books books = bookService.queryBook(id);
    model.addAttribute("idBook",books);
    return "updateBook";
}
//修改書籍
@RequestMapping("/updateBook")
public String updateBook(Books books){
    
    bookService.updateBook(books);
    return "redirect:/book/allBook";
}

allBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>書籍展示頁面</title>
    <%--運用Bootstrap美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/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 class="row">
                <div class="col-md-4 column">
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增書籍</a>
                </div>
                <div class="col-md-4 column">
                    <%--查詢書籍--%>
                    <form action="${pageContext.request.contextPath}/book/allBook" method="post" class="form-inline" style="float: right">
                        <input type="text" placeholder="請輸入要查詢的書籍名稱" class="form-control" name="searchBookName">
                        <input type="submit" value="查詢" class="btn btn-primary">
                    </form>
                </div>
            </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>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCount}</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?id=${book.bookID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

updateBook.jsp

  • 修改數據需要獲得當前 id,這裏使用隱藏域對前端隱藏,獲得當前 id
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改書籍</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/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 action="${pageContext.request.contextPath}/book/updateBook" method="post">
        <div class="form-group">
            <input type="hidden" name="bookID" class="form-control" value="${idBook.bookID}" required>
        </div>
        <div class="form-group">
            <label for="bookName">書籍名稱</label>
            <input type="text" name="bookName" class="form-control" id="bookName" value="${idBook.bookName}" required>
        </div>
        <div class="form-group">
            <label for="bookCount">書籍數量</label>
            <input type="text" name="bookCount" class="form-control" id="bookCount" value="${idBook.bookCount}" required>
        </div>
        <div class="form-group">
            <label for="detail">書籍描述</label>
            <input type="text" name="detail" class="form-control" id="detail" value="${idBook.detail}" required>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-default" value="修改">
        </div>
    </form>
</div>
</body>
</html>

在這裏插入圖片描述

删除書籍信息

BookController.java

//删除書籍
@RequestMapping("/deleteBook")
public String deleteBook(int id){
    
    bookService.deleteBook(id);
    return "redirect:/book/allBook";
}

allBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>書籍展示頁面</title>
    <%--運用Bootstrap美化界面--%>
    <link href="https://cdn.bootcdn.net/ajax/libs/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 class="row">
                <div class="col-md-4 column">
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增書籍</a>
                </div>
                <div class="col-md-4 column">
                    <%--查詢書籍--%>
                    <form action="${pageContext.request.contextPath}/book/allBook" method="post" class="form-inline" style="float: right">
                        <input type="text" placeholder="請輸入要查詢的書籍名稱" class="form-control" name="searchBookName">
                        <input type="submit" value="查詢" class="btn btn-primary">
                    </form>
                </div>
            </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>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCount}</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?id=${book.bookID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

遇到的异常總結


org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘BookServiceImpl’ available

原因:沒有在 applicationContext.xml 文件中

解决方法:在 applicationContext.xml 文件注册其他 xml 資源

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="spring-mapper.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

The connection property ‘useSSL’ acceptable values are: ‘TRUE’, ‘FALSE’, ‘YES’ or ‘NO’. The value ‘TRUE"’ is not acceptable

原因:useSSL 的值只能是 true,false,yes,no,而我們的值是 true"

解决方法:删除 true 後面的 "

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=utf8&useSSL=true

版權聲明
本文為[你不必是我的]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201271843346522.html

隨機推薦