1.CrudRepository接口访问数据:提供了最基本的对实体类的增删改查操作
数据访问层接口:public interface UserRepository extends CruRepository<User,Integer>{}
定义业务层类:查询所有的数据:
public Iterable<User>getAll(){
return userRepository.findAll();
}
返回id对应的User对象
public User getById(Integer id){
Optional<User>op=userRepository.findById(id);
return op.get();}
2.PagingAndSortingRepository接口访问数据继承了CrudRepository接口,所以除了拥有CrudRepository的功能外,它还增加了排序和分页查询的功能。
定义数据访问层接口:public interface ArticleRepository extends PagingAndSortingRepository<Article,Integer>{}
定义业务层:按照指定的排序对象规则查询出实体对象数据
public Iterable<Article>findAllSort(Sort sort){
return articleRepository.findAll(sort);}
分页查询实体对象,包含排序功能操作
public Page<Article> findAll(Pageable page){
return articleRepository.findAll(page)}
对控制器类不在书写,重点看下一个接口