MyBatisPlusのページ付け

注:環境はSpringBootです

1. MyBatisPlusランチャーを通常どおりインポートします

        <!--mybatis plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>

mysql-connector-java、druidなどの残りの依存関係は書き込まれません。

2.application.yml配置

mybatis-plus:
  mapper-locations: classpath:/mapperxml/*.xml
  # type-aliases-package: com.bjsxt.pojo
  configuration:
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

mysql接続パラメータは書き込まれていません。

3.ページングインターセプターを構成します

このページングインターセプターが構成されていない場合、ページング機能は不均一になります。

@Configuration
public class EgoMyBatisPlusConfigration {
    
    

    /**
     * 创建一个MyBatis Plus分页拦截器
     * 在执行MyBatis数据库访问之前,进行SQL语法拦截
     * 拦截操作是,检查查询操作中,是否存在IPage接口实现类型对象。
     * 如果有,则根据驱动driver-class-name,动态拼接分页语法
     * 如:MySQL拼接limit ?,?
     *    Oracle拼接rownum伪列子查询。
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
    
    
        return new PaginationInterceptor();
    }

}

4.ページングクエリ

    public List<Item> selectItemByPage(Integer page, Integer rows) {
    
    
        try {
    
    
            Page<Item> queryPage=new Page<Item>(page,rows);//分页拦截
            // selectPage(E page, @Param("ew") Wrapper<T> queryWrapper)分页查询
            //getRecords():获取查询出来的所有行数据
            return itemMapper.selectPage(queryPage,null).getRecords();
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

おすすめ

転載: blog.csdn.net/weixin_44613100/article/details/107984499