前面的文章介绍了Spring/SpringBoot下Hibernate开启字段驼峰命名转数据库字段下划线的方式,详见:https://blog.terrynow.com/2022/04/22/spring-springboot-hibernate-auto-convert-pojo-camel-to-underline-strategy/
如果项目是使用的 SpringMVC+Hibernate,也需要让实体类的驼峰命令转数据库字段的下划线,要怎么做呢,同样本人也是测试了网上说的hibernate.physical_naming_strategy改physicalNamingStrategy 无效,经过自己的理解,解决了问题
问题解决方法
新增Camel2SnakeNamingStrategy.java
package com.terrynow.test.util;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
/**
* @author Terry
* @date 2022/4/23 15:34
* @description
*/
public class Camel2SnakeNamingStrategy extends PhysicalNamingStrategyStandardImpl {
@Override
public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
private Identifier convertToSnakeCase(final Identifier identifier) {
if (identifier == null) {
return null;
}
final String newName = identifier.getText().replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
return Identifier.toIdentifier(newName);
}
}
修改application-context.xml
网上查到很多关于增加hibernate.physical_naming_strategy的方式无效(本人亲测),而是应该在sessionFactory下增加physicalNamingStrategy,详见配置如下(主要看myCamel2SnakeNamingStrategy的部分,其他配置根据自己情况做调整):
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020.
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:annotation-config/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="com.terrynow.jmgr"/>
<!-- 自动扫描该路径的所有注解 -->
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.terrynow.jmgr.entity"/>
<property name="physicalNamingStrategy" ref="myCamel2SnakeNamingStrategy"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
<!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="myCamel2SnakeNamingStrategy" class="com.terrynow.test.util.Camel2SnakeNamingStrategy" />
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- <tx:annotation-driven mode="aspectj" /> -->
<task:annotation-driven />
</beans>
文章评论