SpringBoot集成mybatis-plus实现分页

1、依赖导入



    com.baomidou
    mybatis-plus-boot-starter
    3.4.2

2、配置mybatis-plus拦截器

package com.lezu.springboot.configuration.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 {
    /**
     * mybatis-plus分页插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

 3、对应表的实体类

package com.lezu.springboot.auto.code.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.lezu.springboot.common.Page;
import com.lezu.springboot.utils.handler.AESHandler;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.Date;

/**
 * 

* *

* * @author LianJiaYu * @since 2022-06-30 * * * 使用@Data时默认@equalsandhashcode的状态为callsuper=false * * @equalsandhashcode(callsuper=false)表达为在对象比较时不会考虑父类中的成员,仅仅比较子类中的属性就判断是否相同 * * @equalsandhashcode(callsuper=true)在比较时会考虑父类中的成员,通过父类和子类中的属性一起判断是否相同 */ @Data @EqualsAndHashCode(callSuper = true) @TableName(value = "lz_collocation",autoResultMap = true) @ApiModel(value = "Collocation对象", description = "") public class Collocation extends Page implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "房源id") @TableId(value = "house_id", type = IdType.AUTO) private Integer houseId; @ApiModelProperty(value = "房源地址") private String houseAddress; @ApiModelProperty(value = "房东姓名") @TableField(typeHandler = AESHandler.class) private String masterName; @ApiModelProperty(value = "业务员姓名") private String salesmanName; @ApiModelProperty(value = "房屋户型(室)") private String houseType1; @ApiModelProperty(value = "房屋户型(厅)") private String houseType2; @ApiModelProperty(value = "房屋户型(卫)") private String houseType3; @ApiModelProperty(value = "托管时间(起始时间)") private String time1; @ApiModelProperty(value = "托管时间(结束时间)") private String time2; @ApiModelProperty(value = "房屋面积") private Double area; @ApiModelProperty(value = "托管金额") private Double money; @ApiModelProperty(value = "房屋描述") private String remarks; @ApiModelProperty(value = "托管时间") private Date collocationTime; @ApiModelProperty(value = "房屋图片") private String houseImg; @ApiModelProperty(value = "标识符ID(修改使用)") private String flagId; }

4、编写代码

    /**
     * mybatis-plus分页查询
     *
     * @return
     */
    @PostMapping("/listCollocationByPage")
    @ApiOperation("mybatis-plus分页查询")
    public ListResult listCollocationByPage(Collocation inDto) {
        ListResult listResult = new ListResult();

        /*组装查询条件*/
        LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
        wrapper.like(Collocation::getHouseAddress,"阳光小区");

        /*进行分页查询操作*/
        Page page = new Page<>(inDto.getPageNum(), inDto.getPageSize());
        collocationService.page(page,wrapper);

        /*数据返回*/
        return listResult.ok(page.getRecords(), page.getTotal());
    }

你可能感兴趣的:(Java,spring,boot,java,mybatis-plus)