spring boot 项目 JPA+SQLite 无法启动

使用spring data JPA搭配SQLite数据库无法启动,打印如下错误

INFO  o.s.d.j.r.config.DialectResolver >>> Couldn't determine Dialect for "sqlite" 
WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext >>> Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration$SpringBootJdbcConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcMappingContext' parameter 1: Error creating bean with name 'jdbcCustomConversions' defined in class path resource [org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration$SpringBootJdbcConfiguration.class]: Failed to instantiate [org.springframework.data.jdbc.core.convert.JdbcCustomConversions]: Factory method 'jdbcCustomConversions' threw exception with message: Error creating bean with name 'jdbcDialect' defined in class path resource [org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration$SpringBootJdbcConfiguration.class]: Failed to instantiate [org.springframework.data.relational.core.dialect.Dialect]: Factory method 'jdbcDialect' threw exception with message: Cannot determine a dialect for org.springframework.jdbc.core.JdbcTemplate@3fd01d2d; Please provide a Dialect 

原因是同时引入了spring-boot-starter-data-jdbc的依赖,将其从pom中删除,仅保留spring-boot-starter-data-jpa和sqlite-jdbc以及hibernate-community-dialects三个和数据库有关的依赖包即可。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-community-dialects</artifactId>
</dependency>

此时项目正常启动

### 如何在 Spring Boot 中集成和配置 SQLite 数据库 #### 1. 创建 Spring Boot 项目 为了开始使用 SQLite,在创建一个新的 Spring Boot 项目之后,需要确保项目的结构合理并准备好进行数据库的操作。 #### 2. 修改 `pom.xml` 文件添加依赖项 为了让 Spring Boot 支持 SQLite 数据库,必须向 Maven 构建文件中加入特定的依赖。这可以通过编辑 `pom.xml` 来实现: ```xml <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.36.0.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 上述代码片段展示了如何引入必要的 JDBCJPA 组件来支持 SQLite 连接[^2]。 #### 3. 配置 application.properties 或者 application.yml 接下来要做的就是在应用配置文件里指定连接字符串和其他参数以便能够访问 SQLite 数据库实例。对于 `.properties` 文件来说,可以这样设置: ```properties spring.datasource.url=jdbc:sqlite:path_to_db_file.db spring.datasource.driver-class-name=org.sqlite.JDBC spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect ``` 如果偏好 YAML 格式的配置,则应如下所示调整: ```yaml spring: datasource: url: jdbc:sqlite:path_to_db_file.db driver-class-name: org.sqlite.JDBC jpa: database-platform: org.hibernate.dialect.SQLiteDialect ``` 这些属性指定了用于建立与 SQLite 数据库通信所需的信息以及 Hibernate 方言的选择[^4]。 #### 4. 使用实体类定义表结构 通过创建 Java 类映射到数据库中的表格来进行持久化对象管理。例如,下面是一个简单的 User 实体例子: ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // Getters and Setters... } ``` 此部分描述了怎样利用 JPA 注解将 POJO 映射成关系型数据库里的记录[^3]。 #### 5. 编写 Repository 接口执行 CRUD 操作 最后一步就是编写接口继承自 JpaRepository 并关联具体的实体类型,从而获得开箱即用的数据存取功能: ```java import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> {} ``` 有了这个仓库层的支持,就可以轻松地对 Users 表实施增删改查等基本操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值