我正在尝试为Spring Boot项目配置两个数据源。这是我用于主要数据源的配置文件。
@PropertySource({"classpath:application-local.properties"})
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManager",
basePackages = {"portal.api.repository"}
)
public class PrimaryDbConfig {
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(primaryDataSource());
em.setPackagesToScan(
new String[]{"portal.api.model"});
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("app.datasource.spring.jpa.hibernate.ddl"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public DataSource primaryDataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("app.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("app.datasource.url"));
dataSource.setUsername(env.getProperty("app.datasource.username"));
dataSource.setPassword(env.getProperty("app.datasource.password"));
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager userTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
primaryEntityManager().getObject());
return transactionManager;
}
}
已生成表,但表名和列名以驼峰式表示。但是我想有一个默认的春季启动的下划线名称。例如,ApiKey
实体将更改为表名api_key
。
如何配置Spring Boot以使用强调的命名策略?
答案 0 :(得分:0)
我认为,如果您在域模型的实例变量中使用@Column(name =“ api_key”),它将起作用。
例如
@Column(name = "api_key")
private String apiKey;
答案 1 :(得分:0)
来自Official doc的引用
默认情况下,Spring Boot使用以下命令配置物理命名策略: SpringPhysicalNamingStrategy。该实现提供了相同的 Hibernate 4的表结构:下划线替换所有点 并且骆驼肠衣也被下划线代替。
您可以在application.yml
中设置以下属性,以告诉springboot使用哪种命名策略:
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
类似的帖子供参考: java.sql.SQLSyntaxErrorException: Unknown column .JPA Entity Issue?