mybatisPlus-基于springboot实现分页

1.引入依赖-pom.xml


<dependency>
     <groupId>com.baomidougroupId>
     <artifactId>mybatis-plus-boot-starterartifactId>
     <version>3.2.0version>
 dependency>
 
 <dependency>
     <groupId>mysqlgroupId>
     <artifactId>mysql-connector-javaartifactId>
     <version>5.1.27version>
 dependency>

2.在application.yml配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mybaits?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456

3.在config包下面增加在里面建一个MybatisPlus配置类 返回一个分页拦截器,

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
@MapperScan("com.mk.springboot.mapper") // @MapperScan注解,扫描mapper包
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

4. 增加user类(我这边使用类lombok )

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "users")
public class User {
   @TableId(type = IdType.AUTO)
   private Integer id;
   private String username;
   private String pwd;
   private String trueName;
   private Integer sex;
   private Integer age;
   private Date birthday;
}

5.UserMapper接口

@Repository
public interface UserMapper extends BaseMapper<User> {
}

6. 直接上测试代码

@Resource
   private UserMapper userMapper;
   @Test
   public void queryUserForPage(){
       IPage<User> userPage = new Page<>(2, 2);//参数一是当前页,参数二是每页个数
       userPage = userMapper.selectPage(userPage, null);
       List<User> list = userPage.getRecords();
       for(User user : list){
           System.out.println(user);
       }
   }

你可能感兴趣的:(springboot)