spring data 简介(一)

目录

Spring Data 概述

JPA Spring Data 概述

Spring Data JPA? HelloWorld

Repository 接口

Repository 子接口

Spring Data??Repository 查询方法定义规范

spring data 支持的关键字

级联查询


Spring Data 概述

Spring Data: Spring 的一个子项目。用于简化数据库访问,支持NoSQL关系数据库存储。其主要目标是使数据库的访问变得方便快捷。

Spring Data 项目所支持NoSQL存储

- - MongoDB(文档数据库)

- - Neo4j (图形数据库)

- - Redis(键/值存储)

- - Hbase(列族数据库)

Spring Data 项目所支持的关系数据存储技术

- - JDBC

- - JPA

JPA Spring Data 概述

JPA Spring Data: 致力于减少数据访问层(DAO)的开发量。开发者唯一要做的,就只是声明持久层的接口,其他都交给Spring Data JPA 来帮你完成!

框架怎么可能代替开发者实现业务逻辑呢?比如:当有一个UserDao.findUserById()这样一个方法声明,大致应该能判断出这是根据给定条件的ID查询出满足条件的User对象。Spring Data JPA 做的便是规范方法的名字,根据符合规范的名字来确定方法需要实现什么样的逻辑。

Spring Data JPA HelloWorld

使用Spring Data JPA进行持久层开发需要的四个步骤:

- 配置Spring 整合 JPA

- 在Spring 配置文件中配置Spring Data ,让Spring 为声明的接口创建代理对象。配置了jpa:repositories后,Spring 初始化容器将会扫描base-package 指定的包目录及其子目录,为继承Repository 或其子接口的接口创建代理对象,并将代理对象注册为Spring Bean ,业务层便可以通过Spring自动封装的特性来直接使用该对象。

- 声明持久层的接口,该接口继承 Repository ,Repository是一个标记型接口,它不包含任何方法,如必要,Spring Data 可实现Repository其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法。

**- 在接口中声明需要的方法。**Spring Data 将根据给定的策略(具体策略稍后讲解)来为其生成实现代码。

Repository 接口

1、Repository是一个空接口,即是一个标记接口

2、若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean 纳入到IOC容器中,进而可以在该接口中定义满足一定规范的方法

3、实际上,也可以通**@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)** 注解来代替 extends Repository<Person, Integer>

Repository 子接口

在eclipse 中选中Repository ,Ctrl + T ,有多个子接口和实现类

继承关系如下:

①、Repository :仅仅是一个标识,表名任何继承它的均为仓库接口类

②、CurdRepository : 继承Repository ,实现了一组CURD相关的方法

③、PagingAndSortingRepository :继承CurdRepository ,实现了一组分页排序相关的方法

④、JpaRepository : 继承PagingAndSortingRepository ,实现一组Jpa 规范相关的方法

⑤、自定义 的 XXXRepository :需要继承JpaRepository ,这样的XxxRepository 接口就具备了通用的数据访问控制层的能力。

⑥、JpaSpecificationExecutor :不属于Repository 体系,实现一组JpaCriteria 查询相关的方法

Spring DataRepository 查询方法定义规范

简单查询条件 :查询某一个实体类或是集合

在Repository 子接口中声明方法:

①、不是随便声明的,而需要符合一定的规范

②、查询方法以 find | read | get 开头

③、涉及条件查询时,条件的属性用条件关键字连接

④、要注意的是:条件属性以首字母大写

**⑤、支持属性的级联查询。若当前类有符合条件的属性,则优先使用,而不使用级联属性。**若需要使用级联属性,则属性之间使用_连接

spring data 支持的关键字

关键字

方法命名

sql where字句

And

findByNameAndPwd

where name= and pwd =

Or

findByNameOrSex

where name= or sex=

Is,Equals

findById,findByIdEquals

where id=

Between

findByIdBetween

where id between and

LessThan

findByIdLessThan

where id <

LessThanEquals

findByIdLessThanEquals

where id <=

GreaterThan

findByIdGreaterThan

where id >

GreaterThanEquals

findByIdGreaterThanEquals

where id > =

After

findByIdAfter

where id >

Before

findByIdBefore

where id <

IsNull

findByNameIsNull

where name is null

isNotNull,NotNull

findByNameNotNull

where name is not null

Like

findByNameLike

where name like

NotLike

findByNameNotLike

where name not like

StartingWith

findByNameStartingWith

where name like ‘%’

EndingWith

findByNameEndingWith

where name like ‘%’

Containing

findByNameContaining

where name like ‘%%’

OrderBy

findByIdOrderByXDesc

where id= order by x desc

Not

findByNameNot

where name <>

In

findByIdIn(Collection<> c)

where id in ()

NotIn

findByIdNotIn(Collection<> c)

where id not in ()

True

findByAaaTue

where aaa = true

False

findByAaaFalse

where aaa = false

IgnoreCase

findByNameIgnoreCase

where UPPER(name)=UPPER()

级联查询

1、创建address 表

2、在person实体类中 添加属性

3、PersonRepository 中

  //where address.id >?
	List<Person> getByAddressIdGreaterThan(Integer id);

4、测试类里面进行测试

①、先要测试是否生成数据表

//生成数据表
	@Test
	public void testJpa(){
		
	}

②、测试查询

//级联查询
	@Test
	public void testWords2(){
		List<Person> persons = personRepository.getByAddressIdGreaterThan(1);
		System.out.println(persons);
	}

③、结果 为[ ] 是因为 数据表里没有数据,只有字段

如若:

person实体类中本来就含有一个address_id

在执行级联查询的方法,会报错如下:

在映射的时候列名重复

更改如下:

运行结果:

含义:支持属性的级联查询。若当前类有符合条件的属性,则优先使用,而不使用级联属性。若需要使用级联属性,

则属性之间使用_连接

 //where address.id >?
	List<Person> getByAddress_IdGreaterThan(Integer id);

查询结果: 连表查询

以上是从某视频处所学!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值