Mybatis Plus 分页查询数据
一、分页配置
package com.example.demomybatispage.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@MapperScan("com.example.demomybatispage.mapper")
@Configuration
public class MyBatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
return interceptor;
}
}
二、使用分页功能
package com.example.demomybatispage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demomybatispage.mapper.EnumDemoMapper;
import com.example.demomybatispage.pojo.EnumTest;
import com.example.demomybatispage.service.EnumDemoService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class DemoMybatisPageApplicationTests {
@Autowired
EnumDemoMapper enumDemoMapper;
@Autowired
EnumDemoService enumDemoService;
/**
* 测试mapper
*/
@Test
void contextLoads() {
QueryWrapper<EnumTest> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().ge(EnumTest::getNum,1);
Page<EnumTest> page = new Page<>(1, 5);
//List<EnumTest> enumTests = enumDemoMapper.selectList(null);
enumDemoMapper.selectPage(page, queryWrapper);
System.out.println("总记录数:" + page.getTotal());
System.out.println("总共多少页:" + page.getPages());
System.out.println("当前页码:" + page.getCurrent());
// 当前页数据
List<EnumTest> users = page.getRecords();
System.out.println(users);
}
/**
* 测试service
*/
@Test
void demoServicePage() {
QueryWrapper<EnumTest> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().ge(EnumTest::getNum,1);
Page<EnumTest> page = new Page<>(1, 5);
enumDemoService.page(page,queryWrapper);
System.out.println("总记录数:" + page.getTotal());
System.out.println("总共多少页:" + page.getPages());
System.out.println("当前页码:" + page.getCurrent());
// 当前页数据
List<EnumTest> users = page.getRecords();
System.out.println(users.size());
}
/**
* 测试自定义
*/
@Test
void demoCustomizationPage() {
QueryWrapper<EnumTest> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().ge(EnumTest::getNum,1);
Page<EnumTest> page = new Page<>(1, 20);
enumDemoMapper.testPage(page);
System.out.println("总记录数:" + page.getTotal());
System.out.println("总共多少页:" + page.getPages());
System.out.println("当前页码:" + page.getCurrent());
System.out.println("每页的数量:" + page.getSize());
// 当前页数据
List<EnumTest> users = page.getRecords();
System.out.println(users.size());
}
}
mapper
package com.example.demomybatispage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demomybatispage.pojo.EnumTest;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EnumDemoMapper extends BaseMapper<EnumTest> {
/**
* 测试分页插件
*
* @param page
* @return
*/
Page<EnumTest> testPage(Page<EnumTest> page);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demomybatispage.mapper.EnumDemoMapper">
<select id="testPage" resultType="com.example.demomybatispage.pojo.EnumTest">
select * from enum_test
</select>
</mapper>
标签:分页,System,println,Plus,Mybatis,import,com,page,out
From: https://www.cnblogs.com/firsthelloworld/p/17711513.html