手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis

article/2025/10/5 22:13:14

我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能;而且在大部分教学课堂中,也会把SSH作为最核心的教学内容。 
但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配合注解的方式,编程非常快捷,而且通过restful风格定义url,让地址看起来非常优雅。 
另外,MyBatis也可以替换hibernate,正因为MyBatis的半自动特点,我们程序猿可以完全掌控SQL,这会让有数据库经验的程序猿能开发出高效率的SQL语句,而且XML配置管理起来也非常方便。 
好了,如果你也认同我的看法,那么下面我们一起来做整合吧!

在写代码之前我们先了解一下这三个框架分别是干什么的? 
相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码!

  1. SpringMVC:它用于web层,相当于controller(等价于传统的servlet和struts的action),用来处理用户请求。举个例子,用户在地址栏输入http://网站域名/login,那么springmvc就会拦截到这个请求,并且调用controller层中相应的方法,(中间可能包含验证用户名和密码的业务逻辑,以及查询数据库操作,但这些都不是springmvc的职责),最终把结果返回给用户,并且返回相应的页面(当然也可以只返回json/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项目,建立好相应的目录结构(重点!)

项目结构图

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

我说一下每个目录都有什么用吧(第一次画表格,我发现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的规范。

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

包结构图

包名 名称 作用
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,所以只需要在pom.xml中加入相应的依赖就好了,如果不使用maven的可以自己去官网下载相应的jar,放到项目WEB-INF/lib目录下。关于maven的学习大家可以看慕课网的视频教程,这里就不展开了。我把项目用到的jar都写在下面,版本都不是最新的,大家有经验的话可以自己调整版本号。另外,所有jar都会与项目一起打包放到我的github上,喜欢的给个star吧~

pom.xml

<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><artifactId>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><artifactId>junit</artifactId><version>4.11</version></dependency><!-- 1.日志 --><!-- 实现slf4j接口并整合 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.1</version></dependency><!-- 2.数据库 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version><scope>runtime</scope></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><!-- DAO: MyBatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.3</version></dependency><!-- 3.Servlet web --><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.5.4</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><!-- 4.Spring --><!-- 1)Spring核心 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.7.RELEASE</version></dependency><!-- 2)Spring DAO层 --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.1.7.RELEASE</version></dependency><!-- 3)Spring web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.7.RELEASE</version></dependency><!-- 4)Spring test --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.1.7.RELEASE</version></dependency><!-- redis客户端:Jedis --><dependency><groupId>redis.clients</groupId><artifactId>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>
  • 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
  • 146
  • 147
  • 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
  • 146
  • 147

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

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

  1. 读入数据库连接相关参数(可选)
  2. 配置数据连接池 
    1. 配置连接属性,可以不读配置项文件直接在这里写死
    2. 配置c3p0,只配了几个常用的
  3. 配置SqlSessionFactory对象(mybatis)
  4. 扫描dao层接口,动态实现dao接口,也就是说不需要daoImpl,sql和参数都写在xml文件上

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://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>
  • 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
  • 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

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

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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

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

  1. 使用自增主键
  2. 使用列别名
  3. 开启驼峰命名转换 create_time -> createTime

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

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

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"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 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层,在spring文件夹里新建spring-web.xml文件。

  1. 开启SpringMVC注解模式,可以使用@RequestMapping,@PathVariable,@ResponseBody等
  2. 对静态资源处理,如js,css,jpg等
  3. 配置jsp 显示ViewResolver,例如在controller中某个方法返回一个string类型的”login”,实际上会返回”/WEB-INF/login.jsp”
  4. 扫描web层 @Controller

spring-web.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://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>
  • 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
  • 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

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

web.xml

<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/javaeehttp://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.xmlMybatis - > 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>
  • 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
  • 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

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

logback.xml

<?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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

配置文件结构图


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

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

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

schema.sql

-- 创建图书表
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='预约图书表'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

entity包中添加两个对应的实体,图书实体Book.java和预约图书实体Appointment.java

Book.java

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

Appointment.java

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

dao包新建接口BookDao.javaAppointment.java

BookDao.java

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);
}
  • 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
  • 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

AppointmentDao.java

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);}
  • 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
  • 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

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


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

BookDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//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 -->SELECTbook_id,name,numberFROMbookWHEREbook_id = #{bookId}</select><select id="queryAll" resultType="Book">SELECTbook_id,name,numberFROMbookORDER BYbook_idLIMIT #{offset}, #{limit}</select><update id="reduceNumber">UPDATE bookSET number = number - 1WHEREbook_id = #{bookId}AND number > 0</update>
</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
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 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

AppointmentDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//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 -->SELECTa.book_id,a.student_id,a.appoint_time,b.book_id "book.book_id",b.`name` "book.name",b.number "book.number"FROMappointment aINNER JOIN book b ON a.book_id = b.book_idWHEREa.book_id = #{bookId}AND a.student_id = #{studentId}</select>
</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
  • 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

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


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

BaseTest.java

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 {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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

新建BookDaoTest.javaAppointmentDaoTest.java两个dao测试文件。

BookDaoTest.java

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 {@Autowiredprivate BookDao bookDao;@Testpublic void testQueryById() throws Exception {long bookId = 1000;Book book = bookDao.queryById(bookId);System.out.println(book);}@Testpublic void testQueryAll() throws Exception {List<Book> books = bookDao.queryAll(0, 4);for (Book book : books) {System.out.println(book);}}@Testpublic void testReduceNumber() throws Exception {long bookId = 1000;int update = bookDao.reduceNumber(bookId);System.out.println("update=" + update);}}
  • 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
  • 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

BookDaoTest测试结果

testQueryById 
testQueryById

testQueryAll 
testQueryAll

testReduceNumber 
testReduceNumber

AppointmentDaoTest.java

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 {@Autowiredprivate AppointmentDao appointmentDao;@Testpublic void testInsertAppointment() throws Exception {long bookId = 1000;long studentId = 12345678910L;int insert = appointmentDao.insertAppointment(bookId, studentId);System.out.println("insert=" + insert);}@Testpublic 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());}}
  • 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
  • 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

AppointmentDaoTest测试结果

testInsertAppointment 
testInsertAppointment

testQueryByKeyWithBook 
testQueryByKeyWithBook


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

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

预约业务操作返回码说明

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

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

AppointStateEnum.java

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;}}
  • 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
  • 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

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

AppointExecution.java

package com.soecode.lyf.dto;import com.soecode.lyf.entity.Appointment;
import com.soecode.lyf.enums.AppointStateEnum;/*** 封装预约执行后结果*/
public class AppointExecution {// 图书IDprivate 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方法}
  • 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
  • 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

接着,在exception包下新建三个文件 
NoNumberException.java 
RepeatAppointException.java 
AppointException.java 
预约业务异常类(都需要继承RuntimeException),分别是无库存异常、重复预约异常、预约未知错误异常,用于业务层非成功情况下的返回(即成功返回结果,失败抛出异常)。

NoNumberException.java

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);}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

RepeatAppointException.java

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);}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

AppointException.java

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);}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

BookService.java

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);}
  • 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
  • 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

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

BookServiceImpl

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依赖@Autowiredprivate BookDao bookDao;@Autowiredprivate AppointmentDao appointmentDao;@Overridepublic Book getById(long bookId) {return bookDao.queryById(bookId);}@Overridepublic 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());}}}
  • 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
  • 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

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

BookServiceImplTest.java

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 {@Autowiredprivate BookService bookService;@Testpublic void testAppoint() throws Exception {long bookId = 1001;long studentId = 12345678910L;AppointExecution execution = bookService.appoint(bookId, studentId);System.out.println(execution);}}
  • 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
  • 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

BookServiceImplTest测试结果

testAppoint 
testAppoint

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


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

Result.java

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方法
}
  • 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
  • 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

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

BookController.java

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());@Autowiredprivate 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 = ModelAndViewreturn "list";// WEB-INF/jsp/"list".jsp}// ajax json@RequestMapping(value = "/{bookId}/detail", method = RequestMethod.GET)@ResponseBodyprivate 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";}@RequestMapping(value = "/{bookId}/appoint", method = RequestMethod.POST, produces = {"application/json; charset=utf-8" })private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId, @Param("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);}}
  • 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
  • 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

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

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

源码下载:http://github.com/liyifeng1994/ssm


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


http://chatgpt.dhexx.cn/article/QpK7SD1u.shtml

相关文章

F-LOAM

欢迎访问我的博客首页。 F-LOAM 1. 传感器模型与特征提取1.1 传感器模型1.2 特征提取 2. 运动估计与畸变补偿2.1 匀速运动模型2.2 祛畸变 3. 位姿估计4. 地图创建与畸变补偿更新5. 附录5.1 位姿变换的传递性 6. 参考 1. 传感器模型与特征提取 1.1 传感器模型 机械式三维激光雷达…

MinIO入门-02 SpringBoot 整合MinIO并实现文件上传

SpringBoot 整合MinIO并实现文件上传 1、依赖 <!-- https://mvnrepository.com/artifact/io.minio/minio --> <dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.3.9</version> </depen…

Invalid bound statement (not found): com.lyf.eduservice.mapper.EduCourseMapper.getpublishInfo

目录 问题分析&#xff1a; 解决方案&#xff1a; 问题分析&#xff1a; dao层编译后只有class文件&#xff0c;没有mapper.xml&#xff0c;因为maven工程在默认情况下src/main/java目录下的所有资源文件是不发布到target目录下的 解决方案&#xff1a; 第一步、在项目的po…

ELF

目录 一&#xff0c;目标文件格式 二&#xff0c;ELF 1&#xff0c;分段 2&#xff0c;工具 3&#xff0c;查看目标文件内容 一&#xff0c;目标文件格式 编译链接 https://blog.csdn.net/nameofcsdn/article/details/116654835 目标文件和可执行文件的逻辑结构是类似的…

littlefs

1、littlefs主要用在微控制器和flash上&#xff0c;是一种嵌入式文件系统。主要有3个特点&#xff1a; 1)、掉电恢复 在写入时即使复位或者掉电也可以恢复到上一个正确的状态。 2)、擦写均衡 有效延长flash的使用寿命 3)、有限的RAM/ROM 节省ROM和RAM空间 2、已有的文件系…

luffy-(13)

内容概览 支付宝支付介绍支付宝支付二次封装订单相关表设计生成订单接口支付前端支付宝回调接口 支付宝支付介绍 """ 项目中有需要在线支付功能,可以使用支付宝支付(沙箱环境)微信支付(需要有备案过的域名)云闪付我们的项目以支付宝支付为例支付流程使用官方…

【FLFL】

论文记录 1. 3.3《基于区块链的联邦学习技术综述》2. 3.4《Swarm Learning for decentralized and confidential clinical machine learning》3. 3.8《Blockchained On-Device Federated Learning》4. 3.11《FLchain: Federated Learning via MEC-enabled Blockchain Network》…

LYF95101A 是一款高性能、高集成度、具有快速

LYF95101A 概述 LYF95101A是一款高性能、高集成度、具有快速 关断特性的单通道同步整流控制器。支持CCM, QR和 DCM的多模式工作。通过智能的控制MOSFET的开通 和关断&#xff0c;可替代反激变换器次级整流的肖特基二极管 来实现效率的提高。 LYF95101A 内置自供电电路&#xff…

渗透测试简介

病毒&#xff1a;是在计算机程序中插入的破坏计算机功能或者数据的代码&#xff0c;能影响计算机使用&#xff0c;能自我复制的一组计算机指令或者程序代码&#xff1b; ●木马&#xff1a;是比较流行的病毒文件&#xff0c;与一般的病毒不同&#xff0c;它不会自我繁殖&#x…

介绍模糊测试(Fuzz Testing,Fuzzing)

介绍模糊测试&#xff08;Fuzz Testing&#xff0c;Fuzzing&#xff09; 一、什么是模糊测试&#xff1f; 模糊测试是一种自动或半自动的测试技术&#xff0c;常被用来发现软件/操作系统/网络的代码中的错误和安全性问题&#xff0c;其中用于输入随机的数据和不合法的数据被称…

#学习笔记4#软件测试基础——测试阶段划分、黑盒测试的一些知识

今天主要看了以下几个方面的知识点&#xff0c;基本都是纯理论&#xff0c;本文只做知识点总结&#xff0c;具体内容要看转载 1.软件测试阶段划分&#xff0c;分为4个阶段&#xff1a;单元测试、集成测试、系统测试、验收测试 单元测试是方法类的覆盖&#xff0c;主要是由开…

辅助程序实现黑盒自动化测试的常见问题

背景 辅助程序&#xff08;Accessibility&#xff09;在大多数机型上具有重启设备后被激活的特性&#xff0c;可以完成Android测试框架&#xff08;Uiautomator1.0、Uiautomator2.0&#xff09;无法实现的功能。本文介绍如何搭建辅助程序和如何利用辅助程序进行黑盒测试。并总…

Android 13 Camera ITS 环境搭建(从Python安装到环境配置详解)

Python 版本 根据CameraITS.pdf 以及报错信息提示需要python版本在3.7.9及以上&#xff0c;本次使用的是3.8.10。 Python 安装 1.官方网站下载Python源码 wget https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tgz 2.进入到下载路径&#xff0c;解压Python文件 …

黑盒测试常见错误类型说明及解决方法有哪些?

目录 1、用户界面错误 2、遗漏信息 3、错误的、误导的或令人迷惑的信息 1、用户界面错误 功能性 易用性&#xff08;用户学习使用程序的时间和记住怎样使用程序的时间&#xff09; 执行速度&#xff08;多数是启动速度&#xff0c;查询速度&#xff0c;刷新速度及响应速度…

浅析黑盒测试与白盒测试

这里写自定义目录标题 黑盒测试黑盒测试常见的测试&#xff1a;黑盒测试常用的方法&#xff1a;等价类划分&#xff1a;边界值分析&#xff1a;因果图分析法、错误推断法&#xff1a; 白盒测试白盒测试常见的方法&#xff1a; 黑盒测试与白盒测试的优缺点二者的优点二者的缺点 …

卸载symantec杀毒软件

(Get-WmiObject -Class Win32_Product -Filter “Name‘Symantec Endpoint Protection’” -ComputerName . ).Uninstall()

schrodinger 薛定谔安装与卸载

schrodinger 薛定谔安装 Windows 版 百度云 链接&#xff1a;https://pan.baidu.com/s/107a4KMHMvg1vrXTFHnGcWw 提取码&#xff1a;c6t4 安装步骤 download所有压缩包&#xff0c;解压任意一个压缩包即可&#xff08;压缩包之间是相互关联的&#xff09;。 进入解压的后的文…

从rookie到基佬~009:无密码卸载Symantec

今天是变直小技巧 今日份洗脑&#xff1a;无密码情况下卸载Symantec&#xff08;赛门铁克&#xff09; 结论&#xff1a;赛门铁克的服务一旦启动&#xff0c;怎么卸载都需要密码&#xff0c;反过来说&#xff0c;你不让他启动&#xff0c;他就是待宰的羔羊。 坐标国内某企业…

赛门铁克下载

Symantec&#xff08;赛门铁克&#xff09;杀毒软件&#xff0c;包括Symantec AntiVirus即SAV系列&#xff0c;Symantec Client Security即SCS系列&#xff0c;以及Symantec Endpoint Protection即SEP系列&#xff0c;都是专门为企业级用户定制的。这3套Symantec杀毒软件均包括…

Symantex Endpoint Protection赛门铁克杀毒软件安装

一、交付邮件 目前软件交付基本都是通过邮件进行。杀毒软件交付邮件中附件包含序列号以及购买产品信息。 二、官网软件下载 在右键中点击“Download Today”跳转下载页面&#xff08;吐槽&#xff1a;交互很差劲&#xff09; 登录网站&#xff1a;https://support.broadcom.…