一、多模块项目结构。
springboot-mybatis-parent
|——springboot-common 公共模块
|——springboot-dao 数据库操作模块
|——springboot-service 接口模块
|——springboot-web 控制器模块
依赖关系:springboot-service依赖springboot-dao、springboot-web依赖springboot-service。
二、父级pom.xml使用的主要依赖。
<!-- 管理所有模块 -->
<modules>
<module>springboot-common</module>
<module>springboot-dao</module>
<module>springboot-service</module>
<module>springboot-web</module>
</modules>
<!-- springboot版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
</parent>
<!-- 设置编码及版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--默认关掉单元测试 -->
<skipTests>true</skipTests>
<pagehelper.version>1.2.3</pagehelper.version>
<druid.version>1.0.29</druid.version>
<fastjson.version>1.2.31</fastjson.version>
<lombok.version>1.16.20</lombok.version>
<servlet.version>3.1.0</servlet.version>
<slf4j.version>1.7.25</slf4j.version>
</properties>
<!-- 版本管理 -->
<dependencyManagement>
<dependencies>
<!--pagehelper自动依赖mybatis -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- alibaba druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- alibaba fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- common层用到servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- common层用到slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
</build>
三、mybatis配置文件使用注意事项。
1.在application.yml中
# mybatis配置
mybatis:
## 设置整个包下类的别名
# typeAliasesPackage: com.hkb.springboot.dto, com.hkb.springboot.entity
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath:mybatis/mapper/*.xml
# 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml
# 开启驼峰功能这里不开启,这里因为使用xml,所以在mybatis-config.xml中使用
# configuration:
# mapUnderscoreToCamelCase: true
# pagehelper分页助手-另一种方式在代码里配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
上面的配置等同于下面的配置,注意:因为这里是springboot整合pagehelper,分页配置不能在mybatis-config.xml里设置。
# mybatis配置
mybatis:
## 设置整个包下类的别名
# type-aliases-package: com.hkb.springboot.dto, com.hkb.springboot.entity
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath:mybatis/mapper/*.xml
# 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml
# 开启驼峰功能这里不开启,这里因为使用xml,所以在mybatis-config.xml中使用
# configuration:
# mapUnderscoreToCamelCase: true
# pagehelper分页助手-另一种方式在代码里配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
2.设置整个包下类的别名也可以在mybatis-config.xml里配置
<?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>
<!-- 开启自动驼峰命名规则 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 允许JDBC支持自动生成主键,需要驱动兼容 -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!-- 设置整个包下类的别名,配置多个 -->
<typeAliases>
<package name="com.hkb.springboot.dto"/>
<package name="com.hkb.springboot.entity"/>
</typeAliases>
</configuration>
四、设置返回json日志格式化。
1.第一种,使用springboot默认的jackson返回,application.yml中
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
2.第二种,使用alibaba的fastjson返回,代码中
@Configuration
public class JsonConfig extends WebMvcConfigurerAdapter {
/**
* 修改自定义消息转换器
*
* @param converters
* 消息转换器列表
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 调用父类的配置
super.configureMessageConverters(converters);
// 创建fastjson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
// 将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
}
SerializerFeature.WriteDateUseDateFormat即为返回日期格式为:yyyy-MM-dd HH:mm:ss
3.有了上面的配置还不行,因为我使用mysql并且设置了日期字段类型为datetime,需要在mapper.xml中设置resultMap映射为TIMESTAMP
<resultMap id="BaseResultMap" type="SysUserEntity">
<id column="user_id" property="userId" jdbcType="BIGINT"/>
<result column="dept_id" property="deptId" jdbcType="BIGINT"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="salt" property="salt" jdbcType="VARCHAR"/>
<result column="mobile" property="mobile" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
<result column="status" property="status" jdbcType="INTEGER"/>
<!-- 返回时间戳对应mysql字段类型为datetime -->
<result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
</resultMap>
五、多模块打包、运行。
1.打包成war形式,启动类中继承SpringBootServletInitializer,重写configure方法
public class StartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(StartApplication.class);
}
/**
* 启动入口
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
2.springboot-web模块的pom.xml中
<dependencies>
<!-- 移除内嵌的tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 没有该配置,devtools不生效 -->
<fork>true</fork>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.hkb.springboot.StartApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的jar包中 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 如果打包报没有web.xml,加上下面的插件 -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>