Idea工具使用Maven多Profile运行
示例如下:
SpringBoot在两个Profile下分别连接两个不同的数据库PostgreSQL和MySQL
Maven在两个Profile下分别依赖PostgreSQL和MySQL的驱动
数据库及数据准备见:https://blog.csdn.net/liwenyang1992/article/details/120311490
application.properties文件如下:使用@变量@作为配置占位符
#业务变量,观察打包是否被替换
business.environment=@project.environment@
server.port=8080
server.servlet.context-path=/maven
#MyBatis配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml
#数据库连接配置
spring.datasource.driver-class-name=@datasource.driver@
spring.datasource.url= @datasource.url@
spring.datasource.username=@datasource.username@
spring.datasource.password=@datasource.password@
VO、Dao及Mapper文件准备见:https://blog.csdn.net/liwenyang1992/article/details/120311490
BookController文件:
package com.lwy.it.book.controller;
import com.lwy.it.book.dao.BookDao;
import com.lwy.it.book.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class BookController {
@Autowired
private BookDao bookDao;
@GetMapping("/list")
public List<BookVO> findAllBook() {
List<BookVO> list = bookDao.findAllBook();
System.out.println(bookDao.findAllBook());
return list;
}
}
多profile的pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.11</version>
<relativePath/>
</parent>
<groupId>com.lwy.it</groupId>
<artifactId>maven-profile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-profile</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>mysql</id>
<properties>
<project.environment>mysql</project.environment>
<datasource.url>jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=UTF-8</datasource.url>
<datasource.username>root</datasource.username>
<datasource.password>123456</datasource.password>
<datasource.driver>com.mysql.cj.jdbc.Driver</datasource.driver>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>pgsql</id>
<properties>
<project.environment>pgsql</project.environment>
<datasource.url>jdbc:postgresql://localhost:5432/goodsdb?sslmode=disable</datasource.url>
<datasource.username>postgres</datasource.username>
<datasource.password>123456</datasource.password>
<datasource.driver>org.postgresql.Driver</datasource.driver>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
占位符变量替换生效,则必须添加:
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
若使用Maven插件切换Profile,则无需添加默认profile,否则插件切换不生效。
<activation>
<activeByDefault>true</activeByDefault>
</activation>
测试验证:
profile选择mysql,Reimport Maven,访问http://localhost:8080/maven/list
输出:
[BookVO(bookId=1, bookName=安徒生童话, bookPrice=99.99), BookVO(bookId=2, bookName=MySQL实战教程, bookPrice=88.88)]
profile选择pgsql,Reimport Maven,访问http://localhost:8080/maven/list
输出:
[BookVO(bookId=1, bookName=格林童话, bookPrice=100.01), BookVO(bookId=2, bookName=PostgreSQL实战, bookPrice=66.66)]
切换操作如下图:
改进点
由于数据库配置信息密级等级较高,即使是本地测试也不适合写在pom.xml文件中,所以通过启动时传递jvm参数的方式进行修改:
application.properties
#业务变量,观察打包是否被替换
business.environment=@project.environment@
server.port=8080
server.servlet.context-path=/maven
#MyBatis配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml
#数据库连接配置
spring.datasource.driver-class-name=@datasource.driver@
spring.datasource.url= @datasource.url@
spring.datasource.username=@datasource.username@
spring.datasource.password=
pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.11</version>
<relativePath/>
</parent>
<groupId>com.lwy.it</groupId>
<artifactId>maven-profile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-profile</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>mysql</id>
<properties>
<project.environment>mysql</project.environment>
<datasource.url>jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=UTF-8</datasource.url>
<datasource.username>root</datasource.username>
<datasource.driver>com.mysql.cj.jdbc.Driver</datasource.driver>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>pgsql</id>
<properties>
<project.environment>pgsql</project.environment>
<datasource.url>jdbc:postgresql://localhost:5432/goodsdb?sslmode=disable</datasource.url>
<datasource.username>postgres</datasource.username>
<datasource.driver>org.postgresql.Driver</datasource.driver>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
编辑启动项,添加spring.datasource.password的值,如图 重新clean compile ,运行
访问http://localhost:8080/maven/list
输出:
[BookVO(bookId=1, bookName=格林童话, bookPrice=100.01), BookVO(bookId=2, bookName=PostgreSQL实战, bookPrice=66.66)]
|