When using PageHelper plug-page, how to add objects and attributes to convert

First, plug presentations

PageHelper is for pagination plug Mybaits to support any complex single-table, multi-table paging.

Second, the basic usage

In springboot example, arranged in two, one is the traditional, introduction of dependence, write configuration class; application.yml is to use configuration.

The first

1. dependence introduction

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.1.6</version>
</dependency>

2. Configure Plug

/**
 * @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;
    }
}

The second

1. dependence introduction

<!-- 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. Configure Plug

pagehelper:
  offset-as-page-num: true
  row-bounds-with-count: true
  page-size-zero: true
  reasonable: true
  auto-dialect: mysql

Paging demonstration

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;
    }

Third, the object conversion

As described above demonstrates, the direct dispensing tab object list with the database mapping entity. But in the development process often have to convert the object, the object is to convert DO DTO or VO, add or remove some properties.

We can do this:

/**
 * @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;
    }
}

Here is a detailed, step 3, first create an object by PageInfo original list, in order to get to those parameters paging. Before I tried to take it for granted Dolist first converted to Volist, then create pageInfo objects, do so, and can not get to the pagination information.

In fact, can do this, it is clever at the paging parameter object after paging parameters of the original object, and the conversion is the same, so we can 狸猫换太子.

There is another way, you can then create a pageInfo Objects, original pageInfo Parameter copy the past, to achieve the same purpose, stickers, for reference.

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

Guess you like

Origin www.cnblogs.com/hanstrovsky/p/12527022.html
Recommended