SpringBoot Mybatis PageHelper分頁插件的兩種用法(一)

1、PageHelper 4.x 版本

  

1、pom.xml

<!--mybatis分頁插件-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</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<MobileNav> mobileNavList = mapper.findAll();
        log.info("導航列表:" + mobileNavList);
        Assert.assertNotEquals(0, mobileNavList);
    }

    @Test
    public void findByPage() {
        PageHelper.startPage(1, 2);
        List<MobileNav> mobileNavList = mapper.findAll();
        for(int i=0;i<mobileNavList.size();i++)
        {
            log.info(mobileNavList.get(i).getNavId());
            log.info(mobileNavList.get(i).getNavName());
        }
        System.out.print(mobileNavList);
        PageInfo<MobileNav> 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<MobileNav> 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" ?>
<!DOCTYPE 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<MobileNav> 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<MobileNav> getNavByPlatType(String platType) {

        List<PlatNav> PlatNavList = platNavRepository.findByPlatType(platType);
        if(CollectionUtils.isEmpty(PlatNavList)){
            log.error("【導航菜單】- APP類型菜單未配置,platType={}",platType);
            return null;
        }

        List<String> navIdList =new ArrayList<String>();
        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 - 預約


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章