最近,使用SpringBoot1.5.9搭建完成了个小项目,记录一下。
一、项目整体搭建
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-parent</name>
<description>test-parent</description>
<packaging>pom</packaging>
<modules>
<module>test-common</module>
<module>test-dal</module>
<module>test-biz</module>
<module>test-web</module>
</modules>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
<!-- 基本信息 -->
<groupId>com.demo</groupId>
<artifactId>test-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-web</name>
<!-- 继承本项目的父工程 -->
<parent>
<groupId>com.demo</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- Web模块相关依赖 -->
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>${profileActive}-test.demo.com</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>application-${profileActive}.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/conf_${profileActive}</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>local</profileActive>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
</profiles>
一个父项目,多个子项目,注意项目间的依赖关系
二、数据库连接池配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://localhost\:3306/test?zeroDateTimeBehavior=convertToNull&characterEncoding\=utf8
spring.datasource.username=name
spring.datasource.password=pw
三、swagger配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
@Configuration
@EnableSwagger2
@Profile({ "local", "dev", "test" })
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.demo.controller")) // Controller所在包(必须新建包)
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("") // 标题
.description("") // 描述
.termsOfServiceUrl("") // 超链接
.contact("") // 联系方式
.version("1.0").build();
}
}
四、上传文件
https://gitee.com/jscode/springboot-uploadfile-demo
五、参数校验
@Validated
@Valid
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
/**
* 验证异常
*
* @param req
* @param e
* @return
* @throws MethodArgumentNotValidException
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseBody
public WebResponse handleMethodArgumentNotValidException(HttpServletRequest req, MethodArgumentNotValidException e)
throws MethodArgumentNotValidException {
WebResponse r = new WebResponse();
BindingResult bindingResult = e.getBindingResult();
String errorMesssage = "参数错误:";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += " " + fieldError.getDefaultMessage();
}
r.setCode(WebResponseStatus.WEB_STATUS_FAILED);
r.setMessage(errorMesssage);
log.info("MethodArgumentNotValidException", e);
return r;
}
}
六、mybatis分页
Page page = PageHelper.startPage(paperQueryRequest.getPageDto().getPageNum(),
paperQueryRequest.getPageDto().getPageSize());
String paperName = StringUtils.isEmpty(paperQueryRequest.getPaperName()) ? null
: "%" + paperQueryRequest.getPaperName() + "%";
List<Paper> paperList = paperDao.getPaperList(paperName);
int number =
(paperQueryRequest.getPageDto().getPageNum() - 1) * paperQueryRequest.getPageDto().getPageSize();
List<PaperQueryResponse> paperQueryResponseList = convertPaper2PaperQueryResponse(paperList, number);
PageInfo info = new PageInfo(page);
info.setList(paperQueryResponseList);
@UpdateProvider
<![CDATA[ end_time > #{startTime} and end_time <= #{endTime} ]]>