当前位置:   article > 正文

SpringDataJpa+MongoDB进行分页查询和模糊查询_jpa 实现mongodb的分页查询

jpa 实现mongodb的分页查询

参考:https://blog.csdn.net/weixin_43935907/article/details/91354738
的’方式二:通过原生的方法实现条件查询、分页和排序’

利用org.springframework.data.domain包的Pageable类实现.
查询方式一:无条件分页查询

	public List<MatchsInfo> findPage(int currentPage,int pageSize) {


		Pageable pageableObj = PageRequest.of(currentPage,pageSize );

		Page<MatchsInfo> all = matchsInfoRepositoryObj.findAll(pageableObj);
		System.out.println(all);
//通过增强for遍历
		for (MatchsInfo matchsInfo : all) {
			System.out.println(matchsInfo);
		}

		return null;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

查询方式二:有条件精确分页查询

	public List<MatchsInfo> findPage(int currentPage,int pageSize) {

		//通过实体类来定义查询条件
		MatchsInfo matchsInfoObj = new MatchsInfo();
		matchsInfoObj.setfIndex("5");//条件一:findex字段的数据为5的
		matchsInfoObj.setfScale("2");//条件二:fScale字段的数据为2的
		
		//将实体类对象传入Example对象中
		Example<MatchsInfo> exampleObj = Example.of(matchsInfoObj);
		Pageable pageableObj = PageRequest.of(currentPage,pageSize);

	//传入Example对象和Pageable对象
		Page<MatchsInfo> all = matchsInfoRepositoryObj.findAll(exampleObj, pageableObj);
		System.out.println(all);
//通过增强for遍历
		for (MatchsInfo matchsInfo : all) {
			System.out.println(matchsInfo);
		}

		return null;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

查询方式三:where id = ? and name = ?,有条件并且式多字段模糊分页查询
参考:https://blog.csdn.net/u010721764/article/details/90444094?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

		int currentPage = 0;
		int pageSize = 10;
		//通过实体类来定义查询条件
		MatchsInfo matchsInfoObj = new MatchsInfo();
		matchsInfoObj.setMatchName("日");
		
		//将实体类对象传入Example对象中
		Example<MatchsInfo> exampleObj = Example.of(matchsInfoObj, ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING));//CONTAINING就是模糊匹配的意思
		Pageable pageableObj = PageRequest.of(currentPage,pageSize);

	//传入Example对象和Pageable对象
		Page<MatchsInfo> all = matchsInfoRepositoryObj.findAll(exampleObj, pageableObj);
		System.out.println(all);
//通过增强for遍历
		for (MatchsInfo matchsInfo : all) {
			System.out.println(matchsInfo);
		}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

查询方式四:where id = ? or name = ?,有条件或者式多字段模糊分页查询
参考:https://blog.csdn.net/weixin_30922589/article/details/96017599
https://blog.csdn.net/qq_28241535/article/details/103365854
https://blog.csdn.net/tjbsl/article/details/80620303
该方法与上面查询方法三还有不同,上面查询方法三的话,虽然可以通过实体类进行多条件分页查询,但是这是属于将拼接性质定义为’and’的情况下. 就相当于mysql的"where id = ? and name = ?"一样.
而此处我想举例的,则是类似于mysql的"where id = ? or name = ?"一样.是或者式的查询.
这项查询方式较为繁琐,但俗话说越繁琐则意味着功能越强.
这项查询方式不仅需要用到Pageable实现分页
需要用到Query对象进行条件确认,
需要用到Criteria对象进行查询条件拼接,
需要用到MongoJpa的模板注入对象’MongoTemplate’,进行最终查询.

@Controller
public class WebTest {
	@Autowired
	private MongoTemplate mongoTemplateObj;
	@RequestMapping("/emp")
	public void findInemp() {
		int currentPage = 0;
		int pageSize = 10;
		Pageable pageableObj = PageRequest.of(currentPage,pageSize); //先创建Pageble分页对象,定义当前页和显示数量

		Query queryObj = new Query();//创建Query对象
		Criteria criteriaObj = new Criteria();//创建Criteria查询语句拼接对象
		criteriaObj.orOperator(
				Criteria.where("homeName").regex("船"),//或者条件一 homeName字段中的数值包含'船'这个字. 此处的regex是正则表达式,'模糊查询'就是依靠这个来实现的.如果不为'regex()'方法而为'is()'方法,则为精准查询.
				Criteria.where("guestName").regex("船")//或者条件二 guestName字段中包含'船这个字;
				);

		
		queryObj.addCriteria(criteriaObj);//通过Query对象的addCriteria方法将Criteria查询语句拼接对象作为参数添加到里头.
		 long count = mongoTemplateObj.count(queryObj, MatchsInfo.class);//取总记录数时。注意,要在find方法执行前面才能获取到正确的记录数量。否则获取到的是分页之后的当页记录数量。
		List<MatchsInfo> find = mongoTemplateObj.find(queryObj.with(pageableObj), MatchsInfo.class);//Query对象通过with方法将Pageble分页对象作为分页参数添加,然后通过MongoTemplate注入对象的find方法将其和实体类作为参数传入进行多条件分页模糊查询.
		
		System.out.println(find.size());
		 for (int i = 0; i < find.size(); i++) {
			 System.out.println(find.get(i));
			}
		
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/55980
推荐阅读
相关标签
  

闽ICP备14008679号