SpringBoot根据pageHelper插件实现mybatis的分页查询

本文介绍了如何在SpringBoot项目中利用PageHelper插件进行Mybatis的分页查询。首先,需要导入PageHelper的jar包。接着,在mybatis-config.xml文件中配置拦截器interceptor。然后,在测试类中调用PageHelper的startPage()方法来启动分页。通过PageHelper的分页功能,可以轻松实现动态查询,简化代码并提高效率。
摘要由CSDN通过智能技术生成
1.导入jar包
		<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-autoconfigure</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
2.mybatis-config.xml文件中配置拦截器interceptor
	<plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!--设置数据库类型-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
3.测试类中调用startPage()方法
	@Test
    public void pageSelect(){
        Page<User> startPage = PageHelper.startPage(3,3);
        Page<User> users = userMapper.selectByPage();
        System.out.println(users);
    }
4.分析
  1. PageHelper继承PageMethod类
    public class PageHelper extends PageMethod

  2. PageMethod类中有Page类型的静态方法,子类可以继承startPage

	public static <E> Page<E> startPage(int pageNum, int pageSize) {
        return startPage(pageNum, pageSize, DEFAULT_COUNT);
    }
  1. 调用Page<E> pageStart = PageHelper.startPage(int pageNum, int pageSize)可以实现分页查询
  2. Page类的源码属性如下:
public class Page<E> extends ArrayList<E> implements Closeable {
    private static final long serialVersionUID = 1L;
    //当前页码
    private int pageNum;
    //当前页对应的信息条数
    private int pageSize;
    //起始页码
    private int startRow;
    //终止页码
    private int endRow;
    //查询信息的总条数
    private long total;
    //分页的总页数
    private int pages;
    private boolean count;
    private Boolean reasonable;
    private Boolean pageSizeZero;
    private String countColumn;
    private String orderBy;
    private boolean orderByOnly;
    ................
}

动态查询
查询集合
例:select * from user where id in{1,2,3,4,6,10};
	<select id="findByForeachTable" resultType="com.pojo.User" parameterType="user">
        select * from user
        <where>
            <if test="list != null">
                <foreach collection="list" open=" and id in (" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
根据名称和性别查询
	<select id="findByCondition" parameterType="com.pojo.User" resultType="user">
        select * from user
        <where>
            <if test="user_name !=null">
                and user_name = #(userName)
            </if>
            <if test="user_sex != null">
                and user_sex = #{userSex}
            </if>
        </where>
    </select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值