当前位置:   article > 正文

SpringMVC:整合SSM框架_sse spring mvc

sse spring mvc

一、大致框架

1.建立数据库,并导入数据

create database cjgl; 
  1. create table books(
  2. bookID int(10) not null auto_increment comment '书id',
  3. bookName varchar(100) not null comment '书名',
  4. bookCounts int(11) not null comment '数量',
  5. detail varchar(200) not null comment '描述',
  6. key bookID(bookID)
  7. )engine=innodb default charset=utf8
  1. insert into books(bookID,bookName,bookCounts,detail)values
  2. (1,'java',1,'从入门到放弃'),
  3. (2,'MySQL',10,'从删库到跑路'),
  4. (3,'Linux',5,'从进门到进牢');

2.新建maven项目SSMZH

鼠标右键点击SSMZH,添加框架框架支持

选中web应用程序点击确定

3.在pom.xml中导入相关的pom依赖!

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-webmvc</artifactId>
  5. <version>5.3.9</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-jdbc</artifactId>
  10. <version>5.3.9</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.13.2</version>
  16. <scope>test</scope>
  17. </dependency>
  18. <!--数据库驱动-->
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <version>8.0.17</version>
  23. </dependency>
  24. <!-- 数据库连接池 -->
  25. <dependency>
  26. <groupId>com.mchange</groupId>
  27. <artifactId>c3p0</artifactId>
  28. <version>0.9.5.2</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>javax.servlet</groupId>
  32. <artifactId>servlet-api</artifactId>
  33. <version>2.5</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>javax.servlet.jsp</groupId>
  37. <artifactId>jsp-api</artifactId>
  38. <version>2.2</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>javax.servlet</groupId>
  42. <artifactId>jstl</artifactId>
  43. <version>1.2</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.apache.tomcat</groupId>
  47. <artifactId>tomcat</artifactId>
  48. <version>8.5.38</version>
  49. <type>pom</type>
  50. </dependency>
  51. <!--Mybatis-->
  52. <dependency>
  53. <groupId>org.mybatis</groupId>
  54. <artifactId>mybatis</artifactId>
  55. <version>3.5.6</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.mybatis</groupId>
  59. <artifactId>mybatis-spring</artifactId>
  60. <version>2.0.5</version>
  61. </dependency>
  62. </dependencies>
  63. <!--Maven资源过滤设置-->
  64. <build>
  65. <resources>
  66. <resource>
  67. <directory>src/main/java</directory>
  68. <includes>
  69. <include>**/*.properties</include>
  70. <include>**/*.xml</include>
  71. </includes>
  72. <filtering>false</filtering>
  73. </resource>
  74. <resource>
  75. <directory>src/main/resources</directory>
  76. <includes>
  77. <include>**/*.properties</include>
  78. <include>**/*.xml</include>
  79. </includes>
  80. <filtering>false</filtering>
  81. </resource>
  82. </resources>
  83. </build>
  84. <properties>
  85. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  86. </properties>

4.添加web模块, 添加web 支持

鼠标右键点击文件选择项目结构,选择工件,在可用元素中点击第一个按住shift在点击最后一个全选,点击鼠标右键,置于/WEB-INF/lib然后点击确定

5.建立基本结构和配置框架

  • yj.com.pojo

  • yj.com.mapper(dao)

  • yj.com.service

  • yj.com.controller

6.在pojo包下建立Books类(和数据库中的表相对应)

 Books中一个四个属性,与表中属性必须的相对应

全部代码如下:

  1. package yj.com.pojo;
  2. public class Books {
  3. private int bookID;
  4. private String bookName;
  5. private int bookCounts;
  6. private String detail;
  7. //添加无参构造、有参构造、getter/setter方法和tostring方法
  8. public Books() {
  9. }
  10. public Books(int bookID, String bookName, int bookCounts, String detail) {
  11. this.bookID = bookID;
  12. this.bookName = bookName;
  13. this.bookCounts = bookCounts;
  14. this.detail = detail;
  15. }
  16. public int getBookID() {
  17. return bookID;
  18. }
  19. public void setBookID(int bookID) {
  20. this.bookID = bookID;
  21. }
  22. public String getBookName() {
  23. return bookName;
  24. }
  25. public void setBookName(String bookName) {
  26. this.bookName = bookName;
  27. }
  28. public int getBookCounts() {
  29. return bookCounts;
  30. }
  31. public void setBookCounts(int bookCounts) {
  32. this.bookCounts = bookCounts;
  33. }
  34. public String getDetail() {
  35. return detail;
  36. }
  37. public void setDetail(String detail) {
  38. this.detail = detail;
  39. }
  40. @Override
  41. public String toString() {
  42. return "Books{" +
  43. "bookID=" + bookID +
  44. ", bookName='" + bookName + '\'' +
  45. ", bookCounts=" + bookCounts +
  46. ", detail='" + detail + '\'' +
  47. '}';
  48. }
  49. }

7.在mapper包下建立接口BookMapper

创建相关操作

  1. package yj.com.mapper;
  2. import yj.com.pojo.Books;
  3. import java.util.List;
  4. public interface BookMapper{
  5. //增加一个Book
  6. int addBook(Books books);
  7. //根据id删除一个Book
  8. int deleteBookById(int id);
  9. //更新Book
  10. int updateBook(Books books);
  11. //根据id查询,返回一个Book
  12. Books queryBookById(int id);
  13. //查询全部Book,返回list集合
  14. List<Books> queryAllBook();
  15. }

8.在mapper包下建立文件BookMapper.xml并配置

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="yj.com.mapper.BookMapper">
  6. <!--增加一个Book-->
  7. <insert id="addBook" parameterType="Books">
  8. insert into cjgl.books(bookName,bookCounts,detail)
  9. values (#{bookName}, #{bookCounts}, #{detail})
  10. </insert>
  11. <!--根据id删除一个Book-->
  12. <delete id="deleteBookById" parameterType="int">
  13. delete from cjgl.books where bookID=#{bookID}
  14. </delete>
  15. <!--更新Book-->
  16. <update id="updateBook" parameterType="Books">
  17. update cjgl.books
  18. set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
  19. </update>
  20. <!--根据id查询,返回一个Book-->
  21. <select id="queryBookById" parameterType="int" resultType="Books">
  22. select * from cjgl.books where bookID=#{bookID}
  23. </select>
  24. <!--查询全部Book-->
  25. <select id="queryAllBook" resultType="Books">
  26. select * from cjgl.books
  27. </select>
  28. </mapper>

 9.在service包下创建接口BookService作业务层

做的业务和dao层的一样

  1. package yj.com.service;
  2. import yj.com.pojo.Books;
  3. import java.util.List;
  4. public interface BookService {
  5. //增加一个Book
  6. int addBook(Books books);
  7. //根据id删除一个Book
  8. int deleteBookById(int id);
  9. //更新Book
  10. int updateBook(Books books);
  11. //根据id查询,返回一个Book
  12. Books queryBookById(int id);
  13. //查询全部Book,返回list集合
  14. List<Books> queryAllBook();
  15. }

10.在service下创建类BookServiceImpl

  1. package yj.com.service;
  2. import yj.com.mapper.BookMapper;
  3. import yj.com.pojo.Books;
  4. import java.util.List;
  5. //实现BookService 重写抽象方法
  6. public class BookServiceImpl implements BookService{
  7. //服务层连接dao层
  8. //创建一个dao层对象
  9. private BookMapper bookMapper;
  10. public void setBookMapper(BookMapper bookMapper) {
  11. this.bookMapper = bookMapper;
  12. }
  13. public int addBook(Books books) {
  14. return bookMapper.addBook(books);
  15. }
  16. public int deleteBookById(int id) {
  17. return bookMapper.deleteBookById(id);
  18. }
  19. public int updateBook(Books books) {
  20. return bookMapper.updateBook(books);
  21. }
  22. public Books queryBookById(int id) {
  23. return bookMapper.queryBookById(id);
  24. }
  25. public List<Books> queryAllBook() {
  26. return bookMapper.queryAllBook();
  27. }
  28. }

11.在controller包下创建BookController类

  1. package yj.com.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import yj.com.service.BookService;
  6. @Controller
  7. @RequestMapping("/book")
  8. public class BookController {
  9. //控制器调用Service层
  10. @Autowired
  11. private BookService bookService;
  12. }

服务层(BookService)调用dao层(BookMapper),dao层调用pojo,控制器调用服务层

二、开始建立配置文件

1.   在resource下建立数据库配置文件database.properties

  1. jdbc.driver=com.mysql.cj.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/cjgl?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=

2. 在resource下编写MyBatis核心配置文件mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <typeAliases>
  7. <package name="yj.com.pojo"/>
  8. </typeAliases>
  9. <mappers>
  10. <mapper resource="yj/com/mapper/BookMapper.xml"/>
  11. </mappers>
  12. </configuration>

3.整合dao层,resource下编写配置spring-dao.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. https://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 配置整合mybatis -->
  10. <!-- 1.关联数据库文件 -->
  11. <context:property-placeholder location="classpath:database.properties"/>
  12. <!-- 2.数据库连接池 -->
  13. <!--数据库连接池
  14. dbcp 半自动化操作 不能自动连接
  15. c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
  16. -->
  17. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  18. <!-- 配置连接池属性 -->
  19. <property name="driverClass" value="${jdbc.driver}"/>
  20. <property name="jdbcUrl" value="${jdbc.url}"/>
  21. <property name="user" value="${jdbc.username}"/>
  22. <property name="password" value="${jdbc.password}"/>
  23. <!-- c3p0连接池的私有属性 -->
  24. <property name="maxPoolSize" value="30"/>
  25. <property name="minPoolSize" value="10"/>
  26. <!-- 关闭连接后不自动commit -->
  27. <property name="autoCommitOnClose" value="false"/>
  28. <!-- 获取连接超时时间 -->
  29. <property name="checkoutTimeout" value="10000"/>
  30. <!-- 当获取连接失败重试次数 -->
  31. <property name="acquireRetryAttempts" value="2"/>
  32. </bean>
  33. <!-- 3.配置SqlSessionFactory对象 -->
  34. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  35. <!-- 注入数据库连接池 -->
  36. <property name="dataSource" ref="dataSource"/>
  37. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
  38. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  39. </bean>
  40. <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
  41. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42. <!-- 注入sqlSessionFactory -->
  43. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  44. <!-- 给出需要扫描Dao接口包 -->
  45. <property name="basePackage" value="yj.com.mapper"/>
  46. </bean>
  47. </beans>

4.Spring整合service层 , resource下编写配置spring-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 扫描service相关的bean -->
  10. <context:component-scan base-package="yj.com.service" />
  11. <!--BookServiceImpl注入到IOC容器中-->
  12. <bean id="BookServiceImpl" class="yj.com.service.BookServiceImpl">
  13. <property name="bookMapper" ref="bookMapper"/>
  14. </bean>
  15. <!-- 配置事务管理器 -->
  16. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  17. <!-- 注入数据库连接池 -->
  18. <property name="dataSource" ref="dataSource" />
  19. </bean>
  20. </beans>

此处正常有两处应该是红的,如果不是则前面有错误(文件没配置完所以报红)

5.整合前段 , resource下编写配置spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!-- Spring整合springMVC -->
  13. <!-- 1.开启SpringMVC注解驱动 -->
  14. <mvc:annotation-driven />
  15. <!-- 2.静态资源默认servlet配置-->
  16. <mvc:default-servlet-handler/>
  17. <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
  18. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  20. <property name="prefix" value="/WEB-INF/jsp/" />
  21. <property name="suffix" value=".jsp" />
  22. </bean>
  23. <!-- 4.扫描web相关的bean -->
  24. <context:component-scan base-package="yj.com.controller" />
  25. </beans>

6.全局配置文件,resource下编写配置applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <import resource="spring-dao.xml"/>
  7. <import resource="spring-service.xml"/>
  8. <import resource="spring-mvc.xml"/>
  9. </beans>

三、配置前段

1.配置前段控制器(中央控制器)web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <servlet>
  7. <servlet-name>DispatcherServlet</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <init-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:applicationContext.xml</param-value>
  12. </init-param>
  13. <load-on-startup>1</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>DispatcherServlet</servlet-name>
  17. <url-pattern>/</url-pattern>
  18. </servlet-mapping>
  19. <filter>
  20. <filter-name>CharacterEncodingFilter</filter-name>
  21. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  22. <init-param>
  23. <param-name>encoding</param-name>
  24. <param-value>utf-8</param-value>
  25. </init-param>
  26. </filter>
  27. <filter-mapping>
  28. <filter-name>CharacterEncodingFilter</filter-name>
  29. <url-pattern>/*</url-pattern>
  30. </filter-mapping>
  31. <session-config>
  32. <session-timeout>15</session-timeout>
  33. </session-config>
  34. </web-app>

2.在文件WEB-INF下建立目录jsp,如下

3.配置index.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: yangj
  4. Date: 2022/12/5
  5. Time: 15:12
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  9. <html>
  10. <head>
  11. <title>首页</title>
  12. <style type="text/css">
  13. a {
  14. text-decoration: none;
  15. color: black;
  16. font-size: 18px;
  17. }
  18. h3 {
  19. width: 180px;
  20. height: 38px;
  21. margin: 100px auto;
  22. text-align: center;
  23. line-height: 38px;
  24. background: deepskyblue;
  25. border-radius: 4px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h3>
  31. <a href="${pageContext.request.contextPath}/book/allBook">点击进入图书管理列表</a>
  32. </h3>
  33. </body>
  34. </html>

 4.配置BookController

  1. package yj.com.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import yj.com.pojo.Books;
  7. import yj.com.service.BookService;
  8. import java.util.List;
  9. @Controller
  10. @RequestMapping("/book")
  11. public class BookController {
  12. //控制器调用Service层
  13. @Autowired
  14. private BookService bookService;
  15. @RequestMapping("allBook")
  16. public String list(Model model) {
  17. List<Books> list = bookService.queryAllBook();
  18. model.addAttribute("list", list);
  19. return "allBook";
  20. }
  21. }

5.在jsp目录下创建并配置文件allBook.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: yangj
  4. Date: 2022/12/6
  5. Time: 9:59
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  10. <html>
  11. <head>
  12. <title>书籍列表</title>
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <!-- 引入 Bootstrap -->
  15. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet">
  16. </head>
  17. <body>
  18. <div class="row clearfix">
  19. <div class="col-md-12 column">
  20. <div class="page-header">
  21. <h1>
  22. <small>书籍列表 —— 显示所有书籍</small>
  23. </h1>
  24. </div>
  25. </div>
  26. </div>
  27. <div class="row">
  28. <div class="col-md-4 column">
  29. <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
  30. </div>
  31. </div>
  32. <div class="row clearfix">
  33. <div class="col-md-12 column">
  34. <table class="table table-hover table-striped">
  35. <thead>
  36. <tr>
  37. <th>书籍编号</th><th>书籍名字</th><th>书籍数量</th><th>书籍详情</th><th>操作</th>
  38. </tr>
  39. </thead>
  40. <tbody>
  41. <c:forEach var="book" items="${requestScope.get('list')}">
  42. <tr>
  43. <td>${book.getBookID()}</td>
  44. <td>${book.getBookName()}</td>
  45. <td>${book.getBookCounts()}</td>
  46. <td>${book.getDetail()}</td>
  47. <td>
  48. <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}">更改</a> |
  49. <a href="${pageContext.request.contextPath}/book/del/${book.getBookID()}">删除</a>
  50. </td>
  51. </tr>
  52. </c:forEach>
  53. </tbody>
  54. </table>
  55. </div>
  56. </div>
  57. </body>
  58. </html>

6.配置服务器

添加配置,点击加号,找到Tomcat Server 本地服务,名称随便什么,应用程序服务器如下tomcat233,部署配置如下点击加号,添加工件,应用程序上下文改为:“/”,然后确定

7.运行程序

8.实现添加功能。

在BookController下继续配置

  1. @RequestMapping("/toAddBook")
  2. public String toAddpaper(){
  3. return "addBook";
  4. }
  5. @RequestMapping("/addBook")
  6. public String addPaper(Books books){
  7. System.out.println(books);
  8. bookService.addBook(books);
  9. return "redirect:/book/allBook";
  10. }

在jsp下创建配置addBook.jsp文件

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: yangj
  4. Date: 2022/12/6
  5. Time: 10:13
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  10. <html>
  11. <head>
  12. <title>新增书籍</title>
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <!-- 引入 Bootstrap -->
  15. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet">
  16. </head>
  17. <body>
  18. <div class="container">
  19. <div class="row clearfix">
  20. <div class="col-md-12 column">
  21. <div class="page-header">
  22. <h1>
  23. <small>新增书籍</small>
  24. </h1>
  25. </div>
  26. </div>
  27. </div>
  28. <form action="${pageContext.request.contextPath}/book/addBook" method="post">
  29. 书籍名称:<input type="text" name="bookName"><br><br><br>
  30. 书籍数量:<input type="text" name="bookCounts"><br><br><br>
  31. 书籍详情:<input type="text" name="detail"><br><br><br>
  32. <input type="submit" value="添加">
  33. </form>
  34. </div>
  35. </body>
  36. </html>

9.实现更新和删除功能

在BookController下继续配置

  1. @RequestMapping("/toUpdateBook")
  2. public String toUpdateBook(Model model,int id){
  3. Books books=bookService.queryBookById(id);
  4. System.out.println(books);
  5. model.addAttribute("book",books);
  6. return "updateBook";
  7. }
  8. @RequestMapping("/updateBook")
  9. public String UpdateBook(Model model,Books book){
  10. System.out.println(book);
  11. bookService.updateBook(book);
  12. Books books=bookService.queryBookById(book.getBookID());
  13. model.addAttribute("book",books);
  14. return "redirect:/book/allBook";
  15. }
  16. @RequestMapping("/del/{bookID}")
  17. public String deleteBook(@PathVariable("bookID") int id){
  18. bookService.deleteBookById(id);
  19. return "redirect:/book/allBook";
  20. }

在jsp下创建配置updateBook.jsp文件

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: yangj
  4. Date: 2022/12/6
  5. Time: 10:24
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  10. <html>
  11. <head>
  12. <title>修改信息</title>
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <!-- 引入 Bootstrap -->
  15. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet">
  16. </head>
  17. <body>
  18. <div class="container">
  19. <div class="row clearfix">
  20. <div class="col-md-12 column">
  21. <div class="page-header">
  22. <h1>
  23. <small>修改信息</small>
  24. </h1>
  25. </div>
  26. </div>
  27. </div>
  28. <form action="${pageContext.request.contextPath}/book/updateBook" method="post">
  29. <input type="hidden" name="bookID" value="${book.getBookID()}"/>
  30. 书籍名称:<input type="text" name="bookName"value="${book.getBookName()}"/>
  31. 书籍数量:<input type="text" name="bookCounts"value="${book.getBookCounts()}"/>
  32. 书籍详情:<input type="text" name="detail" value="${book.getDetail() }"/>
  33. <input type="submit" value="提交"/>
  34. </form>
  35. </div>
  36. </body>
  37. </html>

注意:如果遇见这个问题,无法从底层数据库获取连接! 一般是一下几个问题:

  1. 驱动Jar包版本与注册驱动的类不匹配。
  2. 数据库连接地址有误。
  3. 数据库账户密码是否输入正确。
  4. 数据库,表,字段是否对应。
  5. 项目是否引入驱动包。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/335485
推荐阅读
相关标签
  

闽ICP备14008679号