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
相关推荐
剩下了什么13 小时前
MySQL JSON_SET() 函数
数据库·mysql·json
山峰哥14 小时前
数据库工程与SQL调优——从索引策略到查询优化的深度实践
数据库·sql·性能优化·编辑器
较劲男子汉14 小时前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
java搬砖工-苤-初心不变14 小时前
MySQL 主从复制配置完全指南:从原理到实践
数据库·mysql
山岚的运维笔记16 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
roman_日积跬步-终至千里17 小时前
【LangGraph4j】LangGraph4j 核心概念与图编排原理
java·服务器·数据库
汇智信科17 小时前
打破信息孤岛,重构企业效率:汇智信科企业信息系统一体化运营平台
数据库·重构
野犬寒鸦17 小时前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
晚霞的不甘18 小时前
揭秘 CANN 内存管理:如何让大模型在小设备上“轻装上阵”?
前端·数据库·经验分享·flutter·3d
市场部需要一个软件开发岗位19 小时前
JAVA开发常见安全问题:纵向越权
java·数据库·安全