SSM

整合是一门学问,在于你想用什么技术改变生活

maven分模块搭建SSM框架

  1. 前期的准备
  2. 步骤分解整合SSM以及注意事项
  3. 整合mybatis分页插件 (详见核心代码)
  4. 整合mybatis事务 (详见注意事项)

前期的准备

说明:一个项目分为前台:如考勤,审批.而后台数据库交互模块(dao).通用业务模块(service) 接口模块(api)通用工具(util),dao、service、util你可能想要一些经验丰富的人来维护,模块化开发的另一个好处是如果dao的代码被修改,只需要重新build我们的dao模块就可以了。web模块可以build成war,dao、service、util等可以build成jar,只需要配置好依赖关系,就可以实现模块间的解耦合。这样的设计才是遵循“高内聚,低耦合”设计原则.


步骤分解整合SSM(配置方式很多,活学活用)

构建父级项目方式为(构建方式为pom) 名字为small

IMAGE

父级项目右键构建3个moudle,分别small-web small-service small-dao 其中web打包方式为war 其余的均为jar

small-web:
IMAGE

small-service:
IMAGE

small-dao:同理

pom.xml详见

github链接

配置文件结构(配置文件主要在small-web中)

IMAGE


整合mybatis分页插件(详见核心代码)

通过mybatis分页插件可以不用在mapper映射中使用limit语句,使用插件提供的PageHelper和PageInfo对象

1
2
3
4
5
6
7
8
@RequestMapping("/select1")
@ResponseBody
public List<Student> a11(){
PageHelper.startPage(2, 3);
List<Student> students = studentService.getAllStudent();
PageInfo<Student> pi = new PageInfo<>(students);
return students;
}

整合mybatis事务 (详见注意事项)

  1. 使用 @Transactional 注解在服务的实现类中

  2. 其余操作详见github链接代码


整合swaager文档生成

  • 相关依赖 (在small-web中)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<springfox-swagger2>2.7.0</springfox-swagger2>
<springfox-swagger-ui>2.7.0</springfox-swagger-ui>
<jackson-databind>2.9.0</jackson-databind>
//分隔线注意
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2}</version>
</dependency>
<!--springfox-ui的jar包(里面包含了swagger的界面静态文件) -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger-ui}</version>
</dependency>
<!--springfox依赖的jar包;如果你的项目中已经集成了无需重复 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind}</version>
</dependency>
  • 构建swagger初始化配置 构建一个新报 com.wwj.swagger
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.wwj.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.wwj.controller")
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("small接口文档")
.description("small接口测试")
.version("1.0.0")
.termsOfServiceUrl("")
.license("")
.licenseUrl("")
.build();
}
}
  • 在spring-mvc中注入对象和过滤对象
1
2
3
4
5
<!--启用该标签代表 spring mvc 不拦截css、js、jpg等相关的静态资源-->
<mvc:default-servlet-handler/>
<bean class="com.wwj.swagger.SwaggerConfig"/>
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>

IMAGE

更改index.html中地址为自己的项目名+api+api-docs

url: "http://localhost:8080/small-web/api/api-docs"

  • 方法和参数上使用
1
2
@ApiOperation(value="删除学生信息",httpMethod="POST") 
public Map<String,String> a4(@ApiParam(name = "sid", value = "学生编号", required = true)int sid)

图示如下:

IMAGE