淘淘商城01-分页插件的使用

一、分页插件PageHelper。

  1、配置mybatis的SqlMapConfig.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 配置使用什么数据库:数据库方言 -->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

  2、写一个测试类。

public class TestPageHelper {

    @Test
    public void testPageHelper() {
        //1.初始化spring容器。
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/*.xml");
        //2.获取mapper对象
        TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
        //3使用pageHelper设置条件,且只对第一条查询有效
        PageHelper.startPage(1,3);
        //4、查询tb_item表,example没有加条件相当于select * from tb_item;
        TbItemExample example = new TbItemExample();
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        //5、取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItems);
        //6、打印查询结果
        for(TbItem item : tbItems) {
            System.out.println(item.getTitle());
        }
    }
}

  3、控制台只打印三条数据

原文地址:https://www.cnblogs.com/huclouy/p/9538884.html