JpaRepository接口
前言:该接口继承了PagingAndSortingRepository接口。对继承的父接口中方法的返回值进行适配。
例如:父接口中的方法的返回值是是迭代器,而在子类(JpaRepository)返回值是Lis集合。
1、持久层接口:
public interface UsersJpaRepository extends JpaRepository<Users,Integer> {
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class UsersJpaRepositoryTest {@Autowiredprivate UsersJpaRepository usersJpaRepository;@Testpublic void save() {Users u = new Users();u.setName("zhangsan");u.setAge(22);u.setAddress("zhongguohaidian");usersJpaRepository.save(u);}/*** RepositoryPagingAndSorting 接口排序*/@Testpublic void pagingAndSorting() {//order 定义排序规则Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");//sort对象封装了排序规则Sort sort = new Sort(order);List<Users> list = usersJpaRepository.findAll(sort);for (Users users : list) {System.out.println(users);}}
}
本文源码下载:
github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-9