1、环境搭建

(1)环境:

  • IDEA
  • MySQL 5.7.19
  • Tomcat 9
  • Maven 3.6

要求:

  • 需要熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识;

    (2)系统设计

    书城案例——整合SSM - 图1

    (3)数据库环境

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

    2、基本环境搭建

    (1)创建maven项目,添加web支持

    (2)pom.xml依赖导入

    依赖:junit、数据库驱动、连接池、Servlet、jsp、mybatis、mybatis-spring、spring、spring-webmvc、spring-jdbc、aspectjweaver、lombok 静态资源导出问题

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.learn</groupId>
  7. <artifactId>ssmbuild</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!--依赖问题-->
  10. <!--junit,数据库驱动,连接池,Servlet,jsp,mybatis,mybatis-spring,spring-->
  11. <dependencies>
  12. <!--Junit-->
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>4.12</version>
  17. </dependency>
  18. <!--数据库驱动-->
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <version>5.1.47</version>
  23. </dependency>
  24. <!-- 数据库连接池 c3p0-->
  25. <dependency>
  26. <groupId>com.mchange</groupId>
  27. <artifactId>c3p0</artifactId>
  28. <version>0.9.5.2</version>
  29. </dependency>
  30. <!--Servlet - JSP -->
  31. <dependency>
  32. <groupId>javax.servlet</groupId>
  33. <artifactId>servlet-api</artifactId>
  34. <version>2.5</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>javax.servlet.jsp</groupId>
  38. <artifactId>jsp-api</artifactId>
  39. <version>2.2</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>javax.servlet</groupId>
  43. <artifactId>jstl</artifactId>
  44. <version>1.2</version>
  45. </dependency>
  46. <!--Mybatis-->
  47. <dependency>
  48. <groupId>org.mybatis</groupId>
  49. <artifactId>mybatis</artifactId>
  50. <version>3.5.2</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.mybatis</groupId>
  54. <artifactId>mybatis-spring</artifactId>
  55. <version>2.0.2</version>
  56. </dependency>
  57. <!--Spring-->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-webmvc</artifactId>
  61. <version>5.1.9.RELEASE</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-jdbc</artifactId>
  66. <version>5.1.9.RELEASE</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.aspectj</groupId>
  70. <artifactId>aspectjweaver</artifactId>
  71. <version>1.9.6</version>
  72. </dependency>
  73. <!--lombok-->
  74. <dependency>
  75. <groupId>org.projectlombok</groupId>
  76. <artifactId>lombok</artifactId>
  77. <version>1.16.10</version>
  78. </dependency>
  79. </dependencies>
  80. <!--静态资源导出问题-->
  81. <build>
  82. <resources>
  83. <resource>
  84. <directory>src/main/java</directory>
  85. <includes>
  86. <include>**/*.properties</include>
  87. <include>**/*.xml</include>
  88. </includes>
  89. <filtering>false</filtering>
  90. </resource>
  91. <resource>
  92. <directory>src/main/resources</directory>
  93. <includes>
  94. <include>**/*.properties</include>
  95. <include>**/*.xml</include>
  96. </includes>
  97. <filtering>false</filtering>
  98. </resource>
  99. </resources>
  100. </build>
  101. </project>

「项目结构」——「Artifacts」——「WEB-INF」——「新建lib文件」——导入依赖
书城案例——整合SSM - 图2

(3)Mybatis层编写

a、IDEA关联数据库

b、数据库配置文件 database.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. # 如果使用mysql8.0+ ,需要增加一个时区的配置; &serverTimezone=Asia/Shanghai
  3. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
  4. jdbc.username=root
  5. jdbc.password=root

c、编写数据库对应的实体类 com.learn.pojo.Books

  1. package com.learn.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @AllArgsConstructor
  7. @NoArgsConstructor
  8. public class Books {
  9. private int bookID;
  10. private String bookName;
  11. private int bookCounts;
  12. private String detail;
  13. }

d、mybatis-config.xml的配置

需要配置的是:日志、别名、mapper 数据源的配置交给spring去做

  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. <!--日志-->
  7. <settings>
  8. <setting name="logImpl" value="STDOUT_LOGGING"/>
  9. </settings>
  10. <!--配置数据源,交给spring去做-->
  11. <!--别名-->
  12. <typeAliases>
  13. <package name="com.learn.pojo"/>
  14. </typeAliases>
  15. <!--mapper映射-->
  16. <mappers>
  17. <mapper class="com.learn.dao.BookMapper"/>
  18. </mappers>
  19. </configuration>

e、编写Dao层的 BookMapper接口

  1. package com.learn.dao;
  2. import com.learn.pojo.Books;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. public interface BookMapper {
  6. //增加一个Book
  7. int addBook(Books book);
  8. //根据id删除一个Book
  9. int deleteBookById(@Param("bookID") int id);
  10. //更新Book
  11. int updateBook(Books books);
  12. //根据id查询,返回一个Book
  13. Books queryBookById(@Param("bookID") int id);
  14. //查询全部Book,返回list集合
  15. List<Books> queryAllBook();
  16. //通过书名查询书籍
  17. Books queryBookByName(@Param("bookName") String bookName);
  18. }

f、编写接口对应的Mapper.xml文件。需要导入MyBatis的包;

  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="com.learn.dao.BookMapper">
  6. <!--增加一个Book-->
  7. <insert id="addBook" parameterType="Books">
  8. insert into ssmbuild.books(bookName, bookCounts, detail)
  9. values (#{bookName}, #{bookCounts}, #{detail})
  10. </insert>
  11. <!--根据id删除一个Book-->
  12. <delete id="deleteBookById" parameterType="int">
  13. delete
  14. from ssmbuild.books
  15. where bookID = #{bookID}
  16. </delete>
  17. <!--更新Book-->
  18. <update id="updateBook" parameterType="Books">
  19. update ssmbuild.books
  20. set bookName = #{bookName},
  21. bookCounts = #{bookCounts},
  22. detail = #{detail}
  23. where bookID = #{bookID}
  24. </update>
  25. <!--根据id查询,返回一个Book-->
  26. <select id="queryBookById" resultType="Books">
  27. select *
  28. from ssmbuild.books
  29. where bookID = #{bookID}
  30. </select>
  31. <!--查询全部Book-->
  32. <select id="queryAllBook" resultType="Books">
  33. SELECT *
  34. from ssmbuild.books
  35. </select>
  36. <!--根据书名查询,返回一个Book-->
  37. <select id="queryBookByName" resultType="Books">
  38. select *
  39. from ssmbuild.books
  40. where bookName = #{bookName}
  41. </select>
  42. </mapper>

g、编写Service层的接口BookService和实现类BookServiceImpl

接口

BookService:底下需要去实现,调用dao层。与dao接口代码相同,

  1. package com.learn.service;
  2. import com.learn.pojo.Books;
  3. import java.util.List;
  4. //BookService:底下需要去实现,调用dao层
  5. public interface BookService {
  6. //增加一个Book
  7. int addBook(Books book);
  8. //根据id删除一个Book
  9. int deleteBookById(int id);
  10. //更新Book
  11. int updateBook(Books books);
  12. //根据id查询,返回一个Book
  13. Books queryBookById(int id);
  14. //查询全部Book,返回list集合
  15. List<Books> queryAllBook();
  16. Books queryBookByName(String bookName);
  17. }

实现类
  1. //调用dao层的操作,设置一个set接口,方便Spring管理
  2. private BookMapper bookMapper;

通过调用mapper.xml的方法实现service接口的方法 然后Controller调用service层方法完成业务

  1. package com.learn.service;
  2. import com.learn.dao.BookMapper;
  3. import com.learn.pojo.Books;
  4. import com.learn.service.BookService;
  5. import java.util.List;
  6. public class BookServiceImpl implements BookService {
  7. //调用dao层的操作,设置一个set接口,方便Spring管理
  8. private BookMapper bookMapper;
  9. public void setBookMapper(BookMapper bookMapper) {
  10. this.bookMapper = bookMapper;
  11. }
  12. public int addBook(Books book) {
  13. return bookMapper.addBook(book);
  14. }
  15. public int deleteBookById(int id) {
  16. return bookMapper.deleteBookById(id);
  17. }
  18. public int updateBook(Books books) {
  19. return bookMapper.updateBook(books);
  20. }
  21. public Books queryBookById(int id) {
  22. return bookMapper.queryBookById(id);
  23. }
  24. public List<Books> queryAllBook() {
  25. return bookMapper.queryAllBook();
  26. }
  27. public Books queryBookByName(String bookName) {
  28. return bookMapper.queryBookByName(bookName);
  29. }
  30. }

OK,到此,底层需求操作编写完毕!

(4)Spring层

a、配置Spring整合MyBatis,我们这里数据源使用c3p0连接池;

  1. dbcp:半自动化操作,不能自动连接
  2. c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中)
  3. druid:
  4. hikari:

b、我们去编写Spring整合Mybatis的相关的配置文件——spring-dao.xml

1.关联数据库配置文件 2.连接池 3.sqlSessionFactory,绑定mybatis的配置文件 4.扫描dao包,注入sqlSessionFactory

  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. <!--1.关联数据库配置文件-->
  10. <context:property-placeholder location="classpath:database.properties"/>
  11. <!--2.连接池
  12. dbcp:半自动化操作,不能自动连接
  13. c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中)
  14. druid:
  15. hikari:
  16. -->
  17. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  18. <property name="driverClass" value="${jdbc.driver}"/>
  19. <property name="jdbcUrl" value="${jdbc.url}"/>
  20. <property name="user" value="${jdbc.username}"/>
  21. <property name="password" value="${jdbc.password}"/>
  22. <!-- c3p0连接池的私有属性 -->
  23. <property name="maxPoolSize" value="30"/>
  24. <property name="minPoolSize" value="10"/>
  25. <!-- 关闭连接后不自动commit -->
  26. <property name="autoCommitOnClose" value="false"/>
  27. <!-- 获取连接超时时间 -->
  28. <property name="checkoutTimeout" value="10000"/>
  29. <!-- 当获取连接失败重试次数 -->
  30. <property name="acquireRetryAttempts" value="2"/>
  31. </bean>
  32. <!--3.sqlSessionFactory-->
  33. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  34. <property name="dataSource" ref="dataSource"/>
  35. <!--绑定mybatis的配置文件-->
  36. <property name="configLocation" value="classpath:mybaits-config.xml"/>
  37. </bean>
  38. <!--4.配置dao接口扫描包,动态的实现了dao接口可以注入到spring容器中-->
  39. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  40. <!--注入sqlSessionFactory-->
  41. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  42. <!--要扫描的dao包-->
  43. <property name="basePackage" value="com.learn.dao"/>
  44. </bean>
  45. </beans>

c、Spring整合service层——spring-service.xml

1.扫描service的包 2.将我们所有的业务类注入到spring,可以通过配置,也可以通过注解实现 3.声明式事务配置 4.aop事务支持 5.配置事务切入

  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:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. https://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. https://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!--1.扫描service的包-->
  16. <context:component-scan base-package="com.learn.service"/>
  17. <!--2.将我们所有的业务类注入到spring,可以通过配置,也可以通过注解实现-->
  18. <!--注解就是@service @autowired-->
  19. <bean id="BookServiceImpl" class="com.learn.service.BookServiceImpl">
  20. <property name="bookMapper" ref="bookMapper"/>
  21. </bean>
  22. <!--3.声明式事务配置-->
  23. <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  24. <!--注入数据源-->
  25. <property name="dataSource" ref="dataSource"/>
  26. </bean>
  27. <!--4.aop事务支持-->
  28. <!--结合AOP实现事务的织入-->
  29. <!--配置事务的传播特性: new propagation= -->
  30. <tx:advice id="txAdvice" transaction-manager="TransactionManager">
  31. <tx:attributes>
  32. <!-- <tx:method name="add" propagation="REQUIRED"/>-->
  33. <!-- <tx:method name="delete" propagation="REQUIRED"/>-->
  34. <!-- <tx:method name="update" propagation="REQUIRED"/>-->
  35. <!-- <tx:method name="query" read-only="true"/>-->
  36. <tx:method name="*" propagation="REQUIRED"/>
  37. </tx:attributes>
  38. </tx:advice>
  39. <!--5.配置事务切入-->
  40. <aop:config>
  41. <aop:pointcut id="txPointCut" expression="execution(* com.learn.dao.*.*(..))"/>
  42. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
  43. </aop:config>
  44. </beans>

Spring层搞定!再次理解一下,Spring就是一个大杂烩,一个容器!对吧!

(5)SpringMVC层

a、web.xml

1、DispatcherServlet。 <一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!> 2、乱码过滤 3、Session

  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. <!--DispatcherServlet-->
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:applicationContext.xml</param-value>
  13. </init-param>
  14. <load-on-startup>1</load-on-startup>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>springmvc</servlet-name>
  18. <url-pattern>/</url-pattern>
  19. </servlet-mapping>
  20. <!--乱码过滤-->
  21. <filter>
  22. <filter-name>encodingFilter</filter-name>
  23. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  24. <init-param>
  25. <param-name>encoding</param-name>
  26. <param-value>utf-8</param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>encodingFilter</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33. <!--Session-->
  34. <session-config>
  35. <session-timeout>15</session-timeout>
  36. </session-config>
  37. </web-app>

b、spring-mvc.xml

1.注解驱动 2.静态资源过滤 3.扫描controller包 4.视图解析器

  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:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  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/mvc
  9. https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  10. <!--1.注解驱动-->
  11. <mvc:annotation-driven/>
  12. <!--2.静态资源过滤-->
  13. <mvc:default-servlet-handler/>
  14. <!--3.扫描包 controller-->
  15. <context:component-scan base-package="com.learn.controller"/>
  16. <!--4.视图解析器-->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/WEB-INF/jsp/"/>
  19. <property name="suffix" value=".jsp"/>
  20. </bean>
  21. </beans>

c、Spring配置整合文件,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. <!--整合spring配置文件-->
  7. <import resource="classpath:spring-dao.xml"/>
  8. <import resource="classpath:spring-mvc.xml"/>
  9. <import resource="classpath:spring-service.xml"/>
  10. </beans>

「项目结构」——「Facets」要如下图右边所示,才算Spring配置整合文件成功
书城案例——整合SSM - 图3
配置文件,暂时结束!Controller 和 视图层编写

3、Controller 层

  1. package com.learn.controller;
  2. import com.learn.pojo.Books;
  3. import com.learn.service.BookService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. @Controller
  13. @RequestMapping("/book")
  14. public class BookController {
  15. //controller调service
  16. @Autowired
  17. @Qualifier("BookServiceImpl")
  18. private BookService bookService;
  19. //查询全部的书籍,并且返回到一个书籍展示页面
  20. @RequestMapping("/allBook")
  21. public String list(Model model) {
  22. List<Books> list = bookService.queryAllBook();
  23. model.addAttribute("list", list);
  24. return "allBook";
  25. }
  26. //跳转到增加书籍页面
  27. @RequestMapping("/toAddBook")
  28. public String toAddPaper() {
  29. return "addBook";
  30. }
  31. //添加书籍的请求
  32. @RequestMapping("/addBook")
  33. public String addPaper(Books books) {
  34. bookService.addBook(books);
  35. return "redirect:/book/allBook"; //重定向
  36. }
  37. //跳转到修改页面
  38. @RequestMapping("/toUpdate")
  39. public String toUpdate(int id,Model model) {
  40. Books book = bookService.queryBookById(id);
  41. model.addAttribute("QBook",book);
  42. return "updateBook";
  43. }
  44. //修改修改页面
  45. @RequestMapping("/updateBook")
  46. public String updateBook(Books book) {
  47. System.out.println("update"+book);
  48. bookService.updateBook(book);
  49. return "redirect:/book/allBook";
  50. }
  51. //删除书籍
  52. @RequestMapping("/deleteBook/{bookID}")
  53. public String deleteBook(@PathVariable("bookID") int id){
  54. bookService.deleteBookById(id);
  55. return "redirect:/book/allBook";
  56. }
  57. //查询书籍
  58. @RequestMapping("/queryBook")
  59. public String queryBook(String queryBookName,Model model){
  60. Books books = bookService.queryBookByName(queryBookName);
  61. List<Books> list =new ArrayList<Books>();
  62. if (books==null){
  63. list=bookService.queryAllBook();
  64. model.addAttribute("error","未查到");
  65. }
  66. else {
  67. list.add(books);
  68. }
  69. model.addAttribute("list", list);
  70. return "allBook";
  71. }
  72. }

4、JSP页面

index.jsp——首页

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>首页 </title>
  5. <style>
  6. a{
  7. text-decoration: none;
  8. color: black;
  9. font-size: 18px;
  10. }
  11. h3{
  12. width:180px;
  13. height: 38px;
  14. margin: 100px auto;
  15. text-align: center;
  16. line-height: 38px;
  17. background: aqua;
  18. border-radius: 5px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <h3>
  24. <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
  25. </h3>
  26. </body>
  27. </html>

allBook.jsp——展示所有书籍页面

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>书籍展示页面</title>
  6. <!-- 新 Bootstrap 核心 CSS 文件 -->
  7. <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  8. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  9. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
  10. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  11. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row clearfix ">
  16. <div class="clo-md-12 column">
  17. <div class="page-header">
  18. <h1>
  19. <small>书籍列表---显示所有书籍</small>
  20. </h1>
  21. </div>
  22. </div>
  23. <div class="row">
  24. <div class="col-md-4 column">
  25. <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
  26. <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示所有书籍</a>
  27. </div>
  28. <div class="col-md-8 column">
  29. <%--查询书籍--%>
  30. <form action="${pageContext.request.contextPath}/book/queryBook" method="post" class="form-inline" style="float: right">
  31. <span style="color: red;font-weight: bold">${error}</span>
  32. <input type="text" name="queryBookName" class="form-control" placeholder="请输入要查询的书籍名称">
  33. <input type="submit" class="btn btn-primary " value="查询">
  34. </form>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="row">
  39. <div class="col-md-12 column">
  40. <table class="table table-hover table-striped">
  41. <thead>
  42. <tr>
  43. <th>书籍编号</th>
  44. <th>书籍名称</th>
  45. <th>书籍数量</th>
  46. <th>书籍详情</th>
  47. <th>操作</th>
  48. </tr>
  49. </thead>
  50. <%--从list中遍历出来:foreach--%>
  51. <tbody>
  52. <c:forEach var="book" items="${list}">
  53. <tr>
  54. <td>${book.bookID}</td>
  55. <td>${book.bookName}</td>
  56. <td>${book.bookCounts}</td>
  57. <td>${book.detail}</td>
  58. <td>
  59. <a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a>
  60. &nbsp;|&nbsp;
  61. <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a>
  62. </td>
  63. </tr>
  64. </c:forEach>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. </div>
  70. </body>
  71. </html>

addBook.jsp——添加书籍界面

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>添加书籍页面</title>
  6. <!-- 新 Bootstrap 核心 CSS 文件 -->
  7. <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  8. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  9. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
  10. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  11. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row clearfix ">
  16. <div class="clo-md-12 column">
  17. <div class="page-header">
  18. <h1>
  19. <small>新增书籍</small>
  20. </h1>
  21. </div>
  22. </div>
  23. </div>
  24. <form action="${pageContext.request.contextPath}/book/addBook" method="post">
  25. <div class="form-group">
  26. <label >书籍名称:</label>
  27. <input type="text" name="bookName" class="form-control" required>
  28. </div>
  29. <div class="form-group">
  30. <label >书籍数量:</label>
  31. <input type="text" name="bookCounts" class="form-control" required>
  32. </div>
  33. <div class="form-group">
  34. <label >书籍描述</label>
  35. <input type="text" name="detail" class="form-control" required>
  36. </div>
  37. <div class="form-group">
  38. <input type="submit" class="form-control" value="添加" >
  39. </div>
  40. </form>
  41. </div>
  42. </body>
  43. </html>

update.jsp——修改书籍界面

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>修改书籍页面</title>
  6. <!-- 新 Bootstrap 核心 CSS 文件 -->
  7. <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  8. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  9. <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
  10. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  11. <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row clearfix ">
  16. <div class="clo-md-12 column">
  17. <div class="page-header">
  18. <h1>
  19. <small>修改</small>
  20. </h1>
  21. </div>
  22. </div>
  23. </div>
  24. <form action="${pageContext.request.contextPath}/book/updateBook" method="post">
  25. <%--出现的问题,我们提交了修改的sql请求,但是修改失败
  26. 可能是事务问题
  27. 看一下sql语句能否执行成功
  28. 前段传递隐藏域
  29. --%>
  30. <input type="hidden" name="bookID" value="${QBook.bookID}">
  31. <div class="form-group">
  32. <label>书籍名称:</label>
  33. <input type="text" name="bookName" class="form-control" value="${QBook.bookName}" required>
  34. </div>
  35. <div class="form-group">
  36. <label>书籍数量:</label>
  37. <input type="text" name="bookCounts" class="form-control" value="${QBook.bookCounts}" required>
  38. </div>
  39. <div class="form-group">
  40. <label>书籍描述</label>
  41. <input type="text" name="detail" class="form-control" value="${QBook.detail}" required>
  42. </div>
  43. <div class="form-group">
  44. <input type="submit" class="form-control" value="修改">
  45. </div>
  46. </form>
  47. </div>
  48. </body>
  49. </html>