MyBatis Plus pagination

Note: The environment is Spring Boot

1. Import the MyBatis Plus launcher normally

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

The rest of the dependencies are not written, such as mysql-connector-java, druid, etc.

2. application.yml placement

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

mysql connection parameters have not been written.

3. Configure the paging interceptor

If this paging interceptor is not configured, the paging function will be uneven.

@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. Paging query

    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<>();
        }
    }

Guess you like

Origin blog.csdn.net/weixin_44613100/article/details/107984499