ディレクトリ
1ページ付けプロセス
あなたがリバースエンジニアリングを失うので、リバースエンジニアリング生成されたコードは、あなたが書き込みマッパーへの必要性をページングしたい場合意味、ページ付けプロセスをサポートしていません。開発効率を向上させるためにPageHelperのページネーションプラグMyBatisのを使用することができます。
2ページネーションプラグPageHelper
MyBatisのページネーションプラグ - PageHelper説明
あなたはMyBatisのを使用している場合、我々はページネーションプラグインをお試しください、これはページネーションプラグインを使用するのが最も便利でなければなりません。
プラグインは現在Oracle、MySQLやMariaDB、SQLiteの、HSQLDB、PostgreSQLのデータベースページの6種類をサポートしています。
2.1使用
最初のステップ:PageHelperに依存するJARパッケージがプロジェクトに追加されます。リソースのpagehelperフィックスを使用して貧しい人々のためのリバースエンジニアリングをサポートするためのコードの公式、。
ステップ2:設定、プラグインのXML構成でMyBatisのインターセプター:
<?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>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql" />
</plugin>
</plugins>
</configuration>
ステップ2:コードを使用します
1、ページネーション情報を設定します。
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
図2に示すように、撮影したページング情報
//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();
図3に示すように、第2のページング情報メソッド
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());
3ページのテスト
package com.taotao.mybatis.pagehelper;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
public class TestPageHelper {
@Test
public void testPageHelper() throws Exception {
//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
//获得Mapper的代理对象
TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
//设置分页信息
PageHelper.startPage(1, 30);
//执行查询
TbItemExample example = new TbItemExample();
List<TbItem> list = itemMapper.selectByExample(example);
//取分页信息
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
System.out.println("结果集中的记录数"+list.size());
System.out.println("总记录数"+pageInfo.getTotal());
System.out.println("总页数"+pageInfo.getPages());
}
}