IDEA 搭建springboot多模块项目

首先说一下为什么要建多模块项目,其实很多项目在刚开始的时候,都是单结构应用,常见的几个分层(web层、service层、dao层)直接通过建不同的包名即可,但是随着业务发展,项目参与人员变多,业务变复杂,所有的代码都在一个结构下,就会变得不直观,同时耦合度可能比较高。另外一个问题就是,在多服务的场景下,要给外部服务提供接口了(比如要提供对外的dubbo接口),如果是单体结构,只能整个模块打个jar出去,不优雅,不然还得重新做多模块拆分,麻烦。还有一个问题,可能一些通用的类在好几个工程里都有,多模块结构可以把通用的类放到一起,打包出去给其它服务用。所以,对于可预见未来的中大型项目,最好是刚开始就直接多模块搭建,对于小型项目,单结构即可。

下面简单举个例子,在idea里建一个多模块的项目:

首先说一下例子的结构:

app-info
    └ app-info-commom
    └ app-info-model
    └ app-info-dao
    └ app-info-service
    └ app-info-web

各module依赖关系:

app-info-commom
        ↓
app-info-model
        ↓
app-info-dao
        ↓
app-info-service
        ↓
app-info-web

新建项目,packaging选择jar

下一步,这里不选任何依赖,因为这是最外层的父 module

建好的工程,只保留画红线的部分,其它的文件删掉

 

 

 这一步开始新建子module,首先建最底层的app-info-commom,选择maven即可

 groupId、artifactId填一下

app-info-commom下的pom.xml里<parent>应该是父module的信息

 ↑↑↑↑↑↑  app-info-model参考app-info-commom操作   ↑↑↑↑↑↑

下面新建app-info-dao,因为这里要导入mysql、mybatis相关的包,所以选择spring initializr

建好后,只保留红色部分的文件,其它都可以删掉

↑↑↑↑↑↑  app-info-service、app-info-web参考app-info-dao操作   ↑↑↑↑↑↑

 5个子module建好后,还需要处理的文件:

1、把app-info-service、app-info-dao里src下的初始化启动类删掉,只保留app-info-web的启动类

2、只保留app-info-dao、app-info-web模块里src下的resources,其它模块删掉

3、按照各module依赖关系,在对应子module的pom.xml里需要引入相关依赖(下面会把完整的pom.xml贴出来)

4、如果在建app-info-dao、app-info-service引入了redis或mysql的依赖,需要在app-info-web的application.properties加上mysql或redis的相关配置,不然启动不会成功

最终项目结构

下面把每个module的pom.xml贴出来

app-info下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.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.app.info</groupId>
    <artifactId>app-info</artifactId>
    <version>${app.info.version}</version>
    <name>app-info</name>

    <modules>
        <module>app-info-commom</module>
        <module>app-info-model</module>
        <module>app-info-dao</module>
        <module>app-info-service</module>
        <module>app-info-web</module>
    </modules>

    <properties>
        <app.info.version>1.0.0</app.info.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

app-info-commom下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>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app-info-commom</artifactId>
    <version>${app.info.version}</version>


</project>

app-info-model下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>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app-info-model</artifactId>
    <version>${app.info.version}</version>

    <dependencies>

        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-commom</artifactId>
            <version>${app.info.version}</version>
        </dependency>

    </dependencies>

</project>

app-info-dao下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>
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>app-info-dao</artifactId>
    <version>${app.info.version}</version>

    <dependencies>
        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-model</artifactId>
            <version>${app.info.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

</project>

app-info-service下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>
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>app-info-service</artifactId>
    <version>${app.info.version}</version>

    <dependencies>

        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-dao</artifactId>
            <version>${app.info.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    </dependencies>


</project>

app-info-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>app-info-web</artifactId>
    <version>${app.info.version}</version>

    <dependencies>
        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-service</artifactId>
            <version>${app.info.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

app-info-web下application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=xxxxxx
spring.datasource.username=xxxxxxx
spring.datasource.password=xxxxxx
# 指定为HikariDataSource
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
# 连接池名
spring.datasource.hikari.pool-name=HikariCP
# 最小空闲连接数
spring.datasource.hikari.minimum-idle=5
# 连接池最大连接数
spring.datasource.hikari.maximum-pool-size=20
# 空闲连接存活最大时间,默认10分钟
spring.datasource.hikari.idle-timeout=600000
# 数据库连接超时时间
spring.datasource.hikari.connection-timeout=60000

spring.redis.database=xxxxxx
spring.redis.host=xxxxx
spring.redis.port=xxxx
spring.redis.password=xxxxxx
# 最大连接数
spring.redis.jedis.pool.max-active=200
# 最大空闲
spring.redis.jedis.pool.max-idle=100
# 最小空闲
spring.redis.jedis.pool.min-idle=100
# 最大阻塞等待时间(负数表示没限制)
spring.redis.jedis.pool.max-wait=-1
# 连接超时时间
spring.redis.timeout=2000

启动服务

如果启动报错:【java: 错误: 无效的源发行版:17】,大概率是因为建项目的时候,springboot选的是3.0.0以上的版本,3.0.0需要jdk17支持,可以把springboot版本降到2.7.10或者2.7.6

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
搭建一个多模块Spring Boot项目,可以按照以下步骤: 1. 创建一个Maven项目,并选择“Create a simple project(创建一个简单项目)”选项。 2. 在pom.xml文件中添加以下插件: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 这个插件可以将Spring Boot项目打包成一个可执行的jar文件。 3. 在项目根目录下创建一个子模块,比如叫做“web”。可以使用以下Maven命令创建模块: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=web -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 这个命令会在根目录下创建一个名为“web”的子模块。 4. 在子模块的pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 这个依赖会让子模块成为一个Spring Boot Web应用程序。 5. 在子模块创建一个Spring Boot应用程序,比如叫做“WebApplication”。可以创建一个类似于以下的类: ```java @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } } ``` 这个类使用了Spring Boot的@SpringBootApplication注解,这个注解会自动配置Spring应用程序。 6. 在子模块创建一个Controller,比如叫做“HelloController”。可以创建一个类似于以下的类: ```java @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello World!"; } } ``` 这个Controller定义了一个名为“/hello”的路由,并返回了一个字符串。 7. 运行项目。可以使用以下Maven命令运行项目: ``` mvn spring-boot:run ``` 这个命令会启动Spring Boot应用程序,并在控制台输出日志。可以在浏览器中访问“http://localhost:8080/hello”来测试应用程序。 8. 添加其他子模块。可以按照以上步骤添加其他子模块,比如一个数据库模块、一个服务模块等等。这样就可以将应用程序拆分成多个模块,每个模块负责不同的功能。 注意事项: - 每个子模块都应该有一个唯一的artifactId。 - 如果子模块之间有依赖关系,可以在pom.xml文件中添加相关的依赖。 - 可以在根目录的pom.xml文件中添加公共依赖,这些依赖会被所有子模块继承。 - 如果使用了Spring Cloud等微服务框架,可以将每个子模块打包成一个独立的服务,然后使用注册中心来管理它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值