Spring Data:Repository类

Repository类的定义:

public interface Repository<T, ID extends Serializable> {}

1)Repository是一个空接口,标记接口
没有包含方法声明的接口

2)如果我们自己的接口没有extends Repository,运行时会报错:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type ‘com.imooc.repository.EmployeeRepository’
available

  1. 添加注解能到达到不用extends Repository的功能
    @RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)

继承Repository后方法名定义

继承Repository后方法名不能随便定义,需要使用固定格式

Keyword Sample JPQL snippet
And findByLastnameAndFirstname …where x.lastname=? and x.firstname=?
Or findByLastnameOrFirstname …where x.lastname=? or x.firstname=?
Between findByStartDateBetween …where x.startDate between ? and ?
LessThan findByAgeLessThan …where x.age<?
GreaterThan findByAgeGreaterThan …where x.age>?
After findByStartDateAfter …where x.startDate>?
Before findByStartDateBefore …where x.startDate<?
isNull findByAgeIsNull …where x.age is null
isNotNull,NotNull findByAge(Is)NotNull …where x.age not null
Like findByFirstnameLike …where x.firstname like ?
NotLike findByFirstnameNotLike …where x.firstname not like ?
StartingWith findByFirstnameStartingWith …where x.firstname like ?(参数后加%)
EndingWith findByFirstnameEndingWith …where x.firstname like ?(参数前加%)
Containing findByFirstnameContaining …where x.firstname like ?(参数前后都加%)
OrderBy findByAgeOrderByLastnameDesc …where x.age=? order by x.lastname desc
Not findByLastnameNot …where x.lastname <> ?
In findByAgeIn …where x.age in ?
NotIn findByAgeNotIn …where x.age not in ?
TRUE findByActiveTrue …where x.active=true
FALSE findByActiveFalse …where x.active=false

猜你喜欢

转载自blog.csdn.net/drl_blogs/article/details/89737623