表名配置有外部属性文件(Table name configured with external properties file)

编程入门 行业动态 更新时间:2024-10-26 11:24:07
表名配置有外部属性文件(Table name configured with external properties file)

我构建了一个Spring-Boot应用程序,用于访问数据库并从中提取数据。 一切工作正常,但我想从外部.properties文件配置表名称。

喜欢:

@Entity @Table(name = "${fleet.table.name}") public class Fleet { ... }

我试图找到一些东西,但我没有。

您可以使用@Value("...")注释访问外部属性。

所以我的问题是:有什么办法可以配置表名? 或者我可以更改/拦截由休眠发送的查询吗?

解:

好吧,休眠5与PhysicalNamingStrategy 。 所以我创建了自己的PhysicalNamingStrategy 。

@Configuration public class TableNameConfig{ @Value("${fleet.table.name}") private String fleetTableName; @Value("${visits.table.name}") private String visitsTableName; @Value("${route.table.name}") private String routeTableName; @Bean public PhysicalNamingStrategyStandardImpl physicalNamingStrategyStandard(){ return new PhysicalNamingImpl(); } class PhysicalNamingImpl extends PhysicalNamingStrategyStandardImpl { @Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) { switch (name.getText()) { case "Fleet": return new Identifier(fleetTableName, name.isQuoted()); case "Visits": return new Identifier(visitsTableName, name.isQuoted()); case "Result": return new Identifier(routeTableName, name.isQuoted()); default: return super.toPhysicalTableName(name, context); } } } }

另外, 这个关于NamingStrategy的Stackoverflow文章给了我这个想法。

I build a Spring-Boot application that accesses a Database and extracts data from it. Everything is working fine, but I want to configure the table names from an external .properties file.

like:

@Entity @Table(name = "${fleet.table.name}") public class Fleet { ... }

I tried to find something but I didn't.

You can access external properties with the @Value("...") annotation.

So my question is: Is there any way I can configure the table names? Or can I change/intercept the query that is sent by hibernate?

Solution:

Ok, hibernate 5 works with the PhysicalNamingStrategy. So I created my own PhysicalNamingStrategy.

@Configuration public class TableNameConfig{ @Value("${fleet.table.name}") private String fleetTableName; @Value("${visits.table.name}") private String visitsTableName; @Value("${route.table.name}") private String routeTableName; @Bean public PhysicalNamingStrategyStandardImpl physicalNamingStrategyStandard(){ return new PhysicalNamingImpl(); } class PhysicalNamingImpl extends PhysicalNamingStrategyStandardImpl { @Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) { switch (name.getText()) { case "Fleet": return new Identifier(fleetTableName, name.isQuoted()); case "Visits": return new Identifier(visitsTableName, name.isQuoted()); case "Result": return new Identifier(routeTableName, name.isQuoted()); default: return super.toPhysicalTableName(name, context); } } } }

Also, this Stackoverflow article over NamingStrategy gave me the idea.

最满意答案

表名实际上是通过其策略接口从hibernate本身获得的。 Boot将其配置为SpringNamingStrategy并且Boot 2.x中有一些更改可以自定义。 值得阅读gh-1525这些变化。 配置Hibernate命名策略有更多信息。

有一些想法可以添加一些自定义属性来配置SpringNamingStrategy但是我们允许更轻松地定制整个策略bean,因为它允许用户SpringNamingStrategy地执行任何操作。

AFAIK,没有直接的方式来配置你想要的配置,但我认为如果你创建自己的策略,你可以自动连线你自己的属性到那里。 就像那些自定义的策略界面一样,您将看到实体名称,您可以在引导的配置属性中为此保留一个密钥空间并匹配实体名称。

mytables.naming.fleet.name=foobar mytables.naming.othertable.name=xxx

你的配置属性需要mytables并且在这个naming中将是一个Map 。 然后,在您的自定义策略中,只需通过从映射表中检查是否定义了自定义名称即可。

Table names are really coming from hibernate itself via its strategy interfaces. Boot configures this as SpringNamingStrategy and there were some changes in Boot 2.x how things can be customised. Worth to read gh-1525 where these changes were made. Configure Hibernate Naming Strategy has some more info.

There were some ideas to add some custom properties to configure SpringNamingStrategy but we went with allowing easier customisation of a whole strategy beans as that allows users to to whatever they need to do.

AFAIK, there's no direct way to do config like you asked but I'd assume that if you create your own strategy you can then auto-wire you own properties to there. As in those customised strategy interfaces you will see the entity name, you could reserve a keyspace in boot's configuration properties to this and match entity names.

mytables.naming.fleet.name=foobar mytables.naming.othertable.name=xxx

Your configuration properties would take mytables and within that naming would be a Map. Then in your custom strategy it would simply be by checking from mapping table if you defined a custom name.

更多推荐

本文发布于:2023-07-17 04:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   文件   Table   configured   properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!