如何通过Springboot结合spring-data-jpa实现数据库访问?

更新于
2026-10-12 11:44:26
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计184个文字,预计阅读时间需要1分钟。

如何通过Springboot结合spring-data-jpa实现数据库访问?

plaintextgistfile1.txt

1.在pom.xml文件中添加spring-boot-starter-data-jpa依赖:

org.springframework.boot spring-boot-starter-data-jpa 2

如何通过Springboot结合spring-data-jpa实现数据库访问?

2. 在dao层添加接口类,并继承JPA中的几个主要接口: Repository

gistfile1.txt

1、在pom.xml文件中添加spring-boot-starter-data-jpa依赖; org.springframework.boot spring-boot-starter-data-jpa 2、在dao层添加接口类,并继承jpa中的某几个主要的接口,按需求: Repository: CrudRepository:专门用于crud PagingAndSortingRepository:简单的分页和排序 JpaRepository: JpaSpecificationExecutor:用于复杂的条件查询并分页、排序功能 3、然后可以直接使用该repository,也可以在自己声明的接口中使用注解@Query的方式自定义sql语句 如果是非select的sql语句,需要使用@Modifying来标识该sql语句是增改删,并且在调用该方法的service层的方法上需要添加@Transactional注解表示需要事务的支持 详见gitee.com/caychen/spring-boot-study/tree/master/spring-boot-data-jpa

本文共计184个文字,预计阅读时间需要1分钟。

如何通过Springboot结合spring-data-jpa实现数据库访问?

plaintextgistfile1.txt

1.在pom.xml文件中添加spring-boot-starter-data-jpa依赖:

org.springframework.boot spring-boot-starter-data-jpa 2

如何通过Springboot结合spring-data-jpa实现数据库访问?

2. 在dao层添加接口类,并继承JPA中的几个主要接口: Repository

gistfile1.txt

1、在pom.xml文件中添加spring-boot-starter-data-jpa依赖; org.springframework.boot spring-boot-starter-data-jpa 2、在dao层添加接口类,并继承jpa中的某几个主要的接口,按需求: Repository: CrudRepository:专门用于crud PagingAndSortingRepository:简单的分页和排序 JpaRepository: JpaSpecificationExecutor:用于复杂的条件查询并分页、排序功能 3、然后可以直接使用该repository,也可以在自己声明的接口中使用注解@Query的方式自定义sql语句 如果是非select的sql语句,需要使用@Modifying来标识该sql语句是增改删,并且在调用该方法的service层的方法上需要添加@Transactional注解表示需要事务的支持 详见gitee.com/caychen/spring-boot-study/tree/master/spring-boot-data-jpa