MybatisPlus实现分页——自写SQL

#一、引入依赖

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

#二、编写SQL,返回list

/**
 * (SumTable)表数据库访问层
 *
 * @author zdh
 * @since 2024-04-12 10:49:24
 */
public interface SumTableMapper extends BaseMapper<SumTable> {

    Page<SumTable> selectAll(@Param("page") Page<SumTable> page);

}
<?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="cn.yun.bicarbon.mapper.SumTableMapper">
  <resultMap id="BaseResultMap" type="cn.yun.bicarbon.entity.table.SumTable">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="pid" jdbcType="INTEGER" property="pid" />
    <result column="x" jdbcType="VARCHAR" property="x" />
    <result column="y" jdbcType="VARCHAR" property="y" />
    <result column="value" jdbcType="DOUBLE" property="value" />
    <result column="level" jdbcType="INTEGER" property="level" />
    <result column="unit" jdbcType="VARCHAR" property="unit" />
    <result column="year" jdbcType="INTEGER" property="year" />
    <result column="meter_header" jdbcType="VARCHAR" property="meterHeader" />
    <result column="city" jdbcType="INTEGER" property="city" />
  </resultMap>
  <sql id="Base_Column_List">
    id, pid, x, y, `value`, `level`, unit, `year`, meter_header
  </sql>

  <select id="selectAll" resultType="cn.yun.bicarbon.entity.table.SumTable">
    select
    <include refid="Base_Column_List" />
    from sum_table
  </select>


</mapper>

#三、在实现类中对list进行处理

public Page<SumTable> selectAll(Page<SumTable> page) {
      
        return sumTableMapper.selectAll(page);
    }

#四、传入page参数和条件

/**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param sumTable 查询实体
     * @return 所有数据
     */
    @GetMapping("a")
    public R selectAll(Page<SumTable> page, SumTable sumTable) {
//        return success(this.sumTableService.page(page, new QueryWrapper<>(sumTable)));
        return success(sumTableService.selectAll(page));
    }

#五、配置类

package cn.yun.bicarbon.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值