MyBatisPlus中的分页插件
首先要有MyBatisPlus的依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
接着是配置类
package com.example.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author liubin
* @create 2021-02-21
*/
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
/**
* 分页插件配置
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
下面是使用分页。
如果是使用mapper层的CRUD接口,
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public IPage<Book> getBookByPage(Integer currentPage, Integer pageSize) {
Page<Book> bookPage = new Page<>(currentPage, pageSize);
Page<Book> selectPage = bookMapper.selectPage(bookPage, null);
return selectPage;
}
}
如果是使用service层的CRUD接口,
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public IPage<Book> getBookByPage(Integer currentPage, Integer pageSize) {
Page<Book> bookPage = new Page<>(currentPage, pageSize);
Page<Book> page = page(bookPage,null);
return page;
}
}