百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

「Mybatis-Plus学习笔记四」——Mybatis-Plus分页查询和条件查询

ztj100 2025-05-25 16:51 33 浏览 0 评论

1.分页查询

  • 配置分页插件
@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        //分页插件
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(
            DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

}
  • 测试
	@Test
	void contextLoads() {
		//创建一个 Page 对象(当前页默认1,一页大小默认10)
		Page<User> page = new Page<>();
		//Page<User> page = new Page<>(1,2);
		userMapper.selectPage(page,null);

		//获取当前页数据
		List<User> list= page.getRecords();
		list.forEach(System.out::println);

		System.out.println(page.getTotal()); // 获取总记录数
		System.out.println(page.getSize()); // 获取每页的条数
		System.out.println(page.getPages()); // 获取总页数
		System.out.println(page.hasNext()); // 是否存在下一页
		System.out.println(page.hasPrevious()); // 是否存在上一页
	}

2.QueryWrapper条件构造器使用

	@Test
	void contextLoads() {
		//创建一个 Page 对象(当前页默认1,一页大小默认10)
		Page<User> page = new Page<>();
		//查询条件构造器
		QueryWrapper<User> queryWrapper =new  QueryWrapper<User>();
		//模糊查询
		// queryWrapper.like("name", "小");
		// queryWrapper.eq("age", 18);
		//链式调用
		queryWrapper.like("name", "小").eq("age", 30);

		userMapper.selectPage(page,queryWrapper);

	}

3.LambdaQueryWrapper条件构造器使用

	@Test
	void contextLoads() {
		//创建一个 Page 对象(当前页默认1,一页大小默认10)
		Page<User> page = new Page<>();
		
		//lambda方式 条件构造器
		LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(User::getName, "小").eq(User::getAge, 30);

		userMapper.selectPage(page,wrapper);

	}

4.总结

  • QueryWrapper 的列名匹配使用的是 “数据库中的字段名(一般是下划线规则)”,列名要和数据库中一致。
  • LambdaQueryWrapper 的列名匹配使用的是“Lambda的语法,偏向于对象”,是使用纯java的方式,避免了拼写错误。

相关推荐

10条军规:电商API从数据泄露到高可用的全链路防护

电商API接口避坑指南:数据安全、版本兼容与成本控制的10个教训在电商行业数字化转型中,API接口已成为连接平台、商家、用户与第三方服务的核心枢纽。然而,从数据泄露到版本冲突,从成本超支到系统崩溃,A...

Python 文件处理在实际项目中的困难与应对策略

在Python项目开发,文件处理是一项基础且关键的任务。然而,在实际项目中,Python文件处理往往会面临各种各样的困难和挑战,从文件格式兼容性、编码问题,到性能瓶颈、并发访问冲突等。本文将深入...

The Future of Manufacturing with Custom CNC Parts

ThefutureofmanufacturingisincreasinglybeingshapedbytheintegrationofcustomCNC(ComputerNumericalContro...

Innovative Solutions in Custom CNC Machining

Inrecentyears,thelandscapeofcustomCNCmachininghasevolvedrapidly,drivenbyincreasingdemandsforprecisio...

C#.NET serilog 详解(c# repository)

简介Serilog是...

Custom CNC Machining for Small Batch Production

Inmodernmanufacturing,producingsmallbatchesofcustomizedpartshasbecomeanincreasinglycommondemandacros...

Custom CNC Machining for Customized Solutions

Thedemandforcustomizedsolutionsinmanufacturinghasgrownsignificantly,drivenbydiverseindustryneedsandt...

Revolutionizing Manufacturing with Custom CNC Parts

Understandinghowmanufacturingisevolving,especiallythroughtheuseofcustomCNCparts,canseemcomplex.Thisa...

Breaking Boundaries with Custom CNC Parts

BreakingboundarieswithcustomCNCpartsinvolvesexploringhowadvancedmanufacturingtechniquesaretransformi...

Custom CNC Parts for Aerospace Industry

Intherealmofaerospacemanufacturing,precisionandreliabilityareparamount.Thecomponentsthatmakeupaircra...

Cnc machining for custom parts and components

UnderstandingCNCmachiningforcustompartsandcomponentsinvolvesexploringitsprocesses,advantages,andcomm...

洞察宇宙(十八):深入理解C语言内存管理

分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“深入理解C语言内存管理”...

The Art of Crafting Custom CNC Parts

UnderstandingtheprocessofcreatingcustomCNCpartscanoftenbeconfusingforbeginnersandevensomeexperienced...

Tailored Custom CNC Solutions for Automotive

Intheautomotiveindustry,precisionandefficiencyarecrucialforproducinghigh-qualityvehiclecomponents.Ta...

关于WEB服务器(.NET)一些经验累积(一)

以前做过技术支持,把一些遇到的问题累积保存起来,现在发出了。1.问题:未能加载文件或程序集“System.EnterpriseServices.Wrapper.dll”或它的某一个依赖项。拒绝访问。解...

取消回复欢迎 发表评论: