最近在回顾和总结一些技术,想到了把之前比较火的 SSM 框架重新搭建出来,作为一个小结,同时也希望本文章写出来能对大家有一些帮助和启发,因本人水平有限,难免可能会有一些不对之处,欢迎各位大神拍砖指教,共同进步。
本文章示例使用 IntelliJ IDEA 来开发,JDK 使用 11 版本,其余各框架和技术基本上使用了文章撰写当时的最新版本。
好的,下面直接进入正题。
打开 IntelliJ IDEA,File > New > Project > Maven,选中“Create from archetype”,然后再选中“org.apache.maven.archetypes:maven-archetype-webapp”:
Next,输入项目的“GroupId”、“ArtifactId”和Version:
Next,指定“Maven home directory”等配置:
Next,修改Project Name:
Finish,打开项目,添加一些必要的目录,最终项目框架目录图如下:
修改pom.xml文件,指定各依赖和插件的版本等信息:
UTF-8
11
11
11
5.2.3.RELEASE
4.13
1.18.10
3.3.1
2.3.29
1.1.21
3.1
8.0.19
1.2
4.0.1
2.3.3
2.9.2
3.9
2.10.2
1.3.1.Final
2.13.0
1.7.30
3.1.0
3.1.0
3.8.1
3.0.0-M4
3.2.3
3.0.0-M1
3.0.0-M1
在标签里面管理各依赖的版本号:
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
test
junit
junit
${junit.version}
test
org.projectlombok
lombok
${lombok.version}
provided
com.baomidou
mybatis-plus
${mybatis-plus.version}
com.baomidou
mybatis-plus-generator
${mybatis-plus.version}
test
true
org.freemarker
freemarker
${freemarker.version}
test
true
com.alibaba
druid
${druid.version}
com.github.jsqlparser
jsqlparser
${jsqlparser.version}
mysql
mysql-connector-java
${mysql-connector.version}
javax.servlet.jsp.jstl
jstl-api
${jstl-api.version}
javax.servlet
javax.servlet-api
${servlet-api.version}
provided
javax.servlet.jsp
javax.servlet.jsp-api
${jsp-api.version}
provided
io.springfox
springfox-swagger2
${springfox-swagger.version}
io.springfox
springfox-swagger-ui
${springfox-swagger.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
compile
org.mapstruct
mapstruct
${mapstruct.version}
org.slf4j
slf4j-api
${slf4j.version}
org.apache.logging.log4j
log4j-slf4j-impl
${log4j.version}
添加项目依赖:
org.springframework
spring-context-support
org.springframework
spring-jdbc
org.springframework
spring-aspects
org.springframework
spring-webmvc
org.springframework
spring-test
junit
junit
org.projectlombok
lombok
com.baomidou
mybatis-plus
com.baomidou
mybatis-plus-generator
org.freemarker
freemarker
com.alibaba
druid
mysql
mysql-connector-java
javax.servlet.jsp.jstl
jstl-api
javax.servlet
javax.servlet-api
javax.servlet.jsp
javax.servlet.jsp-api
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
org.apache.commons
commons-lang3
com.fasterxml.jackson.core
jackson-databind
org.mapstruct
mapstruct
org.apache.logging.log4j
log4j-slf4j-impl
管理:
ssm
org.apache.maven.plugins
maven-clean-plugin
${clean.plugin.version}
org.apache.maven.plugins
maven-resources-plugin
${resources.plugin.version}
${project.build.sourceEncoding}
org.apache.maven.plugins
maven-compiler-plugin
${compiler.plugin.version}
${maven.compiler.source}
${maven.compiler.target}
${project.build.sourceEncoding}
org.projectlombok
lombok
${lombok.version}
org.mapstruct
mapstruct-processor
${mapstruct.version}
-Amapstruct.defaultComponentModel=spring
-Amapstruct.unmappedTargetPolicy=IGNORE
org.apache.maven.plugins
maven-surefire-plugin
${surefire.plugin.version}
org.apache.maven.plugins
maven-war-plugin
${war.plugin.version}
org.apache.maven.plugins
maven-install-plugin
${install.plugin.version}
org.apache.maven.plugins
maven-deploy-plugin
${deploy.plugin.version}
依赖配置好之后,开始整合。
先整合Log4j2日志,在项目classpath目录(src/main/resources)下创建log4j2.xml文件,添加Log4j2日志配置:
再整合 Spring 和 Mybatis,本次还整合了 Mybatis Plus 开源框架,新建 src/main/resources/mybatis/mybatis-config.xml 文件:
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
新建 src/main/resources/properties/jdbc.properties 文件,配置数据源连接信息:
jdbc.driver-class-name=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root
新建 src/main/resources/spring/applicationContext-dao.xml 文件,配置 Druid 数据源,SqlSessionFactory 会话工厂,Mybatis 的 Mapper 接口扫描等信息:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
com.example.ssm.service.*
com.example.ssm.mapper.*
class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor"/>
新建 src/main/resources/spring/applicationContext-tx.xml 文件,配置事务:
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">
配置 Mybatis Plus 自动生成 Controller、Service 和 Mapper 文件:
// 自动代码生成器
AutoGenerator autoGenerator = new AutoGenerator();
// 全局配置
GlobalConfig globalConfig = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
globalConfig.setOutputDir(projectPath + "/src/main/java");
globalConfig.setAuthor("calvinit");
globalConfig.setOpen(false);
globalConfig.setFileOverride(true);
autoGenerator.setGlobalConfig(globalConfig);
// 数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
// dataSourceConfig.setSchemaName("public");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root");
autoGenerator.setDataSource(dataSourceConfig);
// 包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.example.ssm");
autoGenerator.setPackageInfo(packageConfig);
// 自定义配置
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定义输出配置
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
tableInfo.setImportPackages("com.baomidou.mybatisplus.annotation.TableName");
tableInfo.setConvert(true);
// 自定义输出文件名
return projectPath + "/src/main/resources/mapper/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
injectionConfig.setFileOutConfigList(focList);
autoGenerator.setCfg(injectionConfig);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
// templateConfig.setEntity(ConstVal.TEMPLATE_ENTITY_JAVA);
// templateConfig.setService(ConstVal.TEMPLATE_SERVICE);
// templateConfig.setController(ConstVal.TEMPLATE_CONTROLLER);
templateConfig.setXml(null);
autoGenerator.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("com.example.ssm.entity.BaseEntity");
strategy.setEntityLombokModel(true);
strategy.setEntityColumnConstant(true);
strategy.setRestControllerStyle(true);
// strategy.setSuperControllerClass("com.example.ssm.controller.BaseController");
strategy.setInclude("t_user", "t_group", "t_user_group_relation");
// strategy.setSuperEntityColumns("id");
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(packageConfig.getModuleName() + "_");
strategy.setEntityTableFieldAnnotationEnable(true);
autoGenerator.setStrategy(strategy);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
autoGenerator.execute();
然后整合 Spring 和 Spring MVC,其中对日期类型返回json作了自定义格式化处理:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* Jackson ObjectMapper 工厂类
*/
public class ObjectMapperFactory {
public static ObjectMapper getMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
SimpleModule module = new SimpleModule();
module.addSerializer(LocalDate.class, new LocalDateSerializer());
module.addSerializer(LocalTime.class, new LocalTimeSerializer());
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
objectMapper.registerModule(module);
return objectMapper;
}
static class LocalDateSerializer extends JsonSerializer {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
* {@inheritDoc}
*/
@Override
public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(DATE_FORMATTER.format(value));
}
}
static class LocalDateTimeSerializer extends JsonSerializer {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* {@inheritDoc}
*/
@Override
public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(DATE_TIME_FORMATTER.format(value));
}
}
static class LocalTimeSerializer extends JsonSerializer {
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
/**
* {@inheritDoc}
*/
@Override
public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(TIME_FORMATTER.format(value));
}
}
}
还集成了 Swgger UI ,方便接口调试:
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
public class Swagger2Configuration {
@Bean("docket")
public Docket createDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.ssm.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
Contact contact = new Contact("calvinit", "https://gitee.com/calvinit/ssm-demo", "");
return new ApiInfoBuilder()
.title("SSM框架搭建Demo")
.description("SSM框架搭建Demo,仅供猿友们学习交流之用,请勿用于商业用途")
.contact(contact)
.version("1.0-RELEASE")
.build();
}
}
新建 src/main/resources/spring/spring-mvc.xml 文件:
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.xsd">
application/json;charset=UTF-8
同时整合了MapStruct,方便 Entity、DTO 和 VO 等之间的转换,使用示例:
import com.example.ssm.entity.User;
import com.example.ssm.vo.UserVo;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import java.util.List;
@Mapper
public interface UserVoConverter {
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "name", target = "userName"),
@Mapping(target = "createDt", dateFormat = "yyyy-MM-dd HH:mm:ss"),
@Mapping(target = "lastUpdateDt", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
UserVo entityToVo(User user);
List batchEntityToVo(List userList);
@InheritInverseConfiguration
User voToEntity(UserVo userVo);
List batchVoToEntity(List userVoList);
}
新建 src/main/resources/spring/applicationContext-common.xml 文件,配置 MapStruct 的 bean 被 Spring 管理:
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">
修改 web.xml 文件:
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
ssm
contextConfigLocation
classpath:spring/applicationContext-*.xml
log4jConfigLocation
classpath:log4j2.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
druidWebStatFilter
com.alibaba.druid.support.http.WebStatFilter
exclusions
*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
profileEnable
true
druidWebStatFilter
/*
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-mvc.xml
1
dispatcherServlet
/
druidStatView
com.alibaba.druid.support.http.StatViewServlet
allow
127.0.0.1
loginUsername
user
loginPassword
password
resetEnable
true
druidStatView
/druid/*
index.jsp
修改 index.jsp 文件:
ssmHello World!
打开 Swagger UI
至此,框架基本整合完毕,下面写一个测试 Contoller 测试 Spring 和 Spring MVC 整合结果:
package com.example.ssm.controller;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Map;
@Api(tags = "首页控制器")
@Controller
@RequestMapping
public class IndexController {
@ApiIgnore
@ApiOperation(value = "首页")
@GetMapping
public String index() {
return "index";
}
@ApiOperation(value = "返回纯字符串")
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello World!";
}
@ApiOperation(value = "测试日期 json 返回")
@GetMapping("/date/test")
@ResponseBody
public Map testDate() {
Map map = Maps.newHashMap();
map.put("java.util.Date", new java.util.Date());
map.put("java.sql.Date", new java.sql.Date(System.currentTimeMillis()));
map.put("java.time.LocalDate", java.time.LocalDate.now());
map.put("java.time.LocalTime", java.time.LocalTime.now());
map.put("java.time.LocalDateTime", java.time.LocalDateTime.now());
return map;
}
}
右上角“Add Configuration”添加 Tomcat 配置,将项目部署信息配置好:
然后我们启动 Tomcat,打开 http://localhost:8080/ssm,如果正常,应该显示如下界面:
点击界面上的超链接,进入 Swagger UI 的界面。
再测试一下 Spring 和 Mybatis 整合是否完成:
package com.example.ssm.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ssm.converter.UserVoConverter;
import com.example.ssm.entity.User;
import com.example.ssm.service.IUserService;
import com.example.ssm.vo.PageVo;
import com.example.ssm.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api
@RestController
@RequestMapping("/users")
public class UserController {
private IUserService userService;
private UserVoConverter userVoConverter;
public UserController(IUserService userService, UserVoConverter userVoConverter) {
this.userService = userService;
this.userVoConverter = userVoConverter;
}
@ApiOperation(value = "分页查询用户列表")
@GetMapping("/page")
@ResponseBody
public PageVo page(
@ApiParam(value = "当前页", required = true, defaultValue = "1", example = "1")
@RequestParam("pageNum") int pageNum,
@ApiParam(value = "页面大小", required = true, defaultValue = "10", example = "10")
@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
Page page = new Page<>();
page.setSize(pageSize);
page.setCurrent(pageNum);
page.addOrder(OrderItem.asc(User.CREATE_DT), OrderItem.asc(User.LAST_UPDATE_DT));
IPage userPage = userService.selectPage(page);
List userList = userPage.getRecords();
List userVoList = userVoConverter.batchEntityToVo(userList);
return PageVo.builder()
.pageNum(userPage.getCurrent())
.pageSize(userPage.getSize())
.pageTotal(userPage.getPages())
.rowTotal(userPage.getTotal())
.rowList(userVoList)
.build();
}
@ApiOperation(value = "查询某个用户")
@GetMapping("/{id}")
@ResponseBody
public User page(
@ApiParam(value = "用户Id", required = true, example = "1")
@PathVariable(value = "id") int id) {
return userService.selectByPrimaryKey(id);
}
@ApiOperation(value = "获取所有00后用户列表")
@GetMapping("/00/list")
@ResponseBody
public List list00() {
LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();
userLambdaQueryWrapper.ge(User::getBirthday, "2000-01-01");
return userService.list(userLambdaQueryWrapper);
}
}
在 Swagger UI 的界面里面测试接口,返回正确:
另外,Druid 的监控信息页面链接为:http://localhost:8080/ssm/druid/index.html,访问白名单、账号和密码在 web.xml 文件中配置。
至此,SSM 框架集成完毕,测试通过!
因篇幅关系,一些代码并没有在此文章中展现,可到我的Gitee上看完整框架代码。