SpringBoot整合SQLite与Hibernate失败:DataSource驱动加载异常求助
SpringBoot + Hibernate + SQLite 无法加载JDBC驱动问题解决
核心问题
启动应用时抛出Failed to load driver class org.sqlite.JDBC错误,根源在于SQLite驱动依赖未被正确纳入运行时classpath,同时存在Hibernate版本与JPA API的依赖冲突。
分步修复
1. 修正SQLite JDBC依赖作用域
POM中sqlite-jdbc依赖的<scope>test</scope>会导致该驱动仅在测试阶段可用,运行时无法被加载。需移除作用域配置,使其默认生效于编译、运行阶段:
<dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.45.1.0</version> <!-- 移除<scope>test</scope> --> </dependency>
2. 补充DataSource驱动类配置
在application.properties中显式指定SQLite驱动类,避免SpringBoot自动识别失效:
# DataSource configuration spring.datasource.url=jdbc:sqlite:C:/Users/xxxxx/GAMEDB.db spring.datasource.username= spring.datasource.password= spring.datasource.driver-class-name=org.sqlite.JDBC
3. 清理Hibernate与JPA依赖冲突
当前POM中混合了Hibernate 5、6版本,以及javax/jakarta两套JPA API,会引发类加载冲突。需做以下调整:
- 移除手动添加的
hibernate-core:spring-boot-starter-data-jpa已包含适配SpringBoot版本的Hibernate核心依赖 - 移除重复的JPA API依赖:
jakarta.persistence-api、hibernate-jpa-2.1-api - 移除过时的
sqlite-dialect,改用Hibernate社区方言 - 移除版本不兼容的
hibernate-community-dialects6.x版本,替换为与Hibernate 5匹配的版本
修改后的精简POM示例:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>AngularAPI</artifactId> <version>0.0.1-SNAPSHOT</version> <name>AngularAPI</name> <description>AngularAPI</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.45.1.0</version> </dependency> <!-- 添加适配Hibernate 5的社区方言依赖 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-community-dialects</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
4. 配置Hibernate SQLite方言
在application.properties中添加方言配置,确保Hibernate正确适配SQLite:
spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect # 可选:禁用DDL自动生成,避免SQLite兼容性问题 spring.jpa.hibernate.ddl-auto=none
验证修复
执行mvn clean install清理并重新构建项目,启动SpringBoot应用,此时应能成功加载SQLite驱动并初始化DataSource。
内容的提问来源于stack exchange,提问作者Pierre
相关产品推荐
相关产品推荐

