mybatisPlus-基于springboot实现分页

1.引入依赖-pom.xml

<!-- 引入mybatisPlus -->
<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.2.0</version>
 </dependency>
 <!-- 引入mysql驱动包 -->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.27</version>
 </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);
       }
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值