spring boot 集成mybatis-plus实现分页

11 篇文章 0 订阅
7 篇文章 0 订阅

此篇文章记录spring boot 集成mybatis-plus实现分页过程;
从pom文件开始:

<!-- mybatis plus-->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.0.6</version>
</dependency>

配置MybatisPlusConfig配置类

// 声明配置类
@Configuration
// 扫描mapper
@MapperScan("com.ybl.cloud.services.*.mapper*")
public class MybatisPlusConfig {
	//覆盖分页组件
	@Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        //声明方言类型
        page.setDialectType("mysql");
        return page;
    }
}

注意,没必要的情况下无须声明MybatisSqlSessionFactoryBean,例如下面:

// 没有特殊需要的情况下,无徐覆盖原生配置
    @Autowired
    private DataSource dataSource;
	@Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactory(){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean= new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

⚠️这样的配置会导致分页配置失效
声明数据排序枚举类(后续有用)

/**
* @Description: 数据库排序枚举类
* @Author: wwr
* @Date: 2018/12/10
*/
public enum Order {

    ASC("asc"), DESC("desc");

    private String des;

    Order(String des){
        this.des = des;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }
}

声明分页工厂:

public class PageFactory<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页
     */
    @TableField(exist = false)
    private String current;

    /**
     * 每页数据量
     */
    @TableField(exist = false)
    private String size;

    /**
     * 排序字段
     */
    @TableField(exist = false)
    private String order;

    /**
     * 排序类型
     */
    @TableField(exist = false)
    private String orderType;

    public void setOrder(String order) {
        this.order = camel2Underline(order);
    }

    @JsonIgnore
    public PageFactory() {
        this.current = "1";
        this.size = "5";
    }

    @JsonIgnore
    public Page<T> getIPage() {
        if (StrUtil.isBlank(order)) {
            Page<T> page = new Page<>(Integer.parseInt(getCurrent()), Integer.parseInt(getSize()));
            return page;
        } else {
            Page<T> page = new Page<T>(Integer.parseInt(current), Integer.parseInt(size));
            if (Order.ASC.getDes().equals(orderType)) {
                page.setAsc(getOrder());
            } else {
                page.setDesc(getOrder());
            }
            return page;
        }
    }


    /**
     * @Description: 驼峰转下划线
     * @Param: [line]
     * @return: java.lang.String
     * @Author: wwr
     * @Date: 2018-12-25
     */
    @JsonIgnore
    public static String camel2Underline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰法(默认小驼峰)
     *
     * @param line       源字符串
     * @param smallCamel 大小驼峰,是否为小驼峰(驼峰,第一个字符是大写还是小写)
     * @return 转换后的字符串
     */
    @JsonIgnore
    public static String underline2Camel(String line, boolean... smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        //匹配正则表达式
        while (matcher.find()) {
            String word = matcher.group();
            //当是true 或则是空的情矿
            if ((smallCamel.length == 0 || smallCamel[0]) && matcher.start() == 0) {
                sb.append(Character.toLowerCase(word.charAt(0)));
            } else {
                sb.append(Character.toUpperCase(word.charAt(0)));
            }
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }


}

解析:

getIPage()方法,拿到Page对象,组装page分页需要参数。然后调用mybatis-plus原生分页方法。
原生实体类需要继承PageFactory

使用方法:
bdCustomerService.page(bdCustomer.getIPage(), wrapper);
@RequestMapping(value = "/pageTest", method = RequestMethod.POST)
    public Result pageTest(@RequestBody BdCustomer bdCustomer) {
        QueryWrapper wrapper = getBeginAndEndTime();
        wrapper.setEntity(bdCustomer);
        IPage page = bdCustomerService.page(bdCustomer.getIPage(), wrapper);
        return Result.success(page);
    }

请求示例:
在这里插入图片描述
current和size分别是当前页和每页数据量。
order字段声明排序字段根据order字段来进行排序
orderType字段声明排序是升序还是降序.
后台接收写法:

bdCustomerPage(@RequestBody(required = false) BdCustomerForm bdCustomerForm)

用@RequestBody声明参数,@RequestBody注解将会把body里面的参数全部映射到对应的实体上,此处针对post请求,如果是get请求,则@RequestBody会把body里面的参数格式化为json串,传到第一个参数上。
在这里插入图片描述
仅做记录。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值