spring boot 集成mybatis-plus实现分页

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



	com.baomidou
	mybatis-plus-boot-starter
	3.0.6

配置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 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 getIPage() {
        if (StrUtil.isBlank(order)) {
            Page page = new Page<>(Integer.parseInt(getCurrent()), Integer.parseInt(getSize()));
            return page;
        } else {
            Page page = new Page(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);
    }

请求示例:
spring boot 集成mybatis-plus实现分页_第1张图片
current和size分别是当前页和每页数据量。
order字段声明排序字段根据order字段来进行排序
orderType字段声明排序是升序还是降序.
后台接收写法:

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

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

你可能感兴趣的:(java,spring,boot,mybatis,plus,orm,数据库,spring,boot,myabtis-plus分页)