Spring Boot JPA 将架构名称外部化到属性文件 [英] Spring Boot JPA externalizing the schema name to property file

查看:43
本文介绍了Spring Boot JPA 将架构名称外部化到属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 spring boot 应用程序有 2 个模式,我在实体类中硬编码,如下所示

I have 2 schemas for my spring boot application, which I hardcode in the Entity class like this

@Entity
@Table(name"TABLE_NAME_1", schema="SCHEMA_NAME_1")
public class EntityName1{
...
}

@Entity
@Table(name"TABLE_NAME_2", schema="SCHEMA_NAME_2")
public class EntityName2{
...
}

问题是这个架构名称在每个版本中都会不断变化.因此,每次发布后,我们都必须到这里对实体文件的架构名称进行必要的更改.

The problem is this schema name keeps on changing every release. So after every release we have to come here and make the necessary changes on the entity file's schema name.

现在我想我们可以在 spring boot 中配置 default_schema 但这行不通,因为我们需要外部化两个架构名称.

Now I figured we can configure default_schema in spring boot but that won't work because we need to externalize both schema names.

有什么办法可以使用这样的东西:@实体@Table(name"TABLE_NAME_1", schema="{{default.schema_1}}")公共类实体名称1{...}

Is there any way we can use something like this: @Entity @Table(name"TABLE_NAME_1", schema="{{default.schema_1}}") public class EntityName1{ ... }

@Entity
@Table(name"TABLE_NAME_2", schema="{{default.schema_2}}")
public class EntityName2{
...
}

我们在外部文件中定义 default.schema_1 和 default.schema_2.

where we define the default.schema_1 and and default.schema_2 in an external file.

推荐答案

请试试这个...

  1. 创建类 SystemProps

public class SystemProps {

    private static Map<String,String> props = null;

    public static void loadPropsAll(){
        props = new HashMap<>();
        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:application.properties");
            loadProps(file,props);
            String x = SystemProperties.get("spring.profiles.active");
            if(x != null) {
                file = ResourceUtils.getFile("classpath:application-" + x + ".properties");
                loadProps(file, props);
            }
        }catch (Exception e){e.printStackTrace();}finally {
        }
    }

    private static void loadProps(File file, Map<String,String> props){
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            while (br.ready()){
                String line = br.readLine();
                System.out.println(line);
                if(!line.startsWith("#") && line.indexOf("=")!=-1){
                    props.put(""+line.split("=")[0].trim(), line.split("=")[1].trim());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getProp(String name){
        if(props==null)
            loadPropsAll();
        return props.get(name);
    }
}

  1. 为 jpa 创建自定义命名策略

public class CustomPhysicalNamingStrategy extends SpringPhysicalNamingStrategy {
    @Override
    public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
        if(name.getText().startsWith("$")){
            String x = name.getText().replace("$","").replaceAll("\\{","").replaceAll("}","");
            String schema = SystemProps.getProp(x);
            return super.toPhysicalSchemaName(new Identifier(schema,name.isQuoted()), jdbcEnvironment);
        }
        return super.toPhysicalSchemaName(name, jdbcEnvironment);
    }
}

  1. 在 application.properties 文件中添加属性...

spring.jpa.hibernate.naming.physical-strategy=my.package.CustomPhysicalNamingStrategy

my_schema_name_property=SCHEMA_NAME_2

现在可以使用了

@Entity
@Table(name"TABLE_NAME_1", schema="${my_schema_name_property}")
public class EntityName1{
...
}

这篇关于Spring Boot JPA 将架构名称外部化到属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆