Spring Data自定义Repository接口方法定义规范
2017-07-29 12:28
671 查看
概述
Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 public interface Repository@Entity public class Employee { private Integer id; private String name; private Integer age; //省略getter/setter方法 } Repository接口方法的具体命名. @RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class) public interface EmployeeRepository { //extends Repository<Employee,Integer>{ public Employee findByName(String name); // where name like ?% and age <? public List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age); // where name like %? and age <? public List<Employee> findByNameEndingWithAndAgeLessThan(String name, Integer age); // where name in (?,?....) or age <? public List<Employee> findByNameInOrAgeLessThan(List<String> names, Integer age); // where name in (?,?....) and age <? public List<Employee> findByNameInAndAgeLessThan(List<String> names, Integer age); }
这里是测试方法:
public class EmployeeRepositoryTest { private ApplicationContext ctx = null; private EmployeeRepository employeeRepository = null; @Before public void setup() { ctx = new ClassPathXmlApplicationContext("beans-new.xml"); employeeRepository = ctx.getBean(EmployeeRepository.class); System.out.println("setup"); } @After public void tearDown() { ctx = null; System.out.println("tearDown"); } @Test public void testFindByName() { //org.springframework.data.jpa.repository.support.SimpleJpaRepository@1a9c0566 System.out.println(employeeRepository); Employee employee = employeeRepository.findByName("zhangsan"); System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } @Test public void testFindByNameStartingWithAndAgeLessThan() { List<Employee> employees = employeeRepository.findByNameStartingWithAndAgeLessThan("test", 22); for (Employee employee : employees) { System.out.println("id:" + employee.getId() + " , name:" + employee.getName() + " ,age:" + employee.getAge()); } } }
具体规范
可以参照官网给出的命名规范.相关文章推荐
- jpa学习4--spring data jpa 中的JpaRepository 方法定义规范
- Spring Data JPA: 为所有Repository添加自定义方法
- spring data mongodb学习以及为repository提供可扩展的自定义方法
- Spring Data Repository有趣的定义query方法
- spring data mongodb学习以及为repository提供可扩展的自定义方法
- 74. Spring Data JPA方法定义规范【从零开始学Spring Boot】
- Spring boot data JPA 自定义JPQL语句,以及PagingAndSortingRepository接口实现分页查询
- SpringData-2 方法定义规范
- Spring Data JPA: 为所有Repository添加自定义方法
- Spring Data JPA: 为单个Repository添加自定义方法
- Spring Data Repository有趣的定义query方法
- 利用AOP为Spring Data Jpa的接口Repository添加全局自定义过滤
- Spring Data JPA方法定义规范及Spring Data简介
- [SpringBoot实践]spring-data-mongo自定义Repository接口及其实现
- 74. Spring Data JPA方法定义规范【从零开始学Spring Boot】
- 004-spring-data-elasticsearch 3.0.0.0使用【二】-spring-data之定义方法、创建repository实例、从聚合根发布事件
- spring data jpa 自定义接口实现
- SpringData方法定义规则
- Spring Data JPA 简单查询--方法定义规则
- 73. Spring Data JPA方法定义规范