springboot 使用jpa
时间: 2023-11-11 20:00:09 浏览: 101
Spring Boot 使用 JPA 可以通过以下步骤实现:
1. 在 pom.xml 中添加 Spring Data JPA 的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```
2. 配置数据源和 JPA 属性,可以在 application.properties 文件中添加以下配置:
```properties
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
# JPA 配置
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
```
3. 创建实体类,使用 @Entity 注解标注实体类,使用 @Id 和 @GeneratedValue 注解标注主键和自动生成策略。
4. 创建 Repository 接口,继承 JpaRepository 接口,可以直接使用 JpaRepository 提供的方法进行数据操作。
阅读全文
相关推荐















