一、mybatisplus实现分页功能

1、创建配置类

/**
 * mybatisPlus分页配置
 */
@Configuration
//扫描mapper包
@MapperScan("com.cjc.mybatisplus.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

2、测试分页功能

@SpringBootTest
public class Paging {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void  selectPage(){
        Page<User> userPage = new Page<>(2,3);
        Page<User> userPage1 = userMapper.selectPage(userPage, null);
        System.out.println(userPage1);
    }
}

二、自定义分页功能

1、定义方法

 /**
     *自定义分页查询
     * @param page
     * @param age
     * @return
     */
     Page<User> selectPages(@Param("page") Page<User> page,@Param("age") Integer age);

2、映射文件

<!--selectPage-->
<select id="selectPages" resultType="user">
    select ids,t_name,age,email from t_user where age > #{age}
</select>
spring:
  # 配置数据源信息
  datasource:
    # 配置数据源类型
    type: com.zaxxer.hikari.HikariDataSource
    # 配置连接数据库信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3307/mybatis_plus?characterEncoding=utf-8&SSL=false
    username: root
    password: 123456
    # 配置mybatisplus日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 配置统一表的前缀名
  global-config:
    db-config:
      table-prefix: t_
      # 配置全局的主键生成策略
      id-type: auto
  # 配置包的别名
  type-aliases-package: com.cjc.mybatisplus.pojo

3、测试方法

@SpringBootTest
public class Paging {
    @Autowired
    private UserMapper userMapper;
    
    @Test
    public void  selectPage02(){
        Page<User> page = new Page<>();
        Page<User> userPage = userMapper.selectPages(page, 23);
        System.out.println(userPage);
    }
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐