SpringBoot Mybatis PageHelper分页插件的两种用法(一)

1、PageHelper 4.x 版本

  

1、pom.xml


<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelperartifactId>
    <version>4.1.6version>
dependency>

2、application.yml
mybatis:
  mapper-locations: classpath:mapper/*.xml
  typeAliasesPackage: com.lming.chcservice.entity
  config-location: classpath:/config/mybatis-config.xml
  check-config-location: true

3、测试类
package com.lming.chcservice.mapper;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lming.chcservice.entity.MobileNav;
import com.lming.chcservice.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MobileNavMapperTest {
    @Autowired
    private MobileNavMapper mapper;

    @Test
    public void findAll() throws Exception {
        List mobileNavList = mapper.findAll();
        log.info("导航列表:" + mobileNavList);
        Assert.assertNotEquals(0, mobileNavList);
    }

    @Test
    public void findByPage() {
        PageHelper.startPage(1, 2);
        List mobileNavList = mapper.findAll();
        for(int i=0;ilog.info(mobileNavList.get(i).getNavId());
            log.info(mobileNavList.get(i).getNavName());
        }
        System.out.print(mobileNavList);
        PageInfo pageInfo = new PageInfo<>(mobileNavList);
        log.info("共{}页,当前页{},下一页{},总记录{}",pageInfo.getPages(),pageInfo.getNavigatePages(),pageInfo.getNextPage(),pageInfo.getTotal());
    }

}
 
  
 
  
package com.lming.chcservice.mapper;

import com.lming.chcservice.entity.MobileNav;

import java.util.List;

public interface MobileNavMapper {

    public List findAll();
}

 
  
 
  
 
  
package com.lming.chcservice.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@Data
public class MobileNav {

    /**
     * 导航Id
     */
    @Id
    private String navId;
    /**
     * 导航名称
     */
    private String navName;
    /**
     * 跳转url
     */
    private String linkUrl;
    /**
     * 图标
     */
    private String icon;
    /**
     * 排序
     */
    private Integer sort;

}

xml version="1.0" encoding="UTF-8" ?>
 mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lming.chcservice.mapper.MobileNavMapper">

    <resultMap id="navResultMap" type="MobileNav">
        <result property="navId" column="nav_id" javaType="java.lang.String"/>
        <result property="navName" column="nav_name" javaType="java.lang.String"/>
        <result property="linkUrl" column="link_url" javaType="java.lang.String"/>
        <result property="icon" column="icon" javaType="java.lang.String"/>
        <result property="sort" column="sort" javaType="java.lang.Integer"/>
    resultMap>

    <select id="findAll" resultMap="navResultMap">
        select * from mobile_nav
    select>
mapper>

package com.lming.chcservice.service;

import com.lming.chcservice.entity.MobileNav;

import java.util.List;

public interface MobileNavService {


    public List getNavByPlatType(String platType);


}
 
  
 
  
package com.lming.chcservice.service.impl;

import com.lming.chcservice.dao.MobileNavRepository;
import com.lming.chcservice.dao.PlatNavRepository;
import com.lming.chcservice.entity.MobileNav;
import com.lming.chcservice.entity.PlatNav;
import com.lming.chcservice.service.MobileNavService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class MobileNavServiceImpl implements MobileNavService{

    @Autowired
    private PlatNavRepository platNavRepository;


    @Autowired
    private MobileNavRepository repository;

    @Override
    public List getNavByPlatType(String platType) {

        List PlatNavList = platNavRepository.findByPlatType(platType);
        if(CollectionUtils.isEmpty(PlatNavList)){
            log.error("【导航菜单】- APP类型菜单未配置,platType={}",platType);
            return null;
        }

        List navIdList =new ArrayList();
        for(PlatNav platNav:PlatNavList){
            navIdList.add(platNav.getNavId());
        }

        return repository.findByNavIdIn(navIdList);

    }

}
 
  
==>  Preparing: SELECT count(0) FROM mobile_nav 
==> Parameters: 
<==    Columns: count(0)
<==        Row: 4
<==      Total: 1
==>  Preparing: select * from mobile_nav limit ?,? 
==> Parameters: 0(Integer), 2(Integer)
<==    Columns: nav_id, nav_name, link_url, icon, sort
<==        Row: N000001, 首页, /index, icon-home, 1
<==        Row: N000002, 预约, /doctor, icon-bubbles4, 2
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1774c4e2]
2017-11-22 21:39:29.572 [main] INFO  com.lming.chcservice.mapper.MobileNavMapperTest - N000001
2017-11-22 21:39:29.572 [main] INFO  com.lming.chcservice.mapper.MobileNavMapperTest - 首页
2017-11-22 21:39:29.572 [main] INFO  com.lming.chcservice.mapper.MobileNavMapperTest - N000002
2017-11-22 21:39:29.572 [main] INFO  com.lming.chcservice.mapper.MobileNavMapperTest - 预约


你可能感兴趣的:(Java,SpringBoot)