- Repository
MyRepository extends JpaRepository<MyEntity, Integer>
精确查询:
Example 包装Entity
Pageable pageable = PageRequest.of(current - 1, pageSize); //myEntity 实体类参数 Example example = Example.of(myEntity); Page<MyEntity> page = myRepository.findAll(example, pageable);
模糊查询
@Test void testQuery() { MyEnitty MyEnitty = new MyEnitty(); MyEnitty.setUserName("ab"); MyEnitty.setAddress("ef"); ExampleMatcher exampleMatcher = ExampleMatcher.matching() //前綴精确匹配ab% .withMatcher("userName", ExampleMatcher.GenericPropertyMatcher::startsWith) //前后缀都模糊匹配%ef% .withMatcher("address", ExampleMatcher.GenericPropertyMatcher::contains); Example<MyEnitty> example = Example.of(MyEnitty, exampleMatcher); List<MyEnitty> result = MyEnittyRepository.findAll(example); for (MyEnitty mt : result) { System.out.println(mt.getUserName + ":" + mt.getAddress); } }