spring data jpa 接口使用

spring data jpa 接口使用

1. 接口继承JpaRepository<T, V>

public interface CustomerDao extends JpaRepository<Customer, Long> {}

2. 加Query注解定义方法及返回类型

自定义看得见的sql查询语句,提供的命名方法没有合适的或者比较麻烦的,比如多表查询等

//Jpql语句
@Query(value = " from Customer")
List<Customer> getAllCustomer();
//分页查询
@Query(value = "from Customer ")
List<Customer> getAllByPage(Pageable pageable);
//条件查询
@Query(value = "from Customer where custId = ?1 ")
Customer getCustomerById (Long id);
//模糊查询
@Query("from Customer where custAddress like ?1 and custName like ?2")
List<Customer> getCustomerList (String address, String name);
//更新
@Query("update Customer set custSource = ?1 where custId = ?2")
@Modifying
@Transactional
void updateSource (String source, long id);

[外链图片转存失败(img-2fhiq7vZ-1566200554082)(C:\soft\Typora\1566200193782.png)]

3. 按照规定命名创建接口(不用写sql语句,不用写注解)

//id查询
Customer findByCustId (Long id);
//模糊
List<Customer> findByCustNameLikeAndCustAddressLike(String name,String address);
//分页
Page<Customer> findByCustAddressLike (String address, Pageable pageable);

接口命名的连接词+entity属性+限制形容词,组成方法名称,然后框架帮我们创建接口的实现方法,只需用@Autowired注入使用即可

Table 3. Supported keywords inside method names
KeywordSampleJPQL snippet

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age <= ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> ages)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库、Map-Reduce 框架、云数据服务等等;另外也包含对关系数据库的访问支持。 Spring Data 包含多个子项目: Commons - 提供共享的基础框架,适合各个子项目使用,支持跨数据库持久化 Hadoop - 基于 Spring 的 Hadoop 作业配置和一个 POJO 编程模型的 MapReduce 作业 Key-Value - 集成了 Redis 和 Riak ,提供多个常用场景下的简单封装 Document - 集成文档数据库:CouchDB 和 MongoDB 并提供基本的配置映射和资料库支持 Graph - 集成 Neo4j 提供强大的基于 POJO 的编程模型 Graph Roo AddOn - Roo support for Neo4j JDBC Extensions - 支持 Oracle RAD、高级队列和高级数据类型 JPA - 简化创建 JPA 数据访问层和跨存储的持久层功能 Mapping - 基于 Grails 的提供对象映射框架,支持不同的数据库 从之前发布其他chm文件下载用户的反映看,有不少朋友反映下载后打开无法显示,这一般不是chm文件的问题,这里统一说明一下解决办法: 如果文件打开看不到右边的内容,是因为你的操作系统为了安全对下载的chm文件进行了锁定,只需要在打开前右键单击该chm文件选择“属性”,然后在“常规”选项卡的下方单击“解除锁定”按钮就可以了。如果还是不能看,请再查看一下你的chm文件所存储的目录或文件名是否有特殊字符如“#”号字符等,去掉特殊字符即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值