1、pom.xml

  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.qing</groupId>
  7. <artifactId>ssm-build</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!--依赖-->
  10. <dependencies>
  11. <!--Junit-->
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.12</version>
  16. </dependency>
  17. <!--数据库驱动-->
  18. <dependency>
  19. <groupId>mysql</groupId>
  20. <artifactId>mysql-connector-java</artifactId>
  21. <version>8.0.24</version>
  22. </dependency>
  23. <!-- 数据库连接池 -->
  24. <!-- https://mvnrepository.com/artifact/com.mchange/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.projectlombok</groupId>
  70. <artifactId>lombok</artifactId>
  71. <version>1.18.6</version>
  72. </dependency>
  73. </dependencies>
  74. <!-- 静态资源导出问题-->
  75. <build>
  76. <resources>
  77. <resource>
  78. <directory>src/main/java</directory>
  79. <includes>
  80. <include>**/*.properties</include>
  81. <include>**/*.xml</include>
  82. </includes>
  83. <filtering>false</filtering>
  84. </resource>
  85. <resource>
  86. <directory>src/main/resources</directory>
  87. <includes>
  88. <include>**/*.properties</include>
  89. <include>**/*.xml</include>
  90. </includes>
  91. <filtering>false</filtering>
  92. </resource>
  93. </resources>
  94. </build>
  95. </project>

2、配置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. <!-- 配置数据源,交给spring做-->
  7. <typeAliases>
  8. <package name="com.qing.pojo"/>
  9. </typeAliases>
  10. <mappers>
  11. <mapper class="com.qing.mapper.BookMapper"/>
  12. </mappers>
  13. </configuration>

3、配置database.properties数据源

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
  3. jdbc.username=root
  4. jdbc.password=qing
  5. #如果mysql8出现时区问题则添加下面一行
  6. #serverTimezone=Asia/Shanghai&

4、配置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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  7. <!--1 关联数据库配置文件-->
  8. <context:property-placeholder location="classpath:database.properties"/>
  9. <!--2 连接池 c3p0
  10. dbcp:半自动化操作 不能自动连接
  11. c3p0:自动化操作 自动加载配置文件,并可以自动设置到对象中
  12. druid:
  13. hikari:
  14. -->
  15. <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  16. <property name="driverClass" value="${jdbc.driver}"/>
  17. <property name="jdbcUrl" value="${jdbc.url}"/>
  18. <property name="user" value="${jdbc.username}"/>
  19. <property name="password" value="${jdbc.password}"/>
  20. </bean>
  21. <!--3 sqlSessionFactory-->
  22. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  23. <property name="dataSource" ref="datasource"/>
  24. <!--绑定MyBatis配置文件-->
  25. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  26. </bean>
  27. <!--4 配置dao接口扫描包 动态实现dao接口可以注入到Spring容器中-->
  28. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  29. <!--注入sqlSessionFactory-->
  30. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  31. <!-- 要扫描的包-->
  32. <property name="basePackage" value="com.qing.mapper"/>
  33. </bean>
  34. </beans>

5、配置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. <!--1 扫描service相关的bean -->
  10. <context:component-scan base-package="com.qing.service" />
  11. <!--2 BookServiceImpl注入到IOC容器中-->
  12. <bean id="BookServiceImpl" class="com.qing.service.BookServiceImpl">
  13. <property name="bookMapper" ref="bookMapper"/>
  14. </bean>
  15. <!--3 配置事务管理器 -->
  16. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  17. <!-- 注入数据库连接池 -->
  18. <property name="dataSource" ref="datasource" />
  19. </bean>
  20. </beans>

6、配置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. https://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. <!--1 注解驱动 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
  13. <context:component-scan base-package="com.qing.controller"/>
  14. <!--2 静态资源过滤 让Spring MVC不处理静态资源 .css .js .html等-->
  15. <mvc:default-servlet-handler />
  16. <!--3 支持mvc注解驱动-->
  17. <mvc:annotation-driven />
  18. <!--4 视图解析器 -->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  20. id="internalResourceViewResolver">
  21. <!-- 前缀 -->
  22. <property name="prefix" value="/WEB-INF/jsp/" />
  23. <!-- 后缀 -->
  24. <property name="suffix" value=".jsp" />
  25. </bean>
  26. </beans>

7、整合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="classpath:spring-dao.xml"/>
  7. <import resource="classpath:spring-service.xml"/>
  8. <import resource="classpath:spring-mvc.xml"/>
  9. </beans>

8、配置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. <!--1.注册DispatcherServlet-->
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <!--关联一个applicationContext.xml配置文件-->
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </init-param>
  15. <!--启动级别-1-->
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <!--/ 匹配所有的请求;(不包括.jsp)-->
  19. <!--/* 匹配所有的请求;(包括.jsp)-->
  20. <servlet-mapping>
  21. <servlet-name>springmvc</servlet-name>
  22. <url-pattern>/</url-pattern>
  23. </servlet-mapping>
  24. <!--解决乱码-->
  25. <filter>
  26. <filter-name>encoding</filter-name>
  27. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  28. <init-param>
  29. <param-name>encoding</param-name>
  30. <param-value>utf-8</param-value>
  31. </init-param>
  32. </filter>
  33. <filter-mapping>
  34. <filter-name>encoding</filter-name>
  35. <url-pattern>/*</url-pattern>
  36. </filter-mapping>
  37. <!--设置session15分钟过期-->
  38. <session-config>
  39. <session-timeout>15</session-timeout>
  40. </session-config>
  41. </web-app>

9、注意事项

(1)打开项目结构,点击Modules,选择项目下的Spring,查看四个配置文件是否都在ApplicationContext下

(2)Spring的三个配置文件必须整合到一起,使用import标签存放到一个applicationContext.xml中

(3)web.xml中注册DispatcherServlet时必须关联applicationContext.xml配置文件,这样才能加载所有的bean,包括service层的

10、Java代码

9.1 实体类

User实体类:

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

9.2 mapper(dao)层

BookMappe接口:

  1. public interface BookMapper {
  2. //增加一本书
  3. int addBook(Books book);
  4. //删除一本书
  5. int deleteBook(@Param("bookId") int id);
  6. //修改一本书
  7. int updateBook(Books books);
  8. //查询一本书
  9. Books queryBookById(@Param("bookId") int id);
  10. //查询所有
  11. List<Books> queryAllBook();
  12. //通过名字查询书籍
  13. Books queryBookByName(@Param("bookName") String bookName);
  14. }

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="com.qing.mapper.BookMapper">
  6. <insert id="addBook" parameterType="Books">
  7. insert into ssmbuild.books(bookName, bookCounts, detail)
  8. VALUES(#{bookName},#{bookCounts},#{detail})
  9. </insert>
  10. <delete id="deleteBook" parameterType="int">
  11. delete from ssmbuild.books where bookID=#{bookId}
  12. </delete>
  13. <update id="updateBook" parameterType="Books">
  14. update ssmbuild.books set bookName=#{bookName},
  15. bookCounts=#{bookCounts}, detail=#{detail} where bookID=#{bookID};
  16. </update>
  17. <select id="queryBookById" resultType="Books">
  18. select * from ssmbuild.books where bookID=#{bookId};
  19. </select>
  20. <select id="queryBookByName" resultType="Books">
  21. select * from ssmbuild.books where bookName=#{bookName};
  22. </select>
  23. <select id="queryAllBook" resultType="Books">
  24. select * from ssmbuild.books
  25. </select>
  26. </mapper>

9.3 service层

BookService接口:

  1. import com.qing.pojo.Books;
  2. import java.util.List;
  3. public interface BookService {
  4. //增加一本书
  5. int addBook(Books book);
  6. //删除一本书
  7. int deleteBook(int id);
  8. //修改一本书
  9. int updateBook(Books books);
  10. //查询一本书
  11. Books queryBookById(int id);
  12. //查询所有
  13. List<Books> queryAllBook();
  14. //通过名字查询书籍
  15. Books queryBookByName(String bookName);
  16. }

BookServiceImpl实现类:

  1. import com.qing.mapper.BookMapper;
  2. import com.qing.pojo.Books;
  3. import java.util.List;
  4. public class BookServiceImpl implements BookService {
  5. private BookMapper bookMapper;
  6. public void setBookMapper(BookMapper bookMapper) {
  7. this.bookMapper = bookMapper;
  8. }
  9. public BookMapper getBookMapper() {
  10. return bookMapper;
  11. }
  12. @Override
  13. public int addBook(Books book) {
  14. return bookMapper.addBook(book);
  15. }
  16. @Override
  17. public int deleteBook(int id) {
  18. return bookMapper.deleteBook(id);
  19. }
  20. @Override
  21. public int updateBook(Books books) {
  22. return bookMapper.updateBook(books);
  23. }
  24. @Override
  25. public Books queryBookById(int id) {
  26. return bookMapper.queryBookById(id);
  27. }
  28. @Override
  29. public List<Books> queryAllBook() {
  30. return bookMapper.queryAllBook();
  31. }
  32. @Override
  33. public Books queryBookByName(String bookName) {
  34. return bookMapper.queryBookByName(bookName);
  35. }
  36. }

9.4 controller层

BookController:

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

11、前端代码

10.1 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. }
  10. h3{
  11. width: 180px;
  12. height: 38px;
  13. margin: 100px auto;
  14. text-align: center;
  15. line-height: 38px;
  16. background: darkgoldenrod;
  17. border-radius: 5px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h3>
  23. <a href="${pageContext.request.contextPath}/book/allBook">进入所有书籍页面</a>
  24. </h3>
  25. </body>
  26. </html>

10.2 allBook书籍主页

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

10.3 AddBook.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>增加书籍</title>
  5. <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. </head>
  7. <body>
  8. <div class="container">
  9. <div class="row clearfix">
  10. <div class="col-md-12 column">
  11. <div class="page-header">
  12. <h1>
  13. <small>书籍列表---新增书籍</small>
  14. </h1>
  15. </div>
  16. </div>
  17. </div>
  18. <form action="${pageContext.request.contextPath}/book/addBook" method="post">
  19. <div class="form-group">
  20. <label for="bookName">书籍名称</label>
  21. <input type="text" name="bookName" class="form-control" id="bookName" required>
  22. </div>
  23. <div class="form-group">
  24. <label for="bookCount">书籍数量</label>
  25. <input type="text" name="bookCounts" class="form-control" id="bookCount" required>
  26. </div>
  27. <div class="form-group">
  28. <label for="bookDesc">书籍描述</label>
  29. <input type="text" name="detail" class="form-control" id="bookDesc">
  30. </div>
  31. <button type="submit" class="btn btn-default">添加</button>
  32. </form>
  33. </div>
  34. </body>
  35. </html>