目录

SSM整合案例:图书管理系统

Spring + SpringMVC + MyBatis+JSP+Servlet+简单前端知识

环境要求:

  • IDEA
  • MySQL 5.7.19
  • Tomcat 9
  • Maven 3.6

1、搭建数据库环境

创建一个存放书籍数据的数据库表:

CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books`(
`bookID` INT(10) PRIMARY KEY AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述'
); INSERT INTO `books`(`bookID`, `bookName`, `bookCounts`, `detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢'); SELECT * FROM books;

数据库连接 url:

jdbc:mysql:/主机:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

注:如果使用的是MySQL8.0+,url连接时增加一个时区的配置:serverTimezone=Asia/Shanghai

2、基本环境搭建

2.1、新建一个Maven项目,起名为:ssmbuild,添加web的支持

2.2、导入pom的相关依赖

<!--导入依赖 junit 数据库连接 数据库连接池 c3p0 Spring MyBatis mybatis-spring servlet jsp jstl-->

<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> <!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency> <!-- 数据库连接池 -->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency> <!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency> <!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency> <!--Servlet JSP-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>

2.3、Maven静态资源过滤设置

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

2.4、建立基本结构和配置框架!

java包:

  • com.rainszj.pojo
  • com.rainszj.dao
  • com.rainszj.service
  • com.rainszj.controller

resources包:

  • 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> </configuration>
  • 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
    http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
  • database.properties

3、MyBatis层编写

3.1、pojo包

  • Books
package com.rainszj.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@AllArgsConstructor
@NoArgsConstructor
public class Books { private int bookID;
private String bookName;
private int bookCounts;
private String detail; }

3.2、dao包

  • BookMapper
package com.rainszj.dao;

import com.rainszj.pojo.Books;
import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookMapper { /**
* 增加一本书
*
* @param books
* @return
*/
int addBook(Books books); /**
* 删除一本书
*
* @param id
* @return
*/
int deleteBook(@Param("bookId") int id); /**
* 修改一本书
*
* @param book
* @return
*/
int updateBook(Books book); /**
* 根据 id 查询一本书
*
* @param id
* @return
*/
Books queryBookById(@Param("bookId") int id); /**
* 查询所有的书
*
* @return
*/
List<Books> queryAllBook(); }
  • BookMapper.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.rainszj.dao.BookMapper"> <insert id="addBook" parameterType="Books">
insert into ssmbuild.books (bookName, bookCounts, detail)
values (#{bookName}, #{bookCounts}, #{detail})
</insert> <delete id="deleteBook" parameterType="int">
delete from ssmbuild.books where bookID = #{bookId}
</delete> <update id="updateBook" parameterType="Books">
update ssmbuild.books
set bookName = #{bookName}, bookCounts = #{bookCounts}, detail = #{detail}
where bookID = #{bookID}
</update> <select id="queryBookById" resultType="Books">
select * from ssmbuild.books where bookID = #{bookId}
</select> <select id="queryAllBook" resultType="Books">
select * from ssmbuild.books
</select> </mapper>

3.3、resources

  • 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> <!--配置数据源,交给Spring--> <!--其别名-->
<typeAliases>
<package name="com.rainszj.pojo"/>
</typeAliases> <!--注册Mapper-->
<mappers>
<mapper class="com.rainszj.dao"/>
</mappers> </configuration>
  • database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

3.4、service包

  • BookService
package com.rainszj.service;

import com.rainszj.pojo.Books;

import java.util.List;

public interface BookService {

    /**
* 增加一本书
*
* @param books
* @return
*/
int addBook(Books books); /**
* 删除一本书
*
* @param id
* @return
*/
int deleteBook(int id); /**
* 修改一本书
*
* @param book
* @return
*/
int updateBook(Books book); /**
* 根据 id 查询一本书
*
* @param id
* @return
*/
Books queryBookById(int id); /**
* 查询所有的书
*
* @return
*/
List<Books> queryAllBook(); }
  • BookServiceImpl
package com.rainszj.service;

import com.rainszj.dao.BookMapper;
import com.rainszj.pojo.Books; import java.util.List; public class BookServiceImpl implements BookService { // service层调dao层,使用组合dao
private BookMapper bookMapper; // 使用Spring管理,实现set注入
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
} public int addBook(Books books) {
return bookMapper.addBook(books);
} public int deleteBook(int id) {
return bookMapper.deleteBook(id);
} public int updateBook(Books book) {
return bookMapper.updateBook(book);
} public Books queryBookById(int id) {
return queryBookById(id);
} public List<Books> queryAllBook() {
return queryAllBook();
}
}

4、Spring层编写

4.1、spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/> <!--2.连接池
dbcp:半自动化操作(不能自动加载文件)
c3p0:自动化操作(自动加载配置文件,并且自动设置到对象中)
druid,hikari
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--配置连接池的属性-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/> <!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean> <!--3.SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置mybatis配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.rainszj.dao"/>
</bean> </beans>

4.2、spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1.扫描service相关的bean -->
<context:component-scan base-package="com.rainszj.service"/> <!--2.将我们所有的业务类,注入到Spring中,可以通过配置或者注解实现-->
<bean id="BookServiceImpl" class="com.rainszj.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean> <!--3.声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/> </bean> <!--4.aop事务支持--> </beans>

5、Spring MVC层编写

5.1、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!--注册DispatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--Spring MVC内置的过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config> </web-app>

5.2、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
http://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
https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.注解驱动-->
<mvc:annotation-driven/>
<!--2.静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3.自动扫描包:controller-->
<context:component-scan base-package="com.rainszj.controller"/> <!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>

底层代码编写完毕,下面只需要编写 Controller 层和 视图层。

6、查询书籍功能

6.1、BookController 类编写 , 方法一:查询全部书籍

@Controller
@RequestMapping("/book")
public class BookController {
// Controller 层调 Service 层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService; /**
* 查询所有书籍
* @param model
* @return
*/
@RequestMapping("/allBook")
public String list(Model model) {
List<Books> list = bookService.queryAllBook();
model.addAttribute("list", list); return "allBook";
}
}

6.2、编写首页 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title> <style> a {
text-decoration: none;
color: #000;
font-size: 18px;
} h3 {
width: 180px;
height: 38px;
margin: 200px auto;
text-align: center;
line-height: 38px;
background-color: deepskyblue;
border-radius: 5px;
}
</style> </head>
<body> <h3>
<a href="${pageContext.request.contextPath}/book/allBook">点击到书籍列表</a>
</h3> </body>
</html>

6.3、添加书籍列表页面 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.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style> </style>
</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> <div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thread>
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍详情</th>
</tr>
</thread> <tbody>
<c:forEach var="book" items="${requestScope.get('list')}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div> </div> </body>
</html>

7、添加书籍

7.1、BookController 类编写 , 方法二:添加书籍

/**
* 跳转到修改页面,并根据 id回显数据
*
* @param id Book id
* @param model 传给前端的数据
* @return
*/
@RequestMapping("/toUpdate/{bookId}")
public String toUpdatePaper(@PathVariable("bookId") int id, Model model) {
Books book = bookService.queryBookById(id);
model.addAttribute("QBook", book); System.out.println(book);
return "updateBook";
} /**
* 处理修改请求
*
* @param book 前端传递的对象
* @return
*/
@RequestMapping("/updateBook")
public String updateBook(Books book) { int res = bookService.updateBook(book); System.out.println(res);
if (res > 0) {
System.out.println("updateBook=>执行成功" + book);
} return "redirect:/book/allBook";
}

7.2、在allBook.jsp中添加新增连接

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

7.3、添加书籍页面:addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加书籍</title>
<%--BootStrap 美化界面--%>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head>
<body> <div class="container"> <%--row clearfix 清除浮动--%>
<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="bookCounts">书籍数量</label>
<input type="text" name="bookCounts" class="form-control" id="bookCounts" required>
</div> <div class="form-group">
<label for="detail">书籍描述</label>
<input type="text" name="detail" class="form-control" id="detail" required>
</div> <input type="submit" class="form-control" value="添加">
</form> </div> </body>
</html>

8、修改和删除书籍

8.1、BookController 类编写 , 方法三:修改书籍和删除书籍

    /**
* 跳转到修改页面,并根据 id回显数据
*
* @param id Book id
* @param model 传给前端的数据
* @return
*/
@RequestMapping("/toUpdate/{bookId}")
public String toUpdatePaper(@PathVariable("bookId") int id, Model model) {
Books book = bookService.queryBookById(id);
model.addAttribute("QBook", book); System.out.println(book);
return "updateBook";
} /**
* 处理修改请求
*
* @param book 前端传递的对象
* @return
*/
@RequestMapping("/updateBook")
public String updateBook(Books book) {
int res = bookService.updateBook(book);
System.out.println(res);
if (res > 0) {
System.out.println("updateBook=>执行成功" + book);
} return "redirect:/book/allBook";
} /**
* 删除一本书
* @param id
* @return
*/
@RequestMapping("/deleteBook/{bookId}")
public String deleteBook(@PathVariable("bookId") int id) { bookService.deleteBook(id);
return "redirect:/book/allBook";
}

8.2、在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.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head>
<body> <div class="container"> <%--row clearfix 清除浮动--%>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 —————— 显示所有书籍</small>
</h1>
</div>
</div>
</div> <div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
</div>
</div> <div class="row clearfix">
<div class="col-md-12 column">
<%--
table-hover: 鼠标滑过时,变色
table-striped: 每一行显示不同的颜色
--%>
<table class="table table-hover table-striped">
<thread>
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thread> <tbody>
<c:forEach var="book" items="${requestScope.get('list')}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<%--传统方式--%>
<%--
<a href="${pageContext.request.contextPath}/book/toUpdate?id=${book.bookID}">修改</a>
&nbsp; | &nbsp;
<a href="${pageContext.request.contextPath}/book/deleteBook?id=${book.bookID}">删除</a>
--%>
<%--RestFul风格--%>
<a href="${pageContext.request.contextPath}/book/toUpdate/${book.bookID}">修改</a>
&nbsp; | &nbsp;
<a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div> </div> </body>
</html>

8.3、添加修改书籍页面 updateBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改书籍</title>
<%--BootStrap 美化界面--%>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head>
<body> <div class="container"> <%--row clearfix 清除浮动--%>
<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"> <%--提交隐藏域,用于提交要修改书的 id--%>
<input type="hidden" name="bookID" value="${QBook.bookID}"> <div class="form-group">
<label for="bookName">书籍名称</label>
<input type="text" name="bookName" class="form-control" id="bookName" value="${QBook.bookName}">
</div> <div class="form-group">
<label for="bookCounts">书籍数量</label>
<input type="text" name="bookCounts" class="form-control" id="bookCounts" value="${QBook.bookCounts}">
</div> <div class="form-group">
<label for="detail">书籍描述</label>
<input type="text" name="detail" class="form-control" id="detail" value="${QBook.detail}">
</div> <input type="submit" class="form-control" value="修改">
</form> </div> </body>
</html>

9、搜索功能

9.1、在allBook.jsp中添加查询书籍功能

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

9.2、在BookController类中编写处理查询书名的请求

/**
* 根据书名查询一本书
*
* @param queryBookName
* @param model
* @return
*/
@RequestMapping("/queryBook")
public String queryBook(String queryBookName, Model model) {
List<Books> list = bookService.queryBookByName(queryBookName); // System.err.println(list); if (list.isEmpty()) {
list = bookService.queryAllBook();
model.addAttribute("error", "未找到!");
} model.addAttribute("list", list); return "allBook";
}

9.3、编写BookMapper接口

/**
* 根据书名查询一本书
*
* @param bookName
* @return
*/
List<Books> queryBookByName(@Param("bookName") String bookName);

9.4、在BookMapper.xml编写Sql

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

9.5、编写BookServicer接口

/**
* 根据书名查询一本书
*
* @param bookName
* @return
*/
List<Books> queryBookByName(String bookName);

9.6、编写BookServicerImpl

public List<Books> queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}

10、项目结构:

11、注意事项

web.xml 中使用总的Spring配置文件!Spring MVC的内置过滤器要设置它的 encoding 属性!

CharacterEncodingFilter 源码中的 encoding 属性

注意静态资源过滤问题!

在项目的发布环境中添加lib依赖!

确保Spring Spring MVC 的配置文件在一个上下文中,将他们关联在一起!

MapperScannerConfigurer

Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

SSM整合案例:图书管理系统的更多相关文章

  1. 08 SSM整合案例(企业权限管理系统):07.订单操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 07.订单操作 SSM订单操作 ...

  2. 08 SSM整合案例(企业权限管理系统):05.SSM整合案例的基本介绍

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 05.SSM整合案例的基本介绍 ...

  3. 08 SSM整合案例(企业权限管理系统):06.产品操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 06.产品操作 SSM 环境搭 ...

  4. 08 SSM整合案例(企业权限管理系统):09.用户和角色操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 09.用户和角色操作 1. 用 ...

  5. 08 SSM整合案例(企业权限管理系统):08.权限控制

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户操作 10.权限关联与控制 11.AOP日志 08.权限控制 SSM权限操作 ...

  6. 08 SSM整合案例(企业权限管理系统):10.权限关联与控制

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户操作 10.权限关联与控制 11.AOP日志 10.权限关联与控制 1.用户 ...

  7. 08 SSM整合案例(企业权限管理系统):11.AOP日志

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 11.AOP日志 1.数据库与 ...

  8. SSM整合案例

    使用IDEA整合SSM spring核心配置文件:beans_core.xml/applicationContext.xml <?xml version="1.0" enco ...

  9. SSM整合案例--用户登录

    实现用户登录案例,并进行非法拦截 实现当用户未登录时,无法跳转到出登录页面以外的任何页面,拦截用户仍在登陆页面:当用户登录成功即可跳转到其他页面 (1)导入依赖 <!-- https://mvn ...

随机推荐

  1. Java的多线程编程模型5--从AtomicInteger开始

    Java的多线程编程模型5--从AtomicInteger开始 2011-06-23 20:50 11393人阅读 评论(9) 收藏 举报 java多线程编程jniinteger测试 AtomicIn ...

  2. ValidForm.js的使用注意点

    dataType的值不能为"", 否则会导致错误发生:Uncaught TypeError: Cannot read property '0' of null,http请求可以发送 ...

  3. Golang——详解Go语言代码规范

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Golang专题的第二篇,我们来看看Go的语言规范. 在我们继续今天的内容之前,先来回答一个问题. 有同学在后台问我,为什么说Gola ...

  4. windows批处理protoc生成C++代码

    1 首先需要生成protoc的可执行文件,具体可以参考  https://www.cnblogs.com/cnxkey/articles/10152646.html 2 将单个protoc文件生成.h ...

  5. Django开发文档-域用户集成登录

    项目概述: 一般在企业中,用户以WINDOWS的域用户统一的管理,所以以Django快速开发的应用,不得不集成AD域登录. 网上一般采用django-python3-ldap的库来做集成登录,但是本方 ...

  6. SQL入门,就这么简单

    随着时代的发展,人类活动产生的信息越来越多,大家常说,现在这个时代是大数据时代.在这样一个前提下,数据的存储成为我们必须要认真对待和研究的问题了.SQL(Structured Query Langua ...

  7. CKEditor与定制

    一 开始使用 官网 基本示例: 搭建服务器(这里使用apache) 下载standard的ckeditor解压放在apache的htdocs的目录下 在htdoc下面新建index.html,写入代码 ...

  8. python 工具链 虚拟环境和包管理工具 pipenv

    Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, ...

  9. 2层感知机(神经网络)实现非线性回归(非线性拟合)【pytorch】

    import torch import numpy import random from torch.autograd import Variable import torch.nn.function ...

  10. Nginx+Fastdfs

    注: 在配置时,使用非root用户配置 fdfs/fdfs 1.    集群部署 1.1.    准备 创建目录:本文档中所有内容安装到/fdfs目录 [fdfs@5861be93b5b0 /]$mk ...