gpt4 book ai didi

java - 环境变量中的数据库名称

转载 作者:行者123 更新时间:2023-12-02 09:31:09 25 4
gpt4 key购买 nike

我的问题如下,我有一个 Spring boot 应用程序,我使用 Jooq 进行 sql,我设置我的 pom 来生成表。数据库信息设置在环境变量中。它在 Intellij 内部工作正常,但是当我生成 .WAR 并将应用程序上传到客户端时,只有当客户端数据库名称与创建 jooq 代码时我的 pom.xml 中的名称相同时,它才有效。这些表完全相同,唯一可以更改的是数据库名称,因此我在环境变量中查找名称信息。我的变量都在属性中设置,在调试中我可以看到我的连接对象和变量数据。

我的pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.speedsoft</groupId>
<artifactId>bedelServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <packaging>jar</packaging>-->
<packaging>war</packaging>

<name>BWS</name>
<description>BWS</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<quartz.version>2.2.1</quartz.version>
<jooq.version>3.9.1</jooq.version>
<logback.version>1.2.3</logback.version>
<logbackaccess.version>1.3.0-alpha4</logbackaccess.version>
<mysql.version>6.0.3</mysql.version>
<exec-maven-plugin>1.7</exec-maven-plugin>

<start-class>com.speedsoft.bws.BwsApplication</start-class>
</properties>



<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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-tomcat</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>${quartz.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>${jooq.version}</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>${jooq.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${logbackaccess.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.8.3</version>

<!-- The plugin should hook into the generate goal -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>

<dependencies/>

<configuration>
<jdbc>
<driver>${jdbc.driver}</driver>
<url>${jdbc.url}</url>
<user>${jdbc.user}</user>
<password>${jdbc.password}</password>
</jdbc>

<generator>
<database>
<inputSchema>bedel</inputSchema>
<name>org.jooq.util.mysql.MySQLDatabase</name>
</database>
<target>
<packageName>com.speedsoft.bws.jooq.model</packageName>
<directory>target/generated-sources/jooq</directory>
<encoding>UTF-8</encoding>
</target>
<generate>
<fluentSetters>false</fluentSetters>
<daos>false</daos>
</generate>
</generator>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<systemProperties>
<systemProperty>
<key>jdbc.driver</key>
<value>${jdbc.driver}</value>
</systemProperty>
<systemProperty>
<key>jdbc.user</key>
<value>${jdbc.user}</value>
</systemProperty>
<systemProperty>
<key>jdbc.password</key>
<value>${jdbc.password}</value>
</systemProperty>
<systemProperty>
<key>jdbc.url</key>
<value>${jdbc.url}</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.user>root</jdbc.user>
<jdbc.password>%47**¨00</jdbc.password>
<jdbc.url>jdbc:mysql://localhost:3306/bedel?serverTimezone=UTC</jdbc.url>
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
</properties>
</profile>
</profiles>

如何配置我的 pom.xml,以便在生成 .WAR 时不会插入对与代码生成时间同名的数据库的依赖项?

我必须在架构中输入所有可能的名称吗?

<schemata>
<schema>
<inputSchema>ui</inputSchema>
</schema>
<schema>
<inputSchema>other_schema</inputSchema>
</schema>
</schemata>

最佳答案

读取环境变量

You can read environment variables directly with Maven通过使用 ${env.VARIABLE_NAME} 语法。如果您知道只有两个模式,但它们的具体名称可能会根据环境而有所不同,您可以这样写:

<schemata>
<schema>
<inputSchema>${env.UI_SCHEMA}</inputSchema>
</schema>
<schema>
<inputSchema>${env.OTHER_SCHEMA}</inputSchema>
</schema>
</schemata>

使用编程代码生成配置

始终有可能实现编程代码生成配置,这将允许对输入模式进行更动态的配置。这记录在此处:https://www.jooq.org/doc/latest/manual/code-generation/codegen-programmatic

使用运行时架构映射

您可以选择在版本控制中 checkin 生成的代码(这有利有弊),而不是根据环境重新生成代码,并使用运行时模式映射功能在运行时修补生成的模式: https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-render-mapping/

关于java - 环境变量中的数据库名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957349/

25 4 0
文章推荐: java - 生成绑定(bind)文件 Message Queue Broker 4.5
文章推荐: java - 如何使用 Micronaut 客户端注释映射 errorType
文章推荐: java - 在一个 SOLR 查询中获取多个字段的不同值
文章推荐: java - 如何在 Java 8 中对 List 进行排序