导入依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
配置Interceptor:
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
使用测试:
@Slf4j
@SpringBootTest
class DemoApplicationTests {
@Autowired
private DataSource dataSource;
@Autowired
private OrderService orderService;
@Test
void contextLoads() {
// log.info("数据源:{}", dataSource);
Page<Order> orderPage = new Page<>(1, 5);
Page<Order> page = orderService.page(orderPage);
long total = page.getTotal();
long pages = page.getPages();
log.info("total:{},pages:{}", total, pages);
}
}