🍇一、需求描述
项目框架springboot集成mybatis,使用的数据库是oracle,使用maven包管理。
在项目开发中涉及到很多定时任务同步设备的状态,如果定时任务和业务代码同步在一起,如果启动停止程序会对定时任务造成影响,就把项目拆分成两个项目,一个负责业务系统web-business ,一个负责定时任务系统web-task。
同时在开发过程中发现业务系统和定时任务系统有很多公用的代码,比如model,mapper等,于是又提取一个公共项目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 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 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的位置。
server:port: 8087servlet:context-path: /session:timeout: PT30Mspring:profiles:active: devdatasource:username: systempassword: 123456driver-class-name: oracle.jdbc.driver.OracleDriverurl: jdbc:oracle:thin:@127.0.0.1:1521:helowintype: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 5max-active: 20max-wait: 60000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: falsemybatis:type-aliases-package: com.example.demo.modelmapper-locations: classpath:mappers/*.xml
