1.导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.7</version>
</dependency>
</dependencies>
2.创建一个配置类
//标明此类为Spring的配置类
@Configuration
//扫描所有mapper接口的实现,让这些mapper能够自动注入
@MapperScan(basePackages = "com.xiao.mp.mapper")
public class MybatisPlusConfig {
@Bean("sqlSessionFactory")
public SqlSessionFactory SqlSessionFactory (MybatisPlusInterceptor interceptor) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();;
sqlSessionFactoryBean.setPlugins(interceptor);
return sqlSessionFactoryBean.getObject();
}
@Bean
public MybatisPlusInterceptor innerInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
Mybatis-plus在没有添加分页插件时,自带的分页方法是不会生效的。在向ioc容器添加MybatisPlusInterceptor之后,还要通过Mybatis-Plus的SqlSessionFactoryBean中的setPlugins()方法把分页插件设置上去,只有这样分页方法才能生效
3.测试方法
@Test
public void testPage() {
IPage<Employee> page = employeeMapper.selectPage(new Page<>(1, 2,true), null);
System.out.println("总页数:"+page.getPages());
System.out.println("总记录数:"+page.getTotal());
System.out.println("一页显示几条:"+page.getSize());
List<Employee> employees = page.getRecords();
employees.forEach(System.out::println);
}
> Preparing: SELECT COUNT() FROM tbl_employee
> Parameters:
< Columns: COUNT()
< Row: 10
<== Total: 1
> Preparing: SELECT id,last_name,email,gender,age FROM tbl_employee LIMIT ?
> Parameters: 2(Long)
< Columns: id, last_name, email, gender, age
< Row: 2, Jerry, jerry@atguigu.com, 0, 25
<== Row: 3, Black, black@atguigu.com, 1, 30
<== Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58be6e8]
总页数:5
总记录数:10
一页显示几条:2
Employee{id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25}
Employee{id=3, lastName=Black, email=black@atguigu.com, gender=1, age=30}