Spring Boot集成PostgreSQL时出现NoClassDefFoundError问题求助
解决Spring Boot 3.1.0连接PostgreSQL时的NoClassDefFoundError问题
问题原因
你使用的Spring Boot 3.1.0集成的是Hibernate 6.x,而org.hibernate.dialect.PostgreSQL95Dialect是Hibernate 5.x中的方言类,Hibernate 6已经重构了方言体系,移除了这类带版本号后缀的方言,因此会出现找不到类的错误。
修复步骤
1. 修改application.yml中的方言配置
有两种可选方案:
方案一:使用Hibernate 6推荐的通用方言
将hibernate.dialect的值改为org.hibernate.dialect.PostgreSQLDialect:spring: datasource: platform: postgres url: jdbc:postgresql://localhost:5432/mtc_db username: postgres password: postgres driverClassName: org.postgresql.Driver jpa: database: postgresql show-sql: true hibernate: ddl-auto: create-drop properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect方案二:移除方言配置(更推荐)
Spring Boot会根据你的数据库URL自动推断合适的方言,完全可以删除hibernate.dialect这一行配置:spring: datasource: platform: postgres url: jdbc:postgresql://localhost:5432/mtc_db username: postgres password: postgres driverClassName: org.postgresql.Driver jpa: database: postgresql show-sql: true hibernate: ddl-auto: create-drop
2. 保持PostgreSQL依赖配置不变
你当前pom.xml中的PostgreSQL依赖是正确的,Spring Boot父项目已经管理了适配的版本,无需手动指定版本号:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
3. 重新执行构建命令
运行mvn clean install即可完成构建。
内容的提问来源于stack exchange,提问作者Emmanuel Madarassou
相关产品推荐
相关产品推荐

