JPA 默认会将实体中的 TABLE_NAME 转成小写如
@Entity @Table(name = "EMPLOYEE") public class Employee { @Id private String id;
会报:java.sql.SQLSyntaxErrorException: Table 'mysql.employee' doesn't exist 表名已经被转成了小写
可以添加一个策略解决此问题
package com.iron.config; import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; /** * Created by Jimmy on 2020/3/13. */ public class UpperTableStrategy extends PhysicalNamingStrategyStandardImpl { private static final long serialVersionUID = 1383021413247872469L; @Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) { // 将表名全部转换成大写 String tableName = name.getText().toUpperCase(); return name.toIdentifier(tableName); } }
application.yml 配置文件中添加相应的配置,启用上面的策略
server: port: 8081 spring: jpa: show-sql: true hibernate: naming: physical-strategy: com.iron.config.UpperTableStrategy datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://172.17.127.53:3306/mysql?Unicode=true&characterEncoding=UTF-8 username: root password: 123