mybatis reverse engineering can not be paged then try pagination plug it!

table of Contents

1 pagination process

2 pagination plug PageHelper

2.1 Use

3 page test

4 Test results:


1 pagination process

Reverse engineering the generated code does not support pagination process, meaning if you want to be paged need to write mapper, so you lose the reverse engineering. In order to improve development efficiency can use pagination plug mybatis of PageHelper.

2 pagination plug PageHelper

Mybatis pagination plug - PageHelper Description

If you are using Mybatis, we recommend trying the pagination plug-in, this must be the most convenient to use the pagination plugin.

The plug-in currently supports Oracle, Mysql, MariaDB, SQLite, Hsqldb, PostgreSQL six kinds of database page.

2.1 Use

The first step: PageHelper dependent jar package is added to the project. The code official for reverse engineering support for the poor, using pagehelper-fix in Resources.

Step Two: Configure plug-in Mybatis interceptors in xml configuration:

<?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>

 

Step 2: Use the code

1, set the pagination information:

    //获取第1页,10条内容,默认查询总数count

    PageHelper.startPage(1, 10);


    //紧跟着的第一个select方法会被分页

List<Country> list = countryMapper.selectIf(1);

2, taken paging information

//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>,

Page<Country> listCountry = (Page<Country>)list;

listCountry.getTotal();

3, the second paging information Methods

//获取第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 page test

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

	}

}

4 Test results:

Published 237 original articles · won praise 20 · views 20000 +

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/105196840