在Spring Boot整合jpa时默认将实体类的名称的驼峰映射成下划线的表名。也就是使用CamelCaseToUnderscoresNamingStrategy。
可以通过自定义的方式来指定上述的映射规则,可以定义一个bean,返回PhysicalNamingStrategy的实例。
@Bean
PhysicalNamingStrategy f(){
return new PhysicalNamingStrategy() {
@Override
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
return name;
}
@Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
return name;
}
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
String columnName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name.getText());
Identifier identifier = new Identifier(columnName+"_test", false);
return identifier;
}
@Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
return name;
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
return name;
}
};
}
其中表名的映射规则为:驼峰转下划线再加_test
#######################################################
定义如下的bean,可以将StudentInfo对应到表名Student_Info
@Bean
public CamelCaseToUnderscoresNamingStrategy caseSensitivePhysicalNamingStrategy()
{
return new CamelCaseToUnderscoresNamingStrategy() {
@Override
protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
return false;
}
};
}
定义如下的bean,可以将StudentInfo对应到表名StudentInfo(也就是直接映射)
@Bean
PhysicalNamingStrategyStandardImpl myPhysicalNamingStrategy() {
return new PhysicalNamingStrategyStandardImpl();
}
也可以通过spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 这种方式来指定映射类