まず、プラグプレゼンテーション
PageHelperは、任意の複雑なシングルテーブル、マルチテーブルページングをサポートするために、ページ付けプラグMybaitsためのものです。
第二に、基本的な使い方
springbootの例では、2つに配置され、一方は依存、書き込みコンフィギュレーションクラスの伝統的な導入である。application.yml設定を使用することです。
最初の
1.依存紹介
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
2. [設定プラグイン
/**
* @author Hanstrovsky
*/
@Configuration
public class MybatisConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
第2
1.依存紹介
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2. [設定プラグイン
pagehelper:
offset-as-page-num: true
row-bounds-with-count: true
page-size-zero: true
reasonable: true
auto-dialect: mysql
ページングデモ
public PageInfo<Student> findAll(Integer pageNum, Integer pageSize) {
//设置分页信息
PageHelper.startPage(pageNum, pageSize);
List<Student> students = studentDao.findAll();
PageInfo<Student> pageInfo = new PageInfo<>(students);
//返回分页对象
return pageInfo;
}
第三に、オブジェクト変換
前述したように、データベースのマッピングエンティティとの直接小出しタブオブジェクトのリストを示しています。しかし、多くの場合、オブジェクトを変換する必要があり、開発プロセスでは、オブジェクトは、DO DTOまたはVOを変換追加したり、いくつかのプロパティを削除することです。
私たちは、これを行うことができます:
/**
* @author Hanstrovsky
*/
@Service
public class TestPage {
@Autowired
private StudentDao studentDao;
public PageInfo<StudentVO> getAllStudent(Integer pageNum, Integer pageSize) {
// 1. 开启分页
PageHelper.startPage(pageNum, pageSize);
// 2. 从数据库中查询出
List<StudentDO> studentDos = studentDao.findAll();
// 3. 这一步的作用主要是为了获取分页信息
PageInfo studentDoPageInfo = new PageInfo<>(studentDos);
// 4. 创建需要分页的VoList
List<StudentVO> studentVos = new ArrayList<>();
// 5. 对象转换
for (StudentDO studentDO : studentDos) {
StudentVO studentVO = new StudentVO();
BeanUtils.copyProperties(studentDO, studentVO);
studentVO.setClassName("六年级二班");
studentVos.add(studentVO);
}
// 6.这一步的作用是将封装后的列表放到分页对象中
studentDoPageInfo.setList(studentVos);
return studentDoPageInfo;
}
}
ここでステップ3、最初のこれらのパラメータのページングを取得するために、PageInfo元のリストによってオブジェクトを作成し、詳細に説明します。私が最初にVolistに変換付与Dolistのためにそれを撮ってみました前に、そして、pageInfoオブジェクトを作成し、そう、とページネーション情報を取得することはできません。
実際には、これを行うことができ、それは元のオブジェクトのページングパラメータ後のページング・パラメータオブジェクトで巧妙である、と私たちは狸猫换太子ことができるように変換は、同じです。
別の方法がありますが、あなたはその後、pageInfoを作成することができます
public class PageUtils {
public static <Do, Vo> PageInfo<Vo> convertPageInfo(PageInfo<Do> pageInfoDo) {
// 创建Page对象,Page对象继承了ArrayList
Page<Vo> page = new Page<>(pageInfoDo.getPageNum(), pageInfoDo.getPageSize());
page.setTotal(pageInfoDo.getTotal());
page.setStartRow(pageInfoDo.getStartRow());
//... 等等信息,可以按需要挨个设置
return new PageInfo<>(page);
}
}