springboot多模块开发
熊孩纸
阅读:628
2021-04-01 11:04:02
评论:0
1.创建parent项目名称multimodule
官网创建springboot项目
http://start.spring.io/
删除src 移除JRE System Library 和 Maven Dependencies
pom文件
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>cn.zk</groupId>
<artifactId>multimodule</artifactId>
<version>1.0</version>
<!-- 配置打包方式 -->
<packaging>pom</packaging>
<name>multimodule</name>
<description>This is multi-module springboot project!</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 定义开发运行时的环境变量:开发、测试、生产环境 -->
<profiles>
<profile> <!-- 开发环境 -->
<id>dev</id>
<properties>
<profiles.env>dev</profiles.env>
<log4j.rootlevel>debug</log4j.rootlevel>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation> <!-- Eclipse中运行时是否启用该节点 -->
</profile>
<profile> <!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.env>test</profiles.env>
<log4j.rootlevel>info</log4j.rootlevel>
</properties>
</profile>
<profile> <!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.env>prod</profiles.env>
<log4j.rootlevel>info</log4j.rootlevel>
</properties>
</profile>
</profiles>
2.新建 new maven module project 名称dao
<modelVersion>4.0.0</modelVersion>
<artifactId>dao</artifactId>
<!-- 父模块的基本信息 -->
<parent>
<groupId>cn.zk</groupId>
<artifactId>multimodule</artifactId>
<version>1.0</version>
</parent>
<!-- 子模块的基本信息 -->
<groupId>cn.zk.multimodule</groupId>
<!-- 配置打包方式 -->
<packaging>jar</packaging>
<name>dao</name>
<description>This is multi-module springboot project!</description>
<!-- 配置Java版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 添加相关依赖 -->
<dependencies>
<!-- 引入mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*</include>
<include>/static/**</include>
<include>/mapper/**</include>
</includes>
<excludes>
<exclude>generatorConfig.xml</exclude>
</excludes> <!-- 打包时不打入指定配置文件(同时影响开发运行时) -->
<filtering>true</filtering> <!-- 是否自动替换${xxx}的内容(涉及log4j2.xml) -->
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources/${profiles.env}</directory>
<includes>
<include>*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3.新建 new maven module project 名称service
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.zk</groupId>
<artifactId>multimodule</artifactId>
<version>1.0</version>
</parent>
<artifactId>service</artifactId>
<!-- 子模块的基本信息 -->
<groupId>cn.zk.multimodule</groupId>
<!-- 配置打包方式 -->
<packaging>jar</packaging>
<name>service</name>
<description>This is multi-module springboot project!</description>
<!-- 配置Java版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 添加相关依赖 -->
<dependencies>
<!-- 引用dao模块 -->
<dependency>
<groupId>cn.zk.multimodule</groupId>
<artifactId>dao</artifactId>
<version>1.0</version>
</dependency>
<!-- commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
4.新建 new maven module project 名称controller
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.zk</groupId>
<artifactId>multimodule</artifactId>
<version>1.0</version>
</parent>
<artifactId>controller</artifactId>
<!-- 子模块的基本信息 -->
<groupId>cn.zk.multimodule</groupId>
<!-- 配置打包方式 -->
<packaging>jar</packaging>
<name>controller</name>
<description>This is multi-module springboot project!</description>
<dependencies>
<!-- 引入service模块 -->
<dependency>
<groupId>cn.zk.multimodule</groupId>
<artifactId>service</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*</include>
<include>/static/**</include>
<include>/mapper/**</include>
</includes>
<excludes>
<exclude>generatorConfig.xml</exclude>
</excludes> <!-- 打包时不打入指定配置文件(同时影响开发运行时) -->
<filtering>true</filtering> <!-- 是否自动替换${xxx}的内容(涉及log4j2.xml) -->
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources/${profiles.env}</directory>
<includes>
<include>*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!-- 打包方法1:程序包和依赖包合并成一个包 -->
<!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <layout>ZIP</layout> <executable>true</executable> </configuration>
</plugin> -->
<!-- 打包方法2:程序包和依赖包分开打包 -->
<!-- plugin1=程序包,plugin2=依赖包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix> <!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<useUniqueVersions>false</useUniqueVersions> <!-- 防止在 MANIFEST.MF 中自动为引用的snapshot添加时间戳 -->
<mainClass>cn.zk.multimodule.controller.ControllerApplication</mainClass>
</manifest>
<manifestEntries>
<!-- <Class-Path>config/</Class-Path> --> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
5.添加controller配置文件
server:
port: 9001
spring:
profiles:
active: dao
6.添加controller启动类
@SpringBootApplication
@RestController
@MapperScan("cn.zk.multimodule.dao.moudle.dao")
public class ControllerApplication {
public static void main(String[] args) {
SpringApplication.run(ControllerApplication.class, args);
}
@Value("${key}")
private String key;
@RequestMapping("/")
public String home() {
return "i'm controller,欢迎光临!==" + key;
}
}
7.添加dao配置文件
spring:
datasource:
url: jdbc:mysql://192.168.2.101:3306/sjjz_main?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
username: gmp
password: bw.2018
key: I am dev
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
#mybatis-plus-mapper-locations: classpath:com/zklt/bigdata/web/demo/dao/*.xml
#实体扫描,多个package用逗号或者分号分隔
mybatis-plus-typeAliasesPackage: bw.sjjz.real.module.model
#主键类型 0:"数据库ID自增", 1:"用户输",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus-global-config-id-type: 3
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus-global-config-field-strategy: 2
#驼峰下划线转换
mybatis-plus-global-config-db-column-underline: true
#刷新mapper 调试神器
#refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
# Sequence序列接口实现类配置
mybatis-plus-global-config-key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
#逻辑删除配置(下面3个配置)
#mybatis-plus-global-config-logic-delete-value: 1
#mybatis-plus-global-config-logic-not-delete-value: 0
#mybatis-plus-global-config-sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
#自定义填充策略接口实现
mybatis-plus-global-config-meta-object-handler: com.baomidou.springboot.MyMetaObjectHandler
mybatis-plus-global-config-configuration:
#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
mybatis-plus-global-config-map-underscore-to-camel-case: true
mybatis-plus-global-config-cache-enabled: false
#配置JdbcTypeForNull, oracle数据库必须配置
mybatis-plus-global-config-jdbc-type-for-null: 'null'
8.启动项目
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。