springboot集成maven多模块开发

🍇一、需求描述

项目框架springboot集成mybatis,使用的数据库是oracle,使用maven包管理。

在项目开发中涉及到很多定时任务同步设备的状态,如果定时任务和业务代码同步在一起,如果启动停止程序会对定时任务造成影响,就把项目拆分成两个项目,一个负责业务系统web-business ,一个负责定时任务系统web-task

同时在开发过程中发现业务系统和定时任务系统有很多公用的代码,比如modelmapper等,于是又提取一个公共项目web-common来存放公用的代码。

🍈二、构建多模块项目

将项目拆分成三个子项目和一个父项目

web-parent:父项目,包纳三个子项目,并负责引入公共包。

web-business:业务模块,负责业务处理。

web-task:定时任务模块,负责定时任务。

web-common:公共模块,公共组件,所有数据相关的mapper也放在公共模块里面。

🥑2.1新建项目

2.1.1web-parent

首先新建一个web-parent项目。

新建好的项目有src,目录,直接将src目录删除。

2.1.2子模块

然后新建几个对应的模块,右键新建moudle

建好之后,wen-parent自动就在pom文件中把几个文件项目导入了

🍆2.2配置项目

2.2.1配置web-parent

父项目的xml中引入公共的jar包。

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.study</groupId>
    <artifactId>web-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
        <module>web-business</module>
        <module>web-task</module>
        <module>web-common</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <!-- springboot相关 -->
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>
        <!-- 数据库相关 -->
        <dependency>
            <groupId>com.oracle.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>11.1.0.6.0</version>
            <scope>system</scope>
            <systemPath>youpath/ojdbc8.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.6</version>
        </dependency>
    </dependencies>
</project>

2.2.2配置web-business

配置打包相关要在子模块中配置,并且要在resources中配置引入mapper的路径。另外几个模块配置类似。

打包时在web-parent目录下执行mvn clean package即可。

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>web-parent</artifactId>
        <groupId>com.study</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web-business</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

yml配置

yml配置中也要指定mapper的位置。

yaml 复制代码
server:
  port: 8087
  servlet:
    context-path: /
    session:
      timeout: PT30M

spring:
  profiles:
    active: dev
  datasource:
    username: system
    password: 123456
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@127.0.0.1:1521:helowin
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false

mybatis:
  type-aliases-package: com.example.demo.model
  mapper-locations: classpath:mappers/*.xml
相关推荐
2301_802502331 小时前
哈工大计算机系统2025大作业——Hello的程序人生
数据库·程序人生·课程设计
Alan3165 小时前
Qt 中,设置事件过滤器(Event Filter)的方式
java·开发语言·数据库
TDengine (老段)6 小时前
TDengine 集群容错与灾备
大数据·运维·数据库·oracle·时序数据库·tdengine·涛思数据
Lao A(zhou liang)的菜园6 小时前
高效DBA的日常运维主题沙龙
运维·数据库·dba
迪迦不喝可乐7 小时前
mysql知识点
数据库·mysql
不太可爱的大白7 小时前
MySQL 事务的 ACID 四大特性及其实现原理
数据库·mysql
风景_fengjing8 小时前
ORACLE 缺失 OracleDBConsoleorcl服务导致https://xxx:port/em 不能访问
oracle
观测云9 小时前
HikariCP 可观测性最佳实践
数据库
文牧之9 小时前
PostgreSQL的扩展 dblink
运维·数据库·postgresql
趁你还年轻_9 小时前
Redis-旁路缓存策略详解
数据库·redis·缓存