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
- 添加注解能到达到不用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 |
博客介绍了Repository类的定义,它是一个空的标记接口,若自定义接口不继承它运行时会报错,添加特定注解可实现相同功能。同时指出继承Repository后,方法名不能随意定义,需采用固定格式。
862

被折叠的 条评论
为什么被折叠?



