springboot mybatis-plus 分頁

1.下載mybatis-plus依賴

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>

2. 添加分頁攔截器

package com.hanhuide.driver.config;

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

/**
 * @author 韓惠德
 * @Description
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        //設置方言類型
        page.setDialectType("mysql");
        return page;
    }
}

這樣就可以使用mybatis分頁了

mapper.xml

  <select id="selectPage" resultMap="BaseResultMap">
        select * from sys_user
    </select>

mapper接口

public interface UserMapper extends BaseMapper<SysUser>

service

    List<SysUser> findAll(Page<SysUser> userPage, Object o);

實現接口

 @Override
    public List<SysUser> findAll(Page<SysUser> userPage, Object o) {
        return userMapper.selectPage(userPage,null);
    }

寫contrller

package com.hanhuide.core.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.hanhuide.core.model.SysUser;
import com.hanhuide.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ProjectName: maven
 * @Package: com.hanhuide.core.controller
 * @ClassName: UserController
 * @Author: 韓惠德
 * @Description: 用戶管理
 * @Date: 2020/1/15 14:20
 * @Version: 1.0
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/list")
    public List<SysUser> list(){
        Page<SysUser> userPage = new Page<>(2, 2);//參數一是當前頁,參數二是每頁個數
        return userService.findAll(userPage, null);
    }
}

測試效果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章