入门级别的springboot多模块项目搭建
一。创建父工程
1.1File-》New-》Project
1.2 选中Spring Iniaializr,sdk选中1.8
1.3 填写信息及选择java版本,下一步
1.4 其他选项不用勾选,选择所需的Springboot版本,我这里选择2.3.7,接下来点击下一步
1.5 选择项目保存的路径,点击完成
1.6 删除无用的文件夹及文件(.mvn、src、mvnw、mvnw.cmd)
二。创建子工程,先创建公共模块
2.1选中父工程,右键 New-》Module
2.2 选择maven,sdk,点击下一步
2.3 输入项目名danyu-common,点击完成
三。创建子工程(danyu-web)
3.1 过程步骤跟第二步一样
四。修改各类配置文件
4.1 修改父工程(danyu)的pom.xml文件
<?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> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.danyu</groupId> <artifactId>danyu</artifactId> <version>0.0.1</version> <name>danyu</name> <description>父项目工程</description> <properties> <danyu.version>1.0.1</danyu.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <druid.version>1.2.2</druid.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--阿里数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- Mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <modules> <module>danyu-common</module> <module>danyu-web</module> </modules> <!-- <build>--> <!-- <plugins>--> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- </plugin>--> <!-- </plugins>--> <!-- </build>--> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
4.2 修改子工程(danyu-common)的pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>danyu</artifactId> <groupId>com.danyu</groupId> <version>0.0.1</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>danyu-common</artifactId> <description>公共模块</description> <dependencies> <!-- servlet包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- hutool各种工具类 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.1</version> </dependency> <!-- gson json解析工具类 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> </dependencies> </project>
4.3 修改子工程(danyu-web)的pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>danyu</artifactId> <groupId>com.danyu</groupId> <version>0.0.1</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>danyu-web</artifactId> <description>控制模块</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 通用工具--> <dependency> <groupId>com.zbbz</groupId> <artifactId>zbbzfw-common</artifactId> <version>1.0.1</version> <!-- 该模块的packaging为war,所以下面两项必须设置,否则打包会出出错 --> <type>jar</type> <!-- <classifier>classes</classifier>--> </dependency> </dependencies> </project>
五.添加application项目入口类
5.1 在danyu-web中,展开src文件夹,选中java右键,选择Mark Directory as->Sources Root
5.2 新建路径包
5.3 输入包名:com.danyu.web,之后回车完成新建
5.4新建web入口类(DanyuWebApplication.java)
package com.danyu.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DanyuWebApplication { public static void main(String[] args) { SpringApplication.run(DanyuWebApplication.class, args); } }
这一步,如果SpringApplication和SpringBootApplication引入的类包报错,则选中父工程,右键maven-》Reimport,更新重新导入一下
六。添加测试的控制器类
在com.danyu.web下添加包(controller),新建java类(TestController)
package com.danyu.web.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author danyu * @date 2020/01/01 */ @RestController @RequestMapping("/manage") public class TestController { @GetMapping("/test") public String test() { return "访问控制器成功"; } }
七。添加springboot配置文件类
7.1 在danyu-web子工程下的resources文件夹,选中右键New-》FIle
输入文件名application.yml
打开application.yml文件,输入
# 开发环境配置 server: # 服务器的HTTP端口,默认为8080 port: 8080 servlet: # 应用的访问路径 context-path: /danyu spring: datasource: name: test url: jdbc:mysql://localhost:3306/test username: root password: root
八。启动工程,进行测试
8.1 运行DanyuWebApplication类,如果项目没报错, 则代表项目运行成功
8.2 浏览器访问验证,在浏览器地址栏输入:http://localhost:8080/danyu/manage/test
至此,一个简易的springboot多模块项目搭建成功