栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus的分页查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus的分页查询

文章目录
    • 1.分页查询的配置类
    • 2.分页查询的操作,测试
    • 3.测试,获得查询结果
    • 4.将内容易Json形式返回前端

1.分页查询的配置类
@Configuration
public class MybatisPlusPageConfig {
	
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor(){
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
		// DbType:数据库类型(根据类型获取应使用的分页方言)
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 分页插件
		return interceptor;
	}


}
2.分页查询的操作,测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class pageTest {
	@Resource
	private StudentMapper studentMapper;

	@Test
	public void testPage(){
		//第一个参数:当前页     第二个参数:页面条数
		Page studentPage = new Page<>(1,4);
		//设置条件
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gender","女");
		//查询
		studentMapper.selectPage(studentPage,queryWrapper);
		System.out.println("总页数: " + studentPage.getPages());
		System.out.println("总记录数: " + studentPage.getTotal());
		//输出结果	
		studentPage.getRecords().forEach(System.out::println);


	}
}
3.测试,获得查询结果

4.将内容易Json形式返回前端

将方法的返回类型改为Json

@GetMapping("/testPage")
	public JSON testPage(){
		//第一个参数:当前页     第二个参数:页面大小
		Page studentPage = new Page<>(1,4);
		//设置条件
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gender","女");
		//查询
		studentMapper.selectPage(studentPage,queryWrapper);
		//控制台输出
		System.out.println("总页数: " + studentPage.getPages());
		System.out.println("总记录数: " + studentPage.getTotal());
		studentPage.getRecords().forEach(System.out::println);
		//返回前端页面
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("total", studentPage.getTotal());
		jsonObject.put("rows",studentPage.getRecords());
		jsonObject.put("page", studentPage.getCurrent());
		return jsonObject;

	}

测试,查看结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943726.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号