SpringBoot+Mybatis-Plus兩種分頁方法

首先配置mybatis-plus配置

package com.qfclo.login.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration

@MapperScan("com.qfclo.login.mapper")

public class MybatisPlusConfig {

/**

* 分頁插件

*/

    @Bean

    public PaginationInterceptorpaginationInterceptor() {

return new PaginationInterceptor();

    }

/**

* 打印 sql

*/

    @Bean

    public PerformanceInterceptorperformanceInterceptor() {

PerformanceInterceptor performanceInterceptor =new PerformanceInterceptor();

        //格式化sql語句

        Properties properties =new Properties();

        properties.setProperty("format", "faalse");

        performanceInterceptor.setProperties(properties);

        return performanceInterceptor;

    }

}

第一種方式,mybatis-plus原生QueryWrapper方式分頁,這種方式比較簡單,可以不用修改Mapper,適合簡單的增刪改查。

    @RequestMapping(value = "/orgist1")//,method = RequestMethod.POST)
    public Map<String,Object> orglist1()
    {

        Map<String,Object> map = new HashMap<>();

        QueryWrapper<OauthOrganization> queryWrapper =  new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        Page<OauthOrganization> page = new Page<>(1,5);  // 查詢第1頁,每頁返回5條
        IPage<OauthOrganization> iPage = oauthOrganizationMapper.selectPage(page,queryWrapper);
        System.out.println(iPage.getRecords().size());
        System.out.println(JSON.toJSONString(iPage));
        return map;
    }

第二種方式,使用mapper文件的select注解,優(yōu)點是可以方便的建立查詢語句,可以聯(lián)合多表查詢。
Mapper文件

    @Select("SELECT * FROM oauth_organization WHERE id < #{m.id} ORDER BY `id` DESC")
    List<OauthOrganization> selectpage(Map<String,Object> m, Page<OauthOrganization> page);

Controller文件

    @RequestMapping(value = "/orgist4")//,method = RequestMethod.POST)
    public Map<String,Object> orglist4()
    {

        Map<String,Object> map = new HashMap<>();
        Map<String,Object> m = new HashMap<>();
        m.put("id",5);
        Page<OauthOrganization> page = new Page<>(1,5);
        page.setRecords(oauthOrganizationMapper.selectpage(m,page));
        System.out.println(page.getRecords().size());
        System.out.println(JSON.toJSONString(page));
        return map;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容