Repository接口是CrudRepository和PagingAndsortingRepository的父级接口。因此Repository拥有的特性,CurdRepository和PagingAndSortingRepository都拥有。
下面我们定义了自己的查询接口继承自Reppository,并定义了几个查询方法,但是并不需要去实现这个类。jpa自动根据我们定义的方法名、参数和返回值来帮我们实现具体查询方法
测试案例如下
- User3Repository.java
package com.yanglimou.jpastudy.repository;
import com.yanglimou.jpastudy.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.Repository;
import java.util.List;
public interface User3Repository extends Repository<User, Long> {
//按名称相等查询
List<User> findByName(String name);
//按名称相等查询并排序
List<User> findByName(String name, Sort sort);
//按名称相等查询并分页排序
Page<User> findByName(String name, Pageable pageable);
//按名称模糊查询
List<User> findByNameLike(String name);
//按年龄小于等于查询
List<User> findByAgeLessThanEqual(Integer age);
//按年龄小于等于查询并且按名称模糊查询
List<User> findByNameLikeAndAgeLessThanEqual(String name, Integer age);
//按年龄小于等于查询或者按名称模糊查询
List<User> findByNameLikeOrAgeLessThanEqual(String name, Integer age);
}
- User3Service.java
package com.yanglimou.jpastudy.service;
import com.yanglimou.jpastudy.entity.User;
import com.yanglimou.jpastudy.repository.User3Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class User3Service {
@Autowired
private User3Repository user3Repository;
public List<User> findByName(String name) {
return user3Repository.findByName(name);
}
public List<User> findByName(String name, Sort sort) {
return user3Repository.findByName(name,sort);
}
public Page<User> findByName(String name, Pageable pageable) {
return user3Repository.findByName(name,pageable);
}
public List<User> findByNameLike(String name) {
return user3Repository.findByNameLike("%"+name+"%");
}
public List<User> findByAgeLessThanEqual(Integer age) {
return user3Repository.findByAgeLessThanEqual(age);
}
public List<User> findByNameLikeAndAgeLessThanEqual(String name, Integer age) {
return user3Repository.findByNameLikeAndAgeLessThanEqual("%"+name+"%",age);
}
public List<User> findByNameLikeOrAgeLessThanEqual(String name, Integer age) {
return user3Repository.findByNameLikeOrAgeLessThanEqual("%"+name+"%",age);
}
}
- User3Controller.java
package com.yanglimou.jpastudy.controller;
import com.yanglimou.jpastudy.entity.User;
import com.yanglimou.jpastudy.service.User3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/users3")
public class User3Controller {
@Autowired
private User3Service user3Service;
@RequestMapping("/findByName")
public List<User> findByName(String name) {
return user3Service.findByName(name);
}
@RequestMapping("/findByName/sort")
public List<User> findByNameSort(String name, MyOrderParams myOrderParams) {
return user3Service.findByName(name,myOrderParams.toSort());
}
@RequestMapping("/findByName/page")
public Page<User> findByNameSort(String name, MyPageRequestParams myPageRequestParams) {
return user3Service.findByName(name,myPageRequestParams.toPageRequest());
}
@RequestMapping("/findByNameLike")
public List<User> findByNameLike(String name) {
return user3Service.findByNameLike(name);
}
@RequestMapping("/findByAgeLessThanEqual")
public List<User> findByAgeLessThanEqual(Integer age) {
return user3Service.findByAgeLessThanEqual(age);
}
@RequestMapping("/findByNameLikeAndAgeLessThanEqual")
public List<User> findByNameLikeAndAgeLessThanEqual(String name,Integer age) {
return user3Service.findByNameLikeAndAgeLessThanEqual(name,age);
}
@RequestMapping("/findByNameLikeOrAgeLessThanEqual")
public List<User> findByNameLikeOrAgeLessThanEqual(String name,Integer age) {
return user3Service.findByNameLikeOrAgeLessThanEqual(name,age);
}
}
网友评论