创新实训(4)——SpringData

简介

Spring Data是一个用于简化数据库访问,并支持云服务的开源框架,是hibernate的封装。其主要目标是使得对数据的访问变得方便快捷。
可以极大的简化JPA的写法,可以在几乎不用写实现的情况下(即不用写具体的查询语句),实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。

Spring Data JPA接口

Spring Data JPA提供了6个接口

  • Repository
  • CrudRepository
  • PagingAndSortingRepository
  • JpaRepository
  • JpaSpecificationExecutor
  • Specification
    下面逐个介绍接口

Repository

最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组件扫描的时候自动识别

CrudRepository

是Repository的子接口,提供CRUD的功能

PagingAndSortingRepository

是CrudRepository的子接口,添加分页和排序的功能

JpaRepository

是PagingAndSortingRepository的子接口,增加了一些实用的功能,比如:批量操作等

JpaSpecificationExecutor

用来做负责查询的接口

Specification

是Spring Data JPA提供的一个查询规范,要做复杂的查询,只需围绕这个规范来设置查询条件即可

Spring Data支持的数据库

Spring Data支持的关系型存储技术:

  • JDBC
  • JPA

Spring Data支持的NoSQL存储技术:

  • Hbase(列存储数据库)
  • MongoDB(文档存储数据库)
  • Redis(key-value数据库)
  • Neo4j(图数据库)

Spring Data Repository 查询方法定义规范

使用Spring Data Repository进行查询时,只需要按照要求写查询方法即可,不需要写详细的查询语句

  • 查询方法以 find | read | get 开头
  • 涉及条件查询时,条件的属性用条件关键字连接
  • 条件属性以首字母大写

方法定义规范

关键字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?
StartingWithfindByNameStartingWithwhere name like ‘?%’
EndingWithfindByNameEndingWithwhere name like ‘%?’
ContainingfindByNameContainingwhere name like ‘%?%’
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not in (?)
TruefindByAaaTuewhere aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来看看小兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值