栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Idea编译器Spring Profile与Maven Profile联动

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Idea编译器Spring Profile与Maven Profile联动

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 findAllBook() {
        List list = bookDao.findAllBook();
        System.out.println(bookDao.findAllBook());
        return list;
    }
}

多profile的pom.xml文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.11
        
    
    com.lwy.it
    maven-profile
    0.0.1-SNAPSHOT
    maven-profile
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            mysql
            
                mysql
                jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=UTF-8
                root
                123456
                com.mysql.cj.jdbc.Driver
            
            
                
                    mysql
                    mysql-connector-java
                    runtime
                
            
        
      
        
            pgsql
            
                pgsql
                jdbc:postgresql://localhost:5432/goodsdb?sslmode=disable
                postgres
                123456
                org.postgresql.Driver
            
            
                
                    org.postgresql
                    postgresql
                    runtime
                
            
        
    
    
        
            
                true
                src/main/resources
            
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

占位符变量替换生效,则必须添加:

        
            
                true
                src/main/resources
            
        

若使用Maven插件切换Profile,则无需添加默认profile,否则插件切换不生效。

            
                true
            

测试验证:

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文件已经去掉密码信息:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.11
        
    
    com.lwy.it
    maven-profile
    0.0.1-SNAPSHOT
    maven-profile
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            mysql
            
                mysql
                jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=UTF-8
                root
                com.mysql.cj.jdbc.Driver
            
            
                
                    mysql
                    mysql-connector-java
                    runtime
                
            
        
        
            pgsql
            
                pgsql
                jdbc:postgresql://localhost:5432/goodsdb?sslmode=disable
                postgres
                org.postgresql.Driver
            
            
                
                    org.postgresql
                    postgresql
                    runtime
                
            
        
    

    
        
            
                true
                src/main/resources
            
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


编辑启动项,添加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)]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331853.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号