(一)、Repository 接口1.方法2.注解(二)、CurdRepository(三)、PagingAndSortingRepository(四)、JpaRepository(五)、JpaSpecificationExecutor
(一)、Repository 接口 1.方法方法名称查询
创建自定义接口,继承类Repository;创建方法实现数据查询;方法名称命名规则: findBy + 属性名(首字母大写) + 查询条件(首字母大写 Is Equals)
代码:
查询接口: public interface UserRepositoryByName extends Repository{ List findByName(String name); List findByNameAndAge(String name, Integer age); List findByNameOrAge(String name, Integer age); List findByNameLike(String name); }
测试: @SpringBootTest class ApplicationTests { @Autowired private UserRepositoryByName userRepositoryByName; @Test void testSelectByName() { List2.注解users = userRepositoryByName.findByName("苏九儿"); // name查询 System.out.println("name查询:"); for (User user : users) { System.out.println(user.toString()); } List byNameAndAge = userRepositoryByName.findByNameAndAge("苏九儿", 23); // name和age查询 System.out.println("name和age查询:"); for (User user : byNameAndAge) { System.out.println(user.toString()); } List byNameOrAge = userRepositoryByName.findByNameOrAge("苏九儿", 16); System.out.println("name或age查询:"); for (User user : byNameOrAge) { System.out.println(user.toString()); } List byNameLike = userRepositoryByName.findByNameLike("%九%"); System.out.println("name模糊查询:"); for (User user : byNameLike) { System.out.println(user.toString()); } } }
@Query
创建自定义接口,继承类Repository;创建方法实现数据查询;
注: update,更新数据需要在调用时添加事务
代码:
查询接口创建: public interface UserRepositoryAnnotation extends Repository{ @Query(value = "from User where name = ?1") List findJpql(String name); @Query(value = "update User set age = ?1 where id = ?2") @Modifying void updateAge(Integer age,Integer id); @Query(value= "select * from t_user where name=?1", nativeQuery = true) List findByName(String name); @Query(value="select * from t_user where name like ?", nativeQuery = true) List findByNameL(String name); }
测试: @SpringBootTest class ApplicationTests { @Autowired private UserRepositoryAnnotation userRepositoryAnnotation; @Test @Transactional void testSelectByAnnotation(){ List(二)、CurdRepositorybyName = userRepositoryAnnotation.findJpql("苏九儿"); System.out.println("name查询:"); for (User user : byName) { System.out.println(user.toString()); } System.out.println("更新age:"); userRepositoryAnnotation.updateAge(10, 1); List byNameHSQL = userRepositoryAnnotation.findByName("三三"); System.out.println("HSQL name查询:"); for (User user : byNameHSQL) { System.out.println(user.toString()); } List byNameLikeHSQL = userRepositoryAnnotation.findByNameL("%三%"); System.out.println("HSQL name Like查询:"); for (User user : byNameLikeHSQL) { System.out.println(user.toString()); } } }
继承接口Repository包含其所有方法特性;主要提供对数据库的增、删、改、查;
(三)、PagingAndSortingRepository
继承接口CurdRepository,包含其所有方法特性;主要提供分页查询及排序查询;
代码:
public interface UserPageSortRepository extends PagingAndSortingRepository{ }
测试: @SpringBootTest class ApplicationTests { @Autowired private UserPageSortRepository userPageSortRepository; @Test void testPagingAndSort(){ // sort Sort sort = Sort.by(Sort.Direction.ASC, "name"); List(四)、JpaRepositoryusers = (List ) userPageSortRepository.findAll(sort); System.out.println("sort 查询:"); for (User user : users) { System.out.println(user.toString()); } // page Pageable pageable = PageRequest.of(0,2); Page byPage = userPageSortRepository.findAll(pageable); System.out.println("page 查询:"); System.out.println("大小:" + byPage.getTotalElements()); System.out.println("当前页:" + byPage.getNumber()); System.out.println("每页大小:" + byPage.getSize()); for (User user : byPage.getContent()) { System.out.println(user.toString()); } } }
继承接口PagingAndSortingRepository包含其所有方法及特性;主要对接口PagingAndSortingRepository的查询结果进行了转换。
(五)、JpaSpecificationExecutor
主要提供对数据库的一些复杂查询,如条件分页、条件排序等;通过配合接口JpaRepository使用,单独继承接口JpaSpecificationExecutor会出现异常。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)