工具
win10、idea
1、创建父级项目
file - new - project…
如图
点下一步
内容设置之后,点击finish完成。
删除父项目的src文件夹,其他保留
2、创建子项目1(maven项目)
右键父级项目 - new - module…
如图,和父级类似
3、创建子项目2(SpringBoot项目)
右键父项目 - new - module…
如图
创建完成。
4、父级工程pom.xml文件查看
如图
如没出现<modules>内容,则可手动添加
5、创建好的父子工程结构
如图
6、子2(springboot)项目配置
6.1 依赖导入pom.xml
web、mysql、swagger、mybatis依赖添加
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.2 application.yml配置、application-dev.yml配置
application.yml
spring:
profiles:
active: dev
server:
port: 8989
application-dev.yml
spring:
application:
name: test
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///testtable?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: ######
server:
servlet:
context-path: /test
mybatis:
mapper-locations: classpath:*/mapper/*.xml
type-aliases-package: bean
7、子项目1
7.1 创建User类 – 对应数据库表
7.2 创建mapper、service等接口 与 增删改查方法
7.3 子项目2:mapper文件夹新建
在src/resources/static下新建mapper文件夹
在mapper文件夹新建userMapper.xml
命名空间与mapper接口全类名路径一致、增删改查id与方法名一致
7.4 启动类注解添加
剩下的自己可以搞定了。
8、swagger配置
前面依赖已导入,配置如下
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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
public class SwaggerConfig {
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.server.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-bootstrap-ui RESTful APIs")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:8989/")
.version("1.0")
.build();
}
}
访问路径:项目路径 + /doc.html
使用方式如下
@ApiOperation(value = "删除用户", notes = "删除")
@ApiImplicitParam(name = "id", value = "用户编号",dataType = "int")
@PostMapping("delete")
public void delete(long id){
userService.delete(id);
}
@ApiModel(value = "User", description = "用户")
public class User {
@ApiModelProperty("用户id")
private Long id;
//......
}
一个简单的父子工程项目搭建完成。
此文章仅仅纯属记录