PageHelper 플러그인 페이지를 사용하는 경우, 어떻게 개체를 추가하고 변환하는 속성에

첫째, 플러그 프리젠 테이션

PageHelper은 복잡한 단일 테이블 멀티 테이블 페이징을 지원하는 페이징 플러그 Mybaits위한 것이다.

둘째, 기본적인 사용법

두 배열 springboot 예에서, 하나는 의존성, 쓰기 구성 클래스의 전통, 소개, 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;
    }
}

두 번째

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를 만들 수 있습니다 객체, 원래 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);
    }
}

추천

출처www.cnblogs.com/hanstrovsky/p/12527022.html