- Spring Data JPA从入门到精通
- 张振华
- 139字
- 2025-03-01 13:50:15
1.4 Spring Data JPA的主要类及结构图
(1)我们需要掌握和使用到的类。
七个Repository接口:
- Repository (org.springframework.data.repository)
- CrudRepository (org.springframework.data.repository)
- PagingAndSortingRepository (org.springframework.data.repository)
- QueryByExampleExecutor (org.springframework.data.repository.query)
- JpaRepository (org.springframework.data.jpa.repository)
- JpaSpecificationExecutor (org.springframework.data.jpa.repository)
- QueryDslPredicateExecutor (org.springframework.data.querydsl)
两个实现类:
- SimpleJpaRepository (org.springframework.data.jpa.repository.support)
- QueryDslJpaRepository (org.springframework.data.jpa.repository.support)
(2)关系结构图如图1-5所示。

图1-5
基本上都是我们要关心的类和接口,先做到心中大体有个数,后面章节我们会一一做讲解。
(3)需要了解的类,真正的JPA的底层封装类。
- EntityManager (javax.persistence)
- EntityManagerImpl (org.hibernate.jpa.internal)
1.5 MySQL的快速开发实例
以Spring Boot和Spring Jdbc为技术场景,选用MySQL来做一个实例。
(1)环境要求:
- JDK 1.8
- Maven 3.0+
- IntelliJ IDEA
(2)第一步:创建数据库并建立user表。
① 创建一个数据的新用户并附上权限:

② 创建一个表:

(3) 第二步:利用IntelliJ IDEA创建Example1,如图1-6、图1-7所示。

图1-6

图1-7
上面的信息是Maven的pom里面所需要的,都可以修改,如图1-8所示。

图1-8
选择JPA、MySQL和Web,一路Next然后完成得到一个工程。完成后结构如图1-9所示。

图1-9
(4)第三步:创建或者修改application.properties文件。在工程的sources下面,如src/main/resources/application.properties。内容如下:

(5)第四步:创建一个@Entity。文件为src/main/java/example/example1/User.java。

(6)第五步:创建一个Repository。文件为src/main/java/example/example1/UserRepository.java。

(7)第六步:创建一个Controller。

(8)第七步:直接运行Example1Application的main函数。打开Example1Application,内容如下:

这时已经可以看到效果了。