服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

2020-12-02 14:35小疯的代码健身房 Java教程

使用SSM(Spring、SpringMVC和Mybatis)已经有段时间了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,下面这篇文章主要给大家介绍了关于整合SSM框架:Spring MVC + Spring + MyBatis的相关资料,需要的朋友可以

前言

ssm(spring+springmvc+mybatis)是目前较为主流的企业级架构方案,不知道大家有没有留意,在我们看招聘信息的时候,经常会看到这一点,需要具备ssh框架的技能;而且在大部分教学课堂中,也会把ssh作为最核心的教学内容。

但是,我们在实际应用中发现,springmvc可以完全替代struts,配合注解的方式,编程非常快捷,而且通过restful风格定义url,让地址看起来非常优雅。

另外,mybatis也可以替换hibernate,正因为mybatis的半自动特点,我们程序猿可以完全掌控sql,这会让有数据库经验的程序猿能开发出高效率的sql语句,而且xml配置管理起来也非常方便。

好了,如果你也认同我的看法,那么下面我们一起来做整合吧!话不多说,来一起看看详细的介绍:

在写代码之前我们先了解一下这三个框架分别是干什么的?

相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码!

  1. springmvc:它用于web层,相当于controller(等价于传统的servlet和struts的action),用来处理用户请求。举个例子,用户在地址栏输入http://网站域名/login,那么springmvc就会拦截到这个请求,并且调用controller层中相应的方法,(中间可能包含验证用户名和密码的业务逻辑,以及查询数据库操作,但这些都不是springmvc的职责),最终把结果返回给用户,并且返回相应的页面(当然也可以只反馈josn/xml等格式数据)。springmvc就是做前面和后面过程的活,与用户打交道!!
  2. spring:太强大了,以至于我无法用一个词或一句话来概括它。但与我们平时开发接触最多的估计就是ioc容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用这个类的时候为它初始化,很少看到关键字new。另外spring的aop,事务管理等等都是我们经常用到的。
  3. mybatis:如果你问我它跟鼎鼎大名的hibernate有什么区别?我只想说,他更符合我的需求。第一,它能自由控制sql,这会让有数据库经验的人(当然不是说我啦~捂脸~)编写的代码能搞提升数据库访问的效率。第二,它可以使用xml的方式来组织管理我们的sql,因为一般程序出错很多情况下是sql出错,别人接手代码后能快速找到出错地方,甚至可以优化原来写的sql。

ssm框架整合配置

好了,前面bb那么多,下面我们真正开始敲代码了~

首先我们打开ied,我这里用的是eclipse(你们应该也是用的这个,对吗?),创建一个动态web项目,建立好相应的目录结构(重点!)

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

(打了马赛克是因为这里还用不到,你们不要那么污好不好?)

我说一下每个目录都有什么用吧(第一次画表格,我发现markdown的表格语法很不友好呀~)

这个目录结构同时也遵循maven的目录规范~

 

文件名 作用
src 根目录,没什么好说的,下面有main和test。
main 主要目录,可以放java代码和一些资源文件。
java 存放我们的java代码,这个文件夹要使用build path -> use as source folder,这样看包结构会方便很多,新建的包就相当于在这里新建文件夹咯。
resources 存放资源文件,譬如各种的spring,mybatis,log配置文件。
mapper 存放dao中每个方法对应的sql,在这里配置,无需写daoimpl。
spring 这里当然是存放spring相关的配置文件,有dao service web三层。
sql 其实这个可以没有,但是为了项目完整性还是加上吧。
webapp 这个貌似是最熟悉的目录了,用来存放我们前端的静态资源,如jsp js css。
resources 这里的资源是指项目的静态资源,如js css images等。
web-inf 很重要的一个目录,外部浏览器无法访问,只有羡慕内部才能访问,可以把jsp放在这里,另外就是web.xml了。你可能有疑问了,为什么上面java中的resources里面的配置文件不妨在这里,那么是不是会被外部窃取到?你想太多了,部署时候基本上只有webapp里的会直接输出到根目录,其他都会放入web-inf里面,项目内部依然可以使用classpath:xxx来访问,好像ide里可以设置部署输出目录,这里扯远了~
test 这里是测试分支。
java 测试java代码,应遵循包名相同的原则,这个文件夹同样要使用build path -> use as source folder,这样看包结构会方便很多。

 

resources 没什么好说的,好像也很少用到,但这个是maven的规范。

我先新建好几个必要的包,并为大家讲解一下每个包的作用,顺便理清一下后台的思路~

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

 

包名 名称 作用
dao 数据访问层(接口) 与数据打交道,可以是数据库操作,也可以是文件读写操作,甚至是redis缓存操作,总之与数据操作有关的都放在这里,也有人叫做dal或者数据持久层都差不多意思。为什么没有daoimpl,因为我们用的是mybatis,所以可以直接在配置文件中实现接口的每个方法。
entity 实体类 一般与数据库的表相对应,封装dao层取出来的数据为一个对象,也就是我们常说的pojo,一般只在dao层与service层之间传输。
dto 数据传输层 刚学框架的人可能不明白这个有什么用,其实就是用于service层与web层之间传输,为什么不直接用entity(pojo)?其实在实际开发中发现,很多时间一个entity并不能满足我们的业务需求,可能呈现给用户的信息十分之多,这时候就有了dto,也相当于vo,记住一定不要把这个混杂在entity里面,答应我好吗?
service 业务逻辑(接口) 写我们的业务逻辑,也有人叫bll,在设计业务接口时候应该站在“使用者”的角度。额,不要问我为什么这里没显示!ide调皮我也拿它没办法~
serviceimpl 业务逻辑(实现) 实现我们业务接口,一般事务控制是写在这里,没什么好说的。
web 控制器 springmvc就是在这里发挥作用的,一般人叫做controller控制器,相当于struts中的action。

 

还有最后一步基础工作,导入我们相应的jar包,我使用的是maven来管理我们的jar,所以只需要在poom.xml中加入相应的依赖就好了,如果不使用maven的可以自己去官网下载相应的jar,放到项目web-inf/lib目录下。关于maven的学习大家可以看慕课网的视频教程,这里就不展开了。我把项目用到的jar都写在下面,版本都不是最新的,大家有经验的话可以自己调整版本号。另外,所有jar都会与项目一起打包放到我的上,喜欢的给个star吧~

poom.xml

 
?
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>com.soecode.ssm</groupid>
 ssm</artifactid>
 <packaging>war</packaging>
 <version>0.0.1-snapshot</version>
 <name>ssm maven webapp</name>
 <url>http://github.com/liyifeng1994/ssm</url>
 <dependencies>
 <!-- 单元测试 -->
 <dependency>
 <groupid>junit</groupid>
 junit</artifactid>
 <version>4.11</version>
 </dependency>
 
 <!-- 1.日志 -->
 <!-- 实现slf4j接口并整合 -->
 <dependency>
 <groupid>ch.qos.logback</groupid>
 logback-classic</artifactid>
 <version>1.1.1</version>
 </dependency>
 
 <!-- 2.数据库 -->
 <dependency>
 <groupid>mysql</groupid>
 mysql-connector-java</artifactid>
 <version>5.1.37</version>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupid>c3p0</groupid>
 c3p0</artifactid>
 <version>0.9.1.2</version>
 </dependency>
 
 <!-- dao: mybatis -->
 <dependency>
 <groupid>org.mybatis</groupid>
 mybatis</artifactid>
 <version>3.3.0</version>
 </dependency>
 <dependency>
 <groupid>org.mybatis</groupid>
 mybatis-spring</artifactid>
 <version>1.2.3</version>
 </dependency>
 
 <!-- 3.servlet web -->
 <dependency>
 <groupid>taglibs</groupid>
 standard</artifactid>
 <version>1.1.2</version>
 </dependency>
 <dependency>
 <groupid>jstl</groupid>
 jstl</artifactid>
 <version>1.2</version>
 </dependency>
 <dependency>
 <groupid>com.fasterxml.jackson.core</groupid>
 jackson-databind</artifactid>
 <version>2.5.4</version>
 </dependency>
 <dependency>
 <groupid>javax.servlet</groupid>
 javax.servlet-api</artifactid>
 <version>3.1.0</version>
 </dependency>
 
 <!-- 4.spring -->
 <!-- 1)spring核心 -->
 <dependency>
 <groupid>org.springframework</groupid>
 spring-core</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <dependency>
 <groupid>org.springframework</groupid>
 spring-beans</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <dependency>
 <groupid>org.springframework</groupid>
 spring-context</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <!-- 2)spring dao层 -->
 <dependency>
 <groupid>org.springframework</groupid>
 spring-jdbc</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <dependency>
 <groupid>org.springframework</groupid>
 spring-tx</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <!-- 3)spring web -->
 <dependency>
 <groupid>org.springframework</groupid>
 spring-web</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <dependency>
 <groupid>org.springframework</groupid>
 spring-webmvc</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 <!-- 4)spring test -->
 <dependency>
 <groupid>org.springframework</groupid>
 spring-test</artifactid>
 <version>4.1.7.release</version>
 </dependency>
 
 <!-- redis客户端:jedis -->
 <dependency>
 <groupid>redis.clients</groupid>
 jedis</artifactid>
 <version>2.7.3</version>
 </dependency>
 <dependency>
 <groupid>com.dyuproject.protostuff</groupid>
 <artifactid>protostuff-core</artifactid>
 <version>1.0.8</version>
 </dependency>
 <dependency>
 <groupid>com.dyuproject.protostuff</groupid>
 <artifactid>protostuff-runtime</artifactid>
 <version>1.0.8</version>
 </dependency>
 
 <!-- map工具类 -->
 <dependency>
 <groupid>commons-collections</groupid>
 <artifactid>commons-collections</artifactid>
 <version>3.2</version>
 </dependency>
 </dependencies>
 <build>
 <finalname>ssm</finalname>
 </build>
</project>

 

下面真的要开始进行编码工作了,坚持到这里辛苦大家了~

第一步:我们先在spring文件夹里新建spring-dao.xml文件,因为spring的配置太多,我们这里分三层,分别是dao service web。

1、读入数据库连接相关参数(可选)

2、配置数据连接池

     配置连接属性,可以不读配置项文件直接在这里写死

     配置c3p0,只配了几个常用的

3、配置sqlsessionfactory对象(mybatis)

4、扫描dao层接口,动态实现dao接口,也就是说不需要daoimpl,sql和参数都写在xml文件上

spring-dao.xml

 
?
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
<?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
 http://www.springframework.org/schema/context/spring-context.xsd">
 <!-- 配置整合mybatis过程 -->
 <!-- 1.配置数据库相关参数properties的属性:${url} -->
 <context:property-placeholder location="classpath:jdbc.properties" />
 
 <!-- 2.数据库连接池 -->
 <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" />
 <!-- 配置mybaties全局配置文件:mybatis-config.xml -->
 <property name="configlocation" value="classpath:mybatis-config.xml" />
 <!-- 扫描entity包 使用别名 -->
 <property name="typealiasespackage" value="com.soecode.lyf.entity" />
 <!-- 扫描sql配置文件:mapper需要的xml文件 -->
 <property name="mapperlocations" value="classpath:mapper/*.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.soecode.lyf.dao" />
 </bean>
</beans>

因为数据库配置相关参数是读取配置文件,所以在resources文件夹里新建一个jdbc.properties文件,存放我们4个最常见的数据库连接属性,这是我本地的,大家记得修改呀~还有喜欢传到github上“大头虾们”记得删掉密码,不然别人就很容易得到你服务器的数据库配置信息,然后干一些羞羞的事情,你懂的!!

jdbc.properties

 
?
1
 
2
3
4
jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3307/ssm?useunicode=true&characterencoding=utf8
jdbc.username=root
jdbc.password=

友情提示:配置文件中的jdbc.username,如果写成username,可能会与系统环境中的username变量冲突,所以到时候真正连接数据库的时候,用户名就被替换成系统中的用户名(有得可能是administrator),那肯定是连接不成功的,这里有个小坑,我被坑了一晚上!!

因为这里用到了mybatis,所以需要配置mybatis核心文件,在recources文件夹里新建mybatis-config.xml文件。

  • 使用自增主键
  • 使用列别名
  • 开启驼峰命名转换 create_time -> createtime

mybatis-config.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>
 <!-- 配置全局属性 -->
 <settings>
 <!-- 使用jdbc的getgeneratedkeys获取数据库自增主键值 -->
 <setting name="usegeneratedkeys" value="true" />
 
 <!-- 使用列别名替换列名 默认:true -->
 <setting name="usecolumnlabel" value="true" />
 
 <!-- 开启驼峰命名转换:table{create_time} -> entity{createtime} -->
 <setting name="mapunderscoretocamelcase" value="true" />
 </settings>
</configuration>

第二步:刚弄好dao层,接下来到service层了。在spring文件夹里新建spring-service.xml文件。

  • 扫描service包所有注解 @service
  • 配置事务管理器,把事务管理交由spring来完成
  • 配置基于注解的声明式事务,可以直接在方法上@transaction

spring-service.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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:tx="http://www.springframework.org/schema/tx"
 xsi:schemalocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd">
 <!-- 扫描service包下所有使用注解的类型 -->
 <context:component-scan base-package="com.soecode.lyf.service" />
 
 <!-- 配置事务管理器 -->
 <bean id="transactionmanager"
 class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
 <!-- 注入数据库连接池 -->
 <property name="datasource" ref="datasource" />
 </bean>
 
 <!-- 配置基于注解的声明式事务 -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

第三步:配置web层,在spring文件夹里新建spring-web.xml文件。

  • 开启springmvc注解模式,可以使用@requestmapping,@pathvariable,@responsebody等
  • 对静态资源处理,如js,css,jpg等
  • 配置jsp 显示viewresolver,例如在controller中某个方法返回一个string类型的”login”,实际上会返回”/web-inf/login.jsp”
  • 扫描web层 @controller

spring-web.xml

 
?
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"?>
<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
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <!-- 配置springmvc -->
 <!-- 1.开启springmvc注解模式 -->
 <!-- 简化配置:
 (1)自动注册defaultanootationhandlermapping,anotationmethodhandleradapter
 (2)提供一些列:数据绑定,数字和日期的format @numberformat, @datetimeformat, xml,json默认读写支持
 -->
 <mvc:annotation-driven />
 
 <!-- 2.静态资源默认servlet配置
 (1)加入对静态资源的处理:js,gif,png
 (2)允许使用"/"做整体映射
 -->
 <mvc:default-servlet-handler/>
 
 <!-- 3.配置jsp 显示viewresolver -->
 <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
 <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" />
 <property name="prefix" value="/web-inf/jsp/" />
 <property name="suffix" value=".jsp" />
 </bean>
 
 <!-- 4.扫描web相关的bean -->
 <context:component-scan base-package="com.soecode.lyf.web" />
</beans>

第四步:最后就是修改web.xml文件了,它在webapp的web-inf下。

web.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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_3_1.xsd"
 version="3.1" metadata-complete="true">
 <!-- 如果是用mvn命令生成的xml,需要修改servlet版本为3.1 -->
 <!-- 配置dispatcherservlet -->
 <servlet>
 <servlet-name>seckill-dispatcher</servlet-name>
 <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
 <!-- 配置springmvc需要加载的配置文件
 spring-dao.xml,spring-service.xml,spring-web.xml
 mybatis - > spring -> springmvc
 -->
 <init-param>
 <param-name>contextconfiglocation</param-name>
 <param-value>classpath:spring/spring-*.xml</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>seckill-dispatcher</servlet-name>
 <!-- 默认匹配所有的请求 -->
 <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

我们在项目中经常会使用到日志,所以这里还有配置日志xml,在resources文件夹里新建logback.xml文件,所给出的日志输出格式也是最基本的控制台s呼出,大家有兴趣查看。

logback.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<configuration debug="true">
 <appender name="stdout" class="ch.qos.logback.core.consoleappender">
 <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.patternlayoutencoder -->
 <encoder>
 <pattern>%d{hh:mm:ss.sss} [%thread] %-5level %logger{36} - %msg%n</pattern>
 </encoder>
 </appender>
 
 <root level="debug">
 <appender-ref ref="stdout" />
 </root>
</configuration>

到目前为止,我们一共写了7个配置文件,我们一起来看下最终的配置文件结构图。

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

ssm框架应用实例(图书管理系统)

一开始想就这样结束教程,但是发现其实很多人都还不会把这个ssm框架用起来,特别是mybatis部分。那我现在就以最常见的“图书管理系统”中【查询图书】和【预约图书】业务来做一个demo吧!

首先新建数据库名为ssm,再创建两张表:图书表book和预约图书表appointment,并且为book表初始化一些数据,sql如下。

schema.sql

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- 创建图书表
create table `book` (
 `book_id` bigint(20) not null auto_increment comment '图书id',
 `name` varchar(100) not null comment '图书名称',
 `number` int(11) not null comment '馆藏数量',
 primary key (`book_id`)
) engine=innodb auto_increment=1000 default charset=utf8 comment='图书表'
 
-- 初始化图书数据
insert into `book` (`book_id`, `name`, `number`)
values
 (1000, 'java程序设计', 10),
 (1001, '数据结构', 10),
 (1002, '设计模式', 10),
 (1003, '编译原理', 10)
 
-- 创建预约图书表
create table `appointment` (
 `book_id` bigint(20) not null comment '图书id',
 `student_id` bigint(20) not null comment '学号',
 `appoint_time` timestamp not null default current_timestamp on update current_timestamp comment '预约时间' ,
 primary key (`book_id`, `student_id`),
 index `idx_appoint_time` (`appoint_time`)
) engine=innodb default charset=utf8 comment='预约图书表'

在entity包中添加两个对应的实体,图书实体book.java和预约图书实体appointment.java。

book.java

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
package com.soecode.lyf.entity;
 
public class book {
 
 private long bookid;// 图书id
 
 private string name;// 图书名称
 
 private int number;// 馆藏数量
 
 // 省略构造方法,getter和setter方法,tostring方法
 
}

appointment.java

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.soecode.lyf.entity;
 
import java.util.date;
 
/**
 * 预约图书实体
 */
public class appointment {
 
 private long bookid;// 图书id
 
 private long studentid;// 学号
 
 private date appointtime;// 预约时间
 
 // 多对一的复合属性
 private book book;// 图书实体
 
 // 省略构造方法,getter和setter方法,tostring方法
 
}

在dao包新建接口bookdao.java和appointment.java

bookdao.java

 
?
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
package com.soecode.lyf.dao;
 
import java.util.list;
 
import com.soecode.lyf.entity.book;
 
public interface bookdao {
 
 /**
 * 通过id查询单本图书
 *
 * @param id
 * @return
 */
 book querybyid(long id);
 
 /**
 * 查询所有图书
 *
 * @param offset 查询起始位置
 * @param limit 查询条数
 * @return
 */
 list<book> queryall(@param("offset") int offset, @param("limit") int limit);
 
 /**
 * 减少馆藏数量
 *
 * @param bookid
 * @return 如果影响行数等于>1,表示更新的记录行数
 */
 int reducenumber(long bookid);
}

appointmentdao.java

 
?
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
package com.soecode.lyf.dao;
 
import org.apache.ibatis.annotations.param;
 
import com.soecode.lyf.entity.appointment;
 
public interface appointmentdao {
 
 /**
 * 插入预约图书记录
 *
 * @param bookid
 * @param studentid
 * @return 插入的行数
 */
 int insertappointment(@param("bookid") long bookid, @param("studentid") long studentid);
 
 /**
 * 通过主键查询预约图书记录,并且携带图书实体
 *
 * @param bookid
 * @param studentid
 * @return
 */
 appointment querybykeywithbook(@param("bookid") long bookid, @param("studentid") long studentid);
 
}

提示:这里为什么要给方法的参数添加@param注解呢?是因为该方法有两个或以上的参数,一定要加,不然mybatis识别不了。上面的bookdao接口的querybyid方法和reducenumber方法只有一个参数book_id,所以可以不用加 @param注解,当然加了也无所谓~

注意:这里不需要实现dao接口不用编写daoimpl, mybatis会给我们动态实现,但是我们需要编写相应的mapper。
在mapper目录里新建两个文件bookdao.xml和appointmentdao.xml,分别对应上面两个dao接口,代码如下。

bookdao.xml

 
?
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"?>
<!doctype mapper
 public "-//mybatis.org//dtd mapper 3.0//en"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.soecode.lyf.dao.bookdao">
 <!-- 目的:为dao接口方法提供sql语句配置 -->
 <select id="querybyid" resulttype="book" parametertype="long">
 <!-- 具体的sql -->
 select
 book_id,
 name,
 number
 from
 book
 where
 book_id = #{bookid}
 </select>
 
 <select id="queryall" resulttype="book">
 select
 book_id,
 name,
 number
 from
 book
 order by
 book_id
 limit #{offset}, #{limit}
 </select>
 
 <update id="reducenumber">
 update book
 set number = number - 1
 where
 book_id = #{bookid}
 and number > 0
 </update>
</mapper>

appointmentdao.xml

 
?
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
<?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.soecode.lyf.dao.appointmentdao">
 <insert id="insertappointment">
 <!-- ignore 主键冲突,报错 -->
 insert ignore into appointment (book_id, student_id)
 values (#{bookid}, #{studentid})
 </insert>
 
 <select id="querybykeywithbook" resulttype="appointment">
 <!-- 如何告诉mybatis把结果映射到appointment同时映射book属性 -->
 <!-- 可以自由控制sql -->
 select
 a.book_id,
 a.student_id,
 a.appoint_time,
 b.book_id "book.book_id",
 b.`name` "book.name",
 b.number "book.number"
 from
 appointment a
 inner join book b on a.book_id = b.book_id
 where
 a.book_id = #{bookid}
 and a.student_id = #{studentid}
 </select>
</mapper>

mapper总结:namespace是该xml对应的接口全名,select和update中的id对应方法名,resulttype是返回值类型,parametertype是参数类型(这个其实可选),最后#{...}中填写的是方法的参数,看懂了是不是很简单!!我也这么觉得~ 还有一个小技巧要交给大家,就是在返回appointment对象包含了一个属性名为book的book对象,那么可以使用"book.属性名"的方式来取值,看上面querybykeywithbook方法的sql。

dao层写完了,接下来test对应的package写我们测试方法吧。

因为我们之后会写很多测试方法,在测试前需要让程序读入spring-dao和mybatis等配置文件,所以我这里就抽离出来一个basetest类,只要是测试方法就继承它,这样那些繁琐的重复的代码就不用写那么多了~

basetest.java

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.soecode.lyf;
 
import org.junit.runner.runwith;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;
 
/**
 * 配置spring和junit整合,junit启动时加载springioc容器 spring-test,junit
 */
@runwith(springjunit4classrunner.class)
// 告诉junit spring配置文件
@contextconfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" })
public class basetest {
 
}

因为spring-service在service层的测试中会时候到,这里也一起引入算了!

新建bookdaotest.java和appointmentdaotest.java两个dao测试文件。

bookdaotest.java

 
?
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
package com.soecode.lyf.dao;
 
import java.util.list;
 
import org.junit.test;
import org.springframework.beans.factory.annotation.autowired;
 
import com.soecode.lyf.basetest;
import com.soecode.lyf.entity.book;
 
public class bookdaotest extends basetest {
 
 @autowired
 private bookdao bookdao;
 
 @test
 public void testquerybyid() throws exception {
 long bookid = 1000;
 book book = bookdao.querybyid(bookid);
 system.out.println(book);
 }
 
 @test
 public void testqueryall() throws exception {
 list<book> books = bookdao.queryall(0, 4);
 for (book book : books) {
 system.out.println(book);
 }
 }
 
 @test
 public void testreducenumber() throws exception {
 long bookid = 1000;
 int update = bookdao.reducenumber(bookid);
 system.out.println("update=" + update);
 }
 
}

bookdaotest测试结果

testquerybyid

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程testquerybyid

testqueryall

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

testreducenumber

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

appointmentdaotest.java

 
?
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
package com.soecode.lyf.dao;
 
import org.junit.test;
import org.springframework.beans.factory.annotation.autowired;
 
import com.soecode.lyf.basetest;
import com.soecode.lyf.entity.appointment;
 
public class appointmentdaotest extends basetest {
 
 @autowired
 private appointmentdao appointmentdao;
 
 @test
 public void testinsertappointment() throws exception {
 long bookid = 1000;
 long studentid = 12345678910l;
 int insert = appointmentdao.insertappointment(bookid, studentid);
 system.out.println("insert=" + insert);
 }
 
 @test
 public void testquerybykeywithbook() throws exception {
 long bookid = 1000;
 long studentid = 12345678910l;
 appointment appointment = appointmentdao.querybykeywithbook(bookid, studentid);
 system.out.println(appointment);
 system.out.println(appointment.getbook());
 }
 
}

appointmentdaotest测试结果

testinsertappointment

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

testquerybykeywithbook

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

嗯,到这里一切到很顺利~那么我们继续service层的编码吧~可能下面开始信息里比较大,大家要做好心理准备~

首先,在写我们的控制器之前,我们先定义几个预约图书操作返回码的数据字典,也就是我们要返回给客户端的信息。我们这类使用枚举类,没听过的小伙伴要好好恶补一下了(我也是最近才学到的= =)

预约业务操作返回码说明

 

返回码 说明
1 预约成功
0 库存不足
-1 重复预约
-2 系统异常

 

新建一个包叫enums,在里面新建一个枚举类appointstateenum.java,用来定义预约业务的数据字典,没听懂没关系,我们直接看代码吧~是不是感觉有模有样了!

appointstateenum.java

 
?
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
package com.soecode.lyf.enums;
 
/**
 * 使用枚举表述常量数据字典
 */
public enum appointstateenum {
 
 success(1, "预约成功"), no_number(0, "库存不足"), repeat_appoint(-1, "重复预约"), inner_error(-2, "系统异常");
 
 private int state;
 
 private string stateinfo;
 
 private appointstateenum(int state, string stateinfo) {
 this.state = state;
 this.stateinfo = stateinfo;
 }
 
 public int getstate() {
 return state;
 }
 
 public string getstateinfo() {
 return stateinfo;
 }
 
 public static appointstateenum stateof(int index) {
 for (appointstateenum state : values()) {
 if (state.getstate() == index) {
 return state;
 }
 }
 return null;
 }
 
}

接下来,在dto包下新建appointexecution.java用来存储我们执行预约操作的返回结果。

appointexecution.java

 
?
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
package com.soecode.lyf.dto;
 
import com.soecode.lyf.entity.appointment;
import com.soecode.lyf.enums.appointstateenum;
 
/**
 * 封装预约执行后结果
 */
public class appointexecution {
 
 // 图书id
 private long bookid;
 
 // 秒杀预约结果状态
 private int state;
 
 // 状态标识
 private string stateinfo;
 
 // 预约成功对象
 private appointment appointment;
 
 public appointexecution() {
 }
 
 // 预约失败的构造器
 public appointexecution(long bookid, appointstateenum stateenum) {
 this.bookid = bookid;
 this.state = stateenum.getstate();
 this.stateinfo = stateenum.getstateinfo();
 }
 
 // 预约成功的构造器
 public appointexecution(long bookid, appointstateenum stateenum, appointment appointment) {
 this.bookid = bookid;
 this.state = stateenum.getstate();
 this.stateinfo = stateenum.getstateinfo();
 this.appointment = appointment;
 }
 
 // 省略getter和setter方法,tostring方法
 
}

接着,在exception包下新建三个文件

  • nonumberexception.java
  • repeatappointexception.java
  • appointexception.java

预约业务异常类(都需要继承runtimeexception),分别是无库存异常、重复预约异常、预约未知错误异常,用于业务层非成功情况下的返回(即成功返回结果,失败抛出异常)。

nonumberexception.java

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.soecode.lyf.exception;
 
/**
 * 库存不足异常
 */
public class nonumberexception extends runtimeexception {
 
 public nonumberexception(string message) {
 super(message);
 }
 
 public nonumberexception(string message, throwable cause) {
 super(message, cause);
 }
 
}

repeatappointexception.java

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.soecode.lyf.exception;
 
/**
 * 重复预约异常
 */
public class repeatappointexception extends runtimeexception {
 
 public repeatappointexception(string message) {
 super(message);
 }
 
 public repeatappointexception(string message, throwable cause) {
 super(message, cause);
 }
 
}

appointexception.java

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.soecode.lyf.exception;
 
/**
 * 预约业务异常
 */
public class appointexception extends runtimeexception {
 
 public appointexception(string message) {
 super(message);
 }
 
 public appointexception(string message, throwable cause) {
 super(message, cause);
 }
 
}

咱们终于可以编写业务代码了,在service包下新建bookservice.java图书业务接口。

bookservice.java

 
?
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
package com.soecode.lyf.service;
 
import java.util.list;
 
import com.soecode.lyf.dto.appointexecution;
import com.soecode.lyf.entity.book;
 
/**
 * 业务接口:站在"使用者"角度设计接口 三个方面:方法定义粒度,参数,返回类型(return 类型/异常)
 */
public interface bookservice {
 
 /**
 * 查询一本图书
 *
 * @param bookid
 * @return
 */
 book getbyid(long bookid);
 
 /**
 * 查询所有图书
 *
 * @return
 */
 list<book> getlist();
 
 /**
 * 预约图书
 *
 * @param bookid
 * @param studentid
 * @return
 */
 appointexecution appoint(long bookid, long studentid);
 
}

在service.impl包下新建bookserviceimpl.java使用bookservice接口,并实现里面的方法。

bookserviceimpl

 
?
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
78
79
80
81
82
83
package com.soecode.lyf.service.impl;
 
import java.util.list;
 
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
 
import com.soecode.lyf.dao.appointmentdao;
import com.soecode.lyf.dao.bookdao;
import com.soecode.lyf.dto.appointexecution;
import com.soecode.lyf.entity.appointment;
import com.soecode.lyf.entity.book;
import com.soecode.lyf.enums.appointstateenum;
import com.soecode.lyf.exception.appointexception;
import com.soecode.lyf.exception.nonumberexception;
import com.soecode.lyf.exception.repeatappointexception;
import com.soecode.lyf.service.bookservice;
 
@service
public class bookserviceimpl implements bookservice {
 
 private logger logger = loggerfactory.getlogger(this.getclass());
 
 // 注入service依赖
 @autowired
 private bookdao bookdao;
 
 @autowired
 private appointmentdao appointmentdao;
 
 
 @override
 public book getbyid(long bookid) {
 return bookdao.querybyid(bookid);
 }
 
 @override
 public list<book> getlist() {
 return bookdao.queryall(0, 1000);
 }
 
 @override
 @transactional
 /**
 * 使用注解控制事务方法的优点: 1.开发团队达成一致约定,明确标注事务方法的编程风格
 * 2.保证事务方法的执行时间尽可能短,不要穿插其他网络操作,rpc/http请求或者剥离到事务方法外部
 * 3.不是所有的方法都需要事务,如只有一条修改操作,只读操作不需要事务控制
 */
 public appointexecution appoint(long bookid, long studentid) {
 try {
 // 减库存
 int update = bookdao.reducenumber(bookid);
 if (update <= 0) {// 库存不足
 //return new appointexecution(bookid, appointstateenum.no_number);//错误写法
 throw new nonumberexception("no number");
 } else {
 // 执行预约操作
 int insert = appointmentdao.insertappointment(bookid, studentid);
 if (insert <= 0) {// 重复预约
 //return new appointexecution(bookid, appointstateenum.repeat_appoint);//错误写法
 throw new repeatappointexception("repeat appoint");
 } else {// 预约成功
 appointment appointment = appointmentdao.querybykeywithbook(bookid, studentid);
 return new appointexecution(bookid, appointstateenum.success, appointment);
 }
 }
 // 要先于catch exception异常前先catch住再抛出,不然自定义的异常也会被转换为appointexception,导致控制层无法具体识别是哪个异常
 } catch (nonumberexception e1) {
 throw e1;
 } catch (repeatappointexception e2) {
 throw e2;
 } catch (exception e) {
 logger.error(e.getmessage(), e);
 // 所有编译期异常转换为运行期异常
 //return new appointexecution(bookid, appointstateenum.inner_error);//错误写法
 throw new appointexception("appoint inner error:" + e.getmessage());
 }
 }
 
}

下面我们来测试一下我们的业务代码吧~因为查询图书的业务不复杂,所以这里只演示我们最重要的预约图书业务!!

bookserviceimpltest.java

 
?
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
package com.soecode.lyf.service.impl;
 
import static org.junit.assert.fail;
 
import org.junit.test;
import org.springframework.beans.factory.annotation.autowired;
 
import com.soecode.lyf.basetest;
import com.soecode.lyf.dto.appointexecution;
import com.soecode.lyf.service.bookservice;
 
public class bookserviceimpltest extends basetest {
 
 @autowired
 private bookservice bookservice;
 
 @test
 public void testappoint() throws exception {
 long bookid = 1001;
 long studentid = 12345678910l;
 appointexecution execution = bookservice.appoint(bookid, studentid);
 system.out.println(execution);
 }
 
}

bookserviceimpltest测试结果

testappoint

一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程

首次执行是“预约成功”,如果再次执行的话,应该会出现“重复预约”,哈哈,我们所有的后台代码都通过单元测试啦~~是不是很开心~

咱们还需要在dto包里新建一个封装json返回结果的类result.java,设计成泛型。

result.java

 
?
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
package com.soecode.lyf.dto;
 
/**
 * 封装json对象,所有返回结果都使用它
 */
public class result<t> {
 
 private boolean success;// 是否成功标志
 
 private t data;// 成功时返回的数据
 
 private string error;// 错误信息
 
 public result() {
 }
 
 // 成功时的构造器
 public result(boolean success, t data) {
 this.success = success;
 this.data = data;
 }
 
 // 错误时的构造器
 public result(boolean success, string error) {
 this.success = success;
 this.error = error;
 }
 
 // 省略getter和setter方法
}

最后,我们写web层,也就是controller,我们在web包下新建bookcontroller.java文件。

bookcontroller.java

 
?
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
package com.soecode.lyf.web;
 
import java.util.list;
 
import org.apache.ibatis.annotations.param;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.responsebody;
 
import com.soecode.lyf.dto.appointexecution;
import com.soecode.lyf.dto.result;
import com.soecode.lyf.entity.book;
import com.soecode.lyf.enums.appointstateenum;
import com.soecode.lyf.exception.nonumberexception;
import com.soecode.lyf.exception.repeatappointexception;
import com.soecode.lyf.service.bookservice;
 
@controller
@requestmapping("/book") // url:/模块/资源/{id}/细分 /seckill/list
public class bookcontroller {
 
 private logger logger = loggerfactory.getlogger(this.getclass());
 
 @autowired
 private bookservice bookservice;
 
 @requestmapping(value = "/list", method = requestmethod.get)
 private string list(model model) {
 list<book> list = bookservice.getlist();
 model.addattribute("list", list);
 // list.jsp + model = modelandview
 return "list";// web-inf/jsp/"list".jsp
 }
 
 @requestmapping(value = "/{bookid}/detail", method = requestmethod.get)
 private string detail(@pathvariable("bookid") long bookid, model model) {
 if (bookid == null) {
 return "redirect:/book/list";
 }
 book book = bookservice.getbyid(bookid);
 if (book == null) {
 return "forward:/book/list";
 }
 model.addattribute("book", book);
 return "detail";
 }
 
 //ajax json
 @requestmapping(value = "/{bookid}/appoint", method = requestmethod.post, produces = {
 "application/json; charset=utf-8" })
 @responsebody
 private result<appointexecution> appoint(@pathvariable("bookid") long bookid, @requestparam("studentid") long studentid) {
 if (studentid == null || studentid.equals("")) {
 return new result<>(false, "学号不能为空");
 }
 //appointexecution execution = bookservice.appoint(bookid, studentid);//错误写法,不能统一返回,要处理异常(失败)情况
 appointexecution execution = null;
 try {
 execution = bookservice.appoint(bookid, studentid);
 } catch (nonumberexception e1) {
 execution = new appointexecution(bookid, appointstateenum.no_number);
 } catch (repeatappointexception e2) {
 execution = new appointexecution(bookid, appointstateenum.repeat_appoint);
 } catch (exception e) {
 execution = new appointexecution(bookid, appointstateenum.inner_error);
 }
 return new result<appointexecution>(true, execution);
 }
 
}

因为我比较懒,所以我们就不测试controller了,好讨厌写前端,呜呜呜~

到此,我们的ssm框架整合配置,与应用实例部分已经结束了,我把所有源码和jar包一起打包放在了我的github上,需要的可以去下载,喜欢就给个star吧,这篇东西写了两个晚上也不容易啊。

补充更新

修改预约业务代码,失败时抛异常,成功时才返回结果,控制层根据捕获的异常返回相应信息给客户端,而不是业务层直接返回错误结果。上面的代码已经作了修改,而且错误示范也注释保留着,之前误人子弟了,还好有位网友前几天提出质疑,我也及时做了修改。

修改bookcontroller几处错误

1.detail方法不是返回json的,故不用加@responsebody注解

2.appoint方法应该加上@responsebody注解

3.另外studentid参数注解应该是@requestparam

4.至于controller测试,测试appoint方法可不必写jsp,用curl就行,比如
curl -h “accept: application/json; charset=utf-8” -d “studentid=1234567890” localhost:8080/book/1003/appoint

源码下载:

github下载地址:http://github.com/liyifeng1994/ssm

本地下载地址:ssm-master.rar

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.2cto.com/kf/201606/518341.html

延伸 · 阅读

精彩推荐