好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Data JPA框架的核心概念与Repository接口详解

1 核心概念

Spring Data存储库抽象的中心接口是Repository。它把要管理的实体类以及实体类的ID类型作为类型参数。这个接口主要是作为一个标记接口,用来捕捉工作中的类型,并帮助你发现扩展这个接口的接口。CrudRepository接口为被管理的实体类提供复杂的CRUD功能。

自己可以看看Repository的扩展接口以及实现类 IDEA中将光标定位在Repository.java文件中, ctrl+h快捷键就可以看。

类图继承关系如下:

CrudRepository接口

这个接口定义了一套CRUD基本操作的方法,使用起来很方便

CrudRepository接口比较强大的地方在于可以派生方法,什么意思, 举个例子

you一张用户表,如果你想按照某个字段统计一下数量, 这个实现的确非常强大。

?

1

2

3

4

5

6

public interface MemberMapper extends CustomRepository<Member, Long> {

     /** 派生方法按照用户名统计人数 */

     Long countByMemberName(String username);

     /** 派生方法按照用户部门号统计人数 */

     Long countByDeptId(Integer deptId);

}

PagingAndSortingRepository接口

在CrudRepository之上,有一个PagingAndSortingRepository的抽象,它增加了额外的方法以方便对实体类的分页查询访问

2 查询方法

标准的CRUD功能库通常有对底层数据存储的查询。使用Spring Data,声明这些查询需要4个步骤:

1】声明一个扩展Repository或其子接口之一的接口,并指定它应该处理的实体类和ID类型

?

1

interface MemberRepository extends Repository<Member, Long> { … }

2】在接口中声明查询方法

?

1

2

3

interface MemberRepository extends Repository<Member, Long> {

   List<Member> findByMembername(String username);

}

3】设置Spring为这些接口创建代理实例, 可以使用配置类或xml配置文件的方式来实现

通过 配置类的方式,示例如下:

?

1

2

3

4

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories

public class MyConfig {

  }

通过 xml配置文件的方式,示例如下:

?

1

2

3

4

5

6

7

8

9

10

<? xml version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://www.springframework.org/schema/beans"

    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

    xmlns:jpa = "http://www.springframework.org/schema/data/jpa"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

      https://www.springframework.org/schema/beans/spring-beans.xsd

      http://www.springframework.org/schema/data/jpa

      https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    < jpa:repositories base-package = "com.kkarma.repository" />

</ beans >

本示例中使用的是JPA命名空间。如果你对任何其他存储使用存储库抽象,你需要将其修改成你的存储模块的对应命名空间声明。换句话说,你应该把jpa换成其他的存储类型,例如mongodb。

另外,请注意,JavaConfig配置类并不明确地配置扫描的基础包,因为默认情况下会使用注释类的包。要自定义要扫描的包,请使用数据存储特定库的 @Enable${store}Repositories-annotation 的 basePackage… 属性之一。什么意思呢,举例:

如果你的store类型是 mongodb, 就使用@EnableMongoRepositories

?

1

2

3

4

@Configuration

@EnableMongoRepositories (*arrayOf( "com.kkarma.repository" , "com.???.???" ))

class PersistenceConfig : AbstractMongoConfiguration() {

}

如果你的store类型是 redis, 就使用@EnableRedisRepositories

如果你的store类型是 jpa, 就使用@EnableJpaRepositories

简单配置单个package,格式如下:

?

1

@EnableJpaRepositories ( "com.spr.repository" )

简单配置支持多个package,格式如下:

?

1

@EnableJpaRepositories ({ "com.cshtong.sample.repository" , "com.cshtong.tower.repository" })

4】注入Repository实例并使用它

?

1

2

3

4

5

6

7

8

9

10

11

12

@Service

public class MemberServiceImpl implements MemberService {

     private final MemberMapper memberMapper;

     public MemberServiceImpl(MemberMapper memberMapper) {

         this .memberMapper = memberMapper;

     }

     @Override

     public ApiResponse insertMember(Member member) {

         Member user = memberMapper.save(member);

         return ApiResponse.success( "新增成功" , user);

     }

}

3 后续内容介绍

我们详细解释上面四步操作中的详细内容。

定义repository接口 定义查询方法 创建Repository实例 为Spring Data Repository库定制实现

到此这篇关于Spring Data JPA框架的核心概念与Repository接口详解的文章就介绍到这了,更多相关Spring Data JPA核心概念内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_41865652/article/details/123878478

查看更多关于Spring Data JPA框架的核心概念与Repository接口详解的详细内容...

  阅读:15次