MyBatis-Plus 分页插件

MyBatis-Plus About 1,131 words

开启插件

@Configuration
public class MybatisPlusConfig {

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }
}

使用分页

基础类

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TestMapper extends BaseMapper<Test> {


}

操作类

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class TestDAO
    extends ServiceImpl<TestMapper, Test>
    implements IService<Test> {

}

分页方法

LambdaQueryWrapper<Test> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Test::getId);
Page<Test> page = testDAO.page(Page.of(1, 10), wrapper);
long total = page.getTotal();
List<Test> records = page.getRecords();
Views: 142 · Posted: 2024-10-28

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh