MyBatis-Plus自定义分页查询

MyBatis-Plus自定义分页查询

1、model

1.1、自定义分页类

继承Page接口

package com.wnx.mall.tiny.modules.test.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class MyPage<T> extends Page<T> {
    private Integer selectInt;
    private String selectStr;

    public MyPage(long current, long size) {
        super(current, size);
    }
}

1.2、设想的查询条件

package com.wnx.mall.tiny.modules.test.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ParamSome {

    private Integer param1;
    private String param2;
}

2、定义方法查询

2.1、mapper接口

package com.wnx.mall.tiny.modules.test.mapper;

import com.wnx.mall.tiny.modules.test.model.MyPage;
import com.wnx.mall.tiny.modules.test.model.ParamSome;
import com.wnx.mall.tiny.modules.test.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;

/**
 * 

* Mapper 接口 *

* * @author wangnaixing * @since 2022-01-11 */
public interface UserMapper extends BaseMapper<User> { /** * 3.x的page可以进行取值,多个入参记得加上注解! * 自定义的page类,必须放在入参第一位 * 返回值可以用IPage接收,也可以使用入参的MyPage接收 * @param myPage 自定义page * @param paramSome 条件查询的参数 * @return */ MyPage<User> mySelectPage(@Param("myPage") MyPage<User> myPage,@Param("paramSome") ParamSome paramSome); }

2.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.wnx.mall.tiny.modules.test.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.wnx.mall.tiny.modules.test.model.User">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="age" property="age" />
        <result column="email" property="email" />
    </resultMap>
    <select id="mySelectPage" resultType="com.wnx.mall.tiny.modules.test.model.User">
        select * from user
            where age = #{myPage.selectInt} and name = #{myPage.selectStr}
               or age = #{paramSome.param1} and name = #{paramSome.param2}
    </select>


</mapper>

3、测试

       log.error("-------------------自定义 XML 分页-------------------------------------------------------");

        ParamSome paramSome = new ParamSome(20, "Jack");
        MyPage<User> page6 = mapper.mySelectPage(new MyPage<User>(1, 5).setSelectInt(20).setSelectStr("Jack"),
                paramSome);
        log.error("总条数 -------------> {}", page6.getTotal());
        log.error("当前页数 -------------> {}", page6.getCurrent());
        log.error("当前每页显示数 -------------> {}", page6.getSize());
        page6.getRecords().forEach(System.out::println);

3.1、结果

022-01-11 18:30:55.212 ERROR 13848 --- [           main] com.wnx.mall.tiny.PaginationTest         : ----------------------------------自定义 XML 分页-------------------------------------------------------
2022-01-11 18:30:55.218 DEBUG 13848 --- [           main] c.w.m.t.m.t.m.UserMapper.mySelectPage    : ==>  Preparing: SELECT COUNT(1) FROM user WHERE age = ? AND name = ? OR age = ? AND name = ? 
2022-01-11 18:30:55.219 DEBUG 13848 --- [           main] c.w.m.t.m.t.m.UserMapper.mySelectPage    : ==> Parameters: 20(Integer), Jack(String), 20(Integer), Jack(String)
2022-01-11 18:30:55.222 DEBUG 13848 --- [           main] c.w.m.t.m.t.m.UserMapper.mySelectPage    : ==>  Preparing: select * from user where age = ? and name = ? or age = ? and name = ? LIMIT ?,? 
2022-01-11 18:30:55.222 DEBUG 13848 --- [           main] c.w.m.t.m.t.m.UserMapper.mySelectPage    : ==> Parameters: 20(Integer), Jack(String), 20(Integer), Jack(String), 0(Long), 5(Long)
2022-01-11 18:30:55.227 DEBUG 13848 --- [           main] c.w.m.t.m.t.m.UserMapper.mySelectPage    : <==      Total: 5
2022-01-11 18:30:55.227 ERROR 13848 --- [           main] com.wnx.mall.tiny.PaginationTest         : 总条数 -------------> 13
2022-01-11 18:30:55.228 ERROR 13848 --- [           main] com.wnx.mall.tiny.PaginationTest         : 当前页数 -------------> 1
2022-01-11 18:30:55.228 ERROR 13848 --- [           main] com.wnx.mall.tiny.PaginationTest         : 当前每页显示数 -------------> 5
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Jack, age=20, email=test2@baomidou.com)
User(id=4, name=Jack, age=20, email=test2@baomidou.com)
User(id=5, name=Jack, age=20, email=test2@baomidou.com)
User(id=6, name=Jack, age=20, email=test2@baomidou.com)

你可能感兴趣的:(Mybatis,intellij-idea,java,intellij,idea)