数据源&& aplication.properties设置JDBC - Postgres的数据库模式

时间:2017-10-04 14:07:57

标签: java spring postgresql jdbc schema

我在为我的应用设置架构时遇到问题。在Postgres中我有两个模式:public和portal。 我希望这个应用程序使用“门户”模式(这就是第二个模式的命名方式),但遗憾的是我无法通过已知技术实现这一点(来自堆栈和谷歌的其他问题)。

这是我的DataSourceConfig:

@Configuration
public class DataSourceConfig {

    @Value("${PORTAL_DB_URL}")
    private String databaseUrl;
    @Value("${PORTAL_DB_USER_NAME}")
    private String userName;
    @Value("${PORTAL_DB_PASSWORD}")
    private String password;
    @Value("${PORTAL_DB_DRIVER}")
    private String driver;
    @Value("${PORTAL_DB_SCHEMA}")
    private String schema;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.tpp-portal")
    public DataSource dataSource(@Value("${PORTAL_DB_SCHEMA}") String schema){
        this.schema = schema;

        DataSource dataSource = DataSourceBuilder
                .create()
                .username(userName)
                .password(password)
                .url(databaseUrl)
                .driverClassName(driver)
                .build();

/*        if(!schema.isEmpty() && dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource){
            ((org.apache.tomcat.jdbc.pool.DataSource) dataSource).setInitSQL("ALTER SESSION SET CURRENT_SCHEMA=" + schema);
        }*/

        return dataSource;
    }
}

MainApplication.java:

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
public class MainApplication  {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);


    }

}

application.properties:

server.port=8081

liquibase.changeLog=classpath:db/changelog/db.changelog-master.xml

spring.mvc.view.suffix=.html

spring.datasource.portal.test-on-borrow=true
spring.datasource.portal.validation-query=SELECT 1;
spring.datasource.portal.validation-interval=3000
spring.datasource.portal.remove-abandoned=true

PORTAL_DB_DRIVER=org.postgresql.Driver
PORTAL_DB_URL=jdbc:postgresql://localhost/db_name?public=portal
PORTAL_DB_USER_NAME=db_name
PORTAL_DB_PASSWORD=db_password
PORTAL_DB_SCHEMA=portal

logging.level.root=INFO

根据这些文件,有谁知道怎么可能这样做? 或者我应该在我的DB中更改它吗?

db.changelog-master.xml

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

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <include file="db/dev/changelog/1.0/1.0-init-tables.xml"/>
    <include file="db/dev/changelog/1.0/1.0-init-data.xml"/>
</databaseChangeLog>

和init-tables.xml

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <changeSet id="1.0-init-tables" author="portal">

        <createTable tableName="service">
            <column name="id"
                    type="INTEGER">
                <constraints primaryKey="true"
                             primaryKeyName="PK_service_id"/>
            </column>

            <column name="status"
                    type="VARCHAR(32)"/>
            <column name="name"
                    type="VARCHAR(64)"/>
            <column name="created_By"
                    type="VARCHAR(64)"/>
            <column name="created_Date"
                    type="DATETIME"/>
            <column name="modified_By"
                    type="VARCHAR(64)"/>
            <column name="modified_Date"
                    type="DATETIME"/>
        </createTable>
    </changeSet>

我在这些xml文件上尝试了几种方法,但它也没有用......

假设我与DB有正确的连接,并且我的架构已经存在(但它是空的。)。

任何帮助将不胜感激!

0 个答案:

没有答案