dao层我尝试使用JPA自带的Like,但是不知道怎么进行分页,所以使用了@Query注解来完成
dao语句
@Query(value = "SELECT * from red_city_list where ztmc like %?1%",nativeQuery = true)
Page<CityRedListRecord> findByContentLike(String content, PageRequest pageRequest);
其中red_city_list表示查询的数据库名称,ztmc表示要查询的字段,CityRedListRecord表示实体类,content模糊查询字段
//1.分页 PageRequest pageRequest = PageRequest.of(cityRedListForm.getPageIndex(), cityRedListForm.getPageSize()); //2.模糊查询查询并分页 Page<CityRedListRecord> byZtmcLikePage = cityRedListRecordRepository.findByContentLike(cityRedListForm.getContent(), pageRequest);
cityRedListForm.getPageIndex(), cityRedListForm.getPageSize()分别为传入参数的页码,页数
byZtmcLikePage即为查询结果,可调用getContent()获取