mybaties-plus分页
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
1. pom.xml添加依赖
注意:mybaties-plus版本的问题,可能导致出现错误,此处使用的是3.5.1版本
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.1</version>
</dependency>
2. 添加配置类
package com.example.demo.commoms.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
3. 测试分页
通过mapper的自带的selectPage方法进行分页查询
package com.example.demo.role;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dao.IRoleMapper;
import com.example.demo.entity.Role;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class GetPageRoleTest {
@Resource
IRoleMapper roleMapper;
private static final Integer current = 1;
private static final Integer pageSize = 5;
@Test
public void GetPageRole() {
Page<Role> rolePage = new Page<>(current, pageSize);
QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
Page<Role> page = roleMapper.selectPage(rolePage, queryWrapper);
System.out.println("total: " + page.getTotal());
System.out.println("current: " + page.getCurrent());
System.out.println("page: " + page.getSize());
}
}
4. 自定义分页
4.1 Mapper文件
@Mapper
public interface IRoleMapper extends BaseMapper<Role> {
IPage<Role> selectRoleByPage(Page<?> page);
}
4.2 对应Mapper.xml文件
<?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.demo.dao.IRoleMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.Role">
<id property="roleId" column="role_id" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="status" column="status" jdbcType="INTEGER"/>
</resultMap>
<select id="selectRoleByPage" resultType="com.example.demo.entity.Role">
SELECT * FROM role;
</select>
</mapper>
4.3 测试
package com.example.demo.role;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dao.IRoleMapper;
import com.example.demo.entity.Role;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class GetPageRoleTest {
@Resource
IRoleMapper roleMapper;
private static final Integer current = 2;
private static final Integer pageSize = 5;
@Test
public void GetPageRoleByCustomer() {
Page<Role> rolePage = new Page<>(current, pageSize);
IPage<Role> roleIPage = roleMapper.selectRoleByPage(rolePage);
System.out.println("total: " + roleIPage.getTotal());
System.out.println("current: " + roleIPage.getCurrent());
System.out.println("page: " + roleIPage.getSize());
}
}
|