项目介绍

  • 使用SSM框架搭建出一套简单的CRUD项目示例

技术点

  • 基础框架-SSM(Spring+SpringMVC+Mybatis)
  • 数据库-MySQL
  • 前端框架-Bootstrap
  • 依赖管理-Maven

环境搭建

数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

meavn工程

  • 设置打包方式为war

  • 设置meavn为本地仓库

  • 设置web.xml

  • 导入jar包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    <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>

    <!-- 数据库连接池 -->
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</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>

    <!--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.2</version>
    </dependency>

    <!--Spring-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.9.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.9.RELEASE</version>
    </dependency>
    <!-- Spring5和Thymeleaf整合包 -->

    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.12.RELEASE</version>
    </dependency>
    <!--日志文件-->

    <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
    </dependency>
    </dependencies>
  • 基本框架结构

    • dao
    • service
    • pojo
    • controller

配置文件

web.xml

  • 配置dispatcherServlet 配置路径选择为spring也就是最大的配置文件域
  • 配置字符编码过滤器
  • session时间等等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- dispatcherServlet注意初始化的时候绑定一个更大的,因为配置文件分开了只有mvc的话是没有bean的-->
<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>
<!-- 乱码过滤-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>

</web-app>

数据库配置

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

mybatis-config

  • 设置别名
  • 引入mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.me.pojo"/>
</typeAliases>

<!--引入映射文件-->
<mappers>
<mapper resource="com/me/dao/BookMapper.xml"/>
</mappers>
</configuration>

配置bean的时候注意id小写

spring-dao

  • 连接数据库信息
  • 数据库连接池
  • SqlSessionFactory
  • 扫描dao包下的类,交给spring来管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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.连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<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"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.me.dao"/>
</bean>

</beans>

spring-service

  • 扫描service包的类,交给spring来管理
  • 事务相关配置(暂时没有配)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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">

<!-- 扫描service下的类-->
<context:component-scan base-package="com.me.service"/>
<!-- 注入属性-->
<bean id="bookServiceImpl" class="com.me.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>

</bean>
<!--声明式事务配置-->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
<!-- <property name="dataSource" value="dataSource"/>-->
<!-- </bean>-->
</beans>

spring-mvc

  • 开启注解驱动
  • 静态资源过滤
  • 扫描controller下的类,交给spring来管理
  • 视图解析器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 1.注解驱动-->
<mvc:annotation-driven/>
<!-- 2.静态资源过滤-->
<mvc:default-servlet-handler/>
<!-- 3.扫描包-->
<context:component-scan base-package="com.me.controller"/>
<!-- 4.视图解析器-->
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>

<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>

spring(总配置文件)

  • applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
<?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">

<!-- 注意项目结构是否正确,导入的是否正确-->
<import resource="classpath:spring-service.xml"></import>
<import resource="classpath:spirng-dao.xml"></import>
<import resource="classpath:spring-mvc.xml"></import>

</beans>
  • 配置文件除了web.xml全部放在resource目录下交给spring来处理

编写实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.me.pojo;

/**
* @Author Maple
* @Date 2022/5/31 18:45
* @Version 1.0
*/
public class Books {

private Integer bookID;

private String bookName;

private Integer bookCounts;

private String detail;

public Books() {
}

public Books(Integer bookID, String bookName, Integer bookCounts, String detail) {
this.bookID = bookID;
this.bookName = bookName;
this.bookCounts = bookCounts;
this.detail = detail;
}

public Integer getBookID() {
return bookID;
}

public void setBookID(Integer bookID) {
this.bookID = bookID;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public Integer getBookCounts() {
return bookCounts;
}

public void setBookCounts(Integer bookCounts) {
this.bookCounts = bookCounts;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

@Override
public String toString() {
return "Books{" +
"bookID=" + bookID +
", bookName='" + bookName + '\'' +
", bookCounts=" + bookCounts +
", detail='" + detail + '\'' +
'}';
}
}

编写DAO

接口

  • 注意@Param注解与sql对应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface BookMapper {
//增加一本书
int addBook(Books books);
//通过id删除一本书
int deleteBookById(@Param("bookId") int id);
//更新一本书
int updateBook(Books books);
//通过id查询一本书
Books queryBookById(@Param("bookId") int id);
//查询全部书籍
List<Books> queryAllBook();
//通过名称查询
Books queryBookByName(@Param("bookName") String bookName);
}

实现类(mapper)

  • 注意namespace与接口对应
  • 注意sql的id与方法名对应
  • 返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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.me.dao.BookMapper">

<!-- int addBook(Books books);-->
<insert id="addBook" parameterType="Books">
insert into ssmbuild.books(bookName, bookCounts, detail)
values (#{bookName},#{bookCounts},#{detail})
</insert>
<!-- int deleteBookById(int id);-->
<delete id="deleteBookById" parameterType="int">
delete from ssmbuild.books where bookID=#{bookId}
</delete>

<!-- int updateBook(Books books);-->
<update id="updateBook" parameterType="Books">
update ssmbuild.books
set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID=#{bookID};
</update>
<!-- Books queryBookById(int id);-->
<select id="queryBookById" resultType="Books">
select * from ssmbuild.books where bookID=#{bookId}
</select>
<!-- List<Books> queryAllBook();-->
<select id="queryAllBook" resultType="Books">
select * from ssmbuild.books
</select>
<!-- Books queryBookByName(@Param("bookName") String bookName);-->
<select id="queryBookByName" resultType="Books">
select * from ssmbuild.books where bookName=#{bookName}
</select>
</mapper>

编写Service

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public interface BookService {
//C
int addBook(Books books);
//D
int deleteBookById(int id);
//U
int updateBook(Books books);
//R
Books queryBookById(int id);
//R
List<Books> queryAllBook();

Books queryBookByName(String bookName);
}

实现类

  • service调用dao也就是mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class BookServiceImpl implements BookService{

private BookMapper bookMapper;

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

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

public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}

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

public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}

public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}

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

编写前端页面和控制器

  • 注意html引入Thymeleaf
1
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  • @Autowired:根据属性类型进行自动装配
  • @Qualifier:根据名称进行注入 这个@Qualifier 注解的使用,和上面@Autowired 一起使用

首页

页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<style>
a{
text-decoration: none;
color: black;
font-size: 18px;
}
</style>
<body>
<h1>你好</h1>

<a th:href="@{/allBook}">查询所有书籍</a>

</body>
</html>

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public class BookController {

@Autowired
@Qualifier("bookServiceImpl")
private BookService bookService;

//返回首页
@RequestMapping("/")
public String index(){
return "index";
}

}

书籍显示

  • 包含了以下跳转连接或按钮
    • 添加书籍
    • 删除书籍
    • 修改书籍
    • 查询书籍
    • 显示书籍

页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>全部书籍</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表----显示所有书籍</small>
</h1>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" th:href="@{/toAddBook}">新增书籍</a>
<a class="btn btn-primary" th:href="@{/allBook}">显示全部书籍</a>
</div>
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<form th:action="@{/queryBook}" method="post" style="float: right">
<input type="text" name="theBookName" class="form-control" placeholder="请输入查询书籍的名称" required>
<input type="submit" class="btn btn-primary" value="查询">
</form>
</div>
</div>
</div>
</div>

<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>具体操作</th>
</thead>

<tbody>
<tr th:each="allbook: ${bookList}">
<td th:text="${allbook.bookID}"></td>
<td th:text="${allbook.bookName}"></td>
<td th:text="${allbook.bookCounts}"></td>
<td th:text="${allbook.detail}"></td>
<td>
<a th:href="@{/toUpdateBook/}+${allbook.bookID}">修改</a> &nbsp;|&nbsp;
<a th:href="@{/deleteBook/}+${allbook.bookID}">删除</a>
</td>
</tr>
</tbody>
</table>

</div>
</div>

</body>
</html>

控制器

1
2
3
4
5
6
7
//查询全部书籍,并且返回到一个书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List<Books> books = bookService.queryAllBook();
model.addAttribute("bookList", books);
return "books";
}

添加书籍

  • 跳转过来的页面

页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加书籍</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增书籍</small>
</h1>
</div>
</div>
</div>
</div>

<form th:action="@{/addOneBook}" method="post">
<div class="form-group">
<label >书籍名称</label>
<input type="text" name="bookName" class="form-control" required>
</div>
<div class="form-group">
<label >书籍数量</label>
<input type="text" name="bookCounts" class="form-control" required>
</div>
<div class="form-group">
<label >书籍描述</label>
<input type="text" name="detail" class="form-control" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加" >
</div>
</form>

</body>
</html>

控制器

1
2
3
4
5
6
7
//增加书籍
@RequestMapping("/addOneBook")
public String addBook(Books books){
System.out.println("add->"+books.toString());
bookService.addBook(books);
return "redirect:/allBook";
}

删除书籍

控制器

1
2
3
4
5
@RequestMapping("/deleteBook/{id}")
public String deleteTheBook(@PathVariable("id") Integer bookId){
bookService.deleteBookById(bookId);
return "redirect:/allBook";
}

修改书籍

  • 跳转过来时注意回显数据

  • 注意标签设置为 disabled时候是不能传递参数的,需要用隐藏域来传输

页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改书籍</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改书籍</small>
</h1>
</div>
</div>
</div>
</div>

<form th:action="@{/updateOneBook}" method="post">

<!-- 使用隐藏域来传递-->
<input type="hidden" name="BookID" th:value="${theBook.bookID}">
<div class="form-group">
<label >书籍ID</label>
<!-- 设置为disable时无法传递值-->
<input type="text" name="showBookID" class="form-control" th:value="${theBook.bookID}" disabled>
</div>
<div class="form-group">
<label >书籍名称</label>
<input type="text" name="bookName" class="form-control" th:value="${theBook.bookName}">
</div>
<div class="form-group">
<label >书籍数量</label>
<input type="text" name="bookCounts" class="form-control" th:value="${theBook.bookCounts}">
</div>
<div class="form-group">
<label >书籍描述</label>
<input type="text" name="detail" class="form-control" th:value="${theBook.detail}">
</div>
<div class="form-group">
<input type="submit" class="form-control" value="修改" >
</div>
</form>
</body>
</html>

控制器

1
2
3
4
5
6
7
//更新书籍
@RequestMapping("/updateOneBook")
public String updateBook(Books theBook){
System.out.println("save->"+theBook.toString());
bookService.updateBook(theBook);
return "redirect:/allBook";
}

通过名称查询书籍

控制器

1
2
3
4
5
6
7
8
9
10
11
@RequestMapping("/queryBook")
public String queryBook(@RequestParam(value = "theBookName") String queryBookName , Model model){
Books book = bookService.queryBookByName(queryBookName);

if (book!=null){
List<Books> books =new ArrayList<Books>();
books.add(book);
model.addAttribute("bookList", books);
}
return "books";
}

总控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@Controller
public class BookController {

@Autowired
@Qualifier("bookServiceImpl")
private BookService bookService;

@RequestMapping("/")
public String index(){
return "index";
}

//查询全部书籍,并且返回到一个书籍展示页面
@RequestMapping("/allBook")
public String list(Model model){
List<Books> books = bookService.queryAllBook();
model.addAttribute("bookList", books);
return "books";
}

//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPage(){
return "addBook";
}

//增加书籍
@RequestMapping("/addOneBook")
public String addBook(Books books){
System.out.println("add->"+books.toString());
bookService.addBook(books);
return "redirect:/allBook";
}

//跳转到修改书籍页面
@RequestMapping("/toUpdateBook/{id}")
public String toUpdatePage(@PathVariable("id") Integer bookId,Model model){
Books books=bookService.queryBookById(bookId);
model.addAttribute("theBook", books);
return "updateBook";
}

//更新书籍
@RequestMapping("/updateOneBook")
public String updateBook(Books theBook){
System.out.println("save->"+theBook.toString());
bookService.updateBook(theBook);
return "redirect:/allBook";
}

//删除书籍
@RequestMapping("/deleteBook/{id}")
public String deleteTheBook(@PathVariable("id") Integer bookId){
bookService.deleteBookById(bookId);
return "redirect:/allBook";
}

@RequestMapping("/queryBook")
public String queryBook(@RequestParam(value = "theBookName") String queryBookName , Model model){
Books book = bookService.queryBookByName(queryBookName);

if (book!=null){
List<Books> books =new ArrayList<Books>();
books.add(book);
model.addAttribute("bookList", books);
}
return "books";
}

}