mybatis pagehelper分页插件

 

1. 配置 插件

<!-- 日志 -->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <!-- 插件 -->
    <plugins>
          <!--  <plugin interceptor="com.pea.mybatis.asplugin.SelectInterceptor" />-->
        <plugin interceptor="com.github.pagehelper.PageInterceptor" >
            <property name="helperDialect" value="mysql"/>
            <property name="pageSizeZero" value="true"/>
        </plugin>
    </plugins>

2. 使用

    public static void main(String[] args) throws IOException {
        // 从配置文件构建 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 从 SqlSessionFactory 中获取 SqlSession
        try (SqlSession session = sqlSessionFactory.openSession()) {
            PageHelper.startPage(3, 5); // 第一步
            DeptMapper mapper = session.getMapper(DeptMapper.class);
            List<Dept> deptList = mapper.selectAll();
            PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList); // 第二步
            System.out.println("deptList.size() = " + deptList.size());
            for(Dept dept : deptList){
                System.out.println("dept = " + dept.toString());
            }
        }
    }

 

posted @ 2020-09-01 16:56  停不下的时光  阅读(118)  评论(0编辑  收藏  举报