🍇一、需求描述
项目框架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: 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