您的位置:首页 > 编程语言 > Java开发

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

2017-11-22 21:41 951 查看
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/*.xmltypeAliasesPackage: com.lming.chcservice.entityconfig-location: classpath:/config/mybatis-config.xmlcheck-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@Slf4jpublic class MobileNavMapperTest {@Autowiredprivate MobileNavMapper mapper;@Testpublic void findAll() throws Exception {List<MobileNav> mobileNavList = mapper.findAll();log.info("导航列表:" + mobileNavList);Assert.assertNotEquals(0, mobileNavList);}@Testpublic 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@Datapublic class MobileNav {/*** 导航Id*/@Idprivate String navId;/*** 导航名称*/private String navName;/*** 跳转url*/private String linkUrl;/*** 图标*/private String icon;/*** 排序*/private Integer sort;}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//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@Slf4jpublic class MobileNavServiceImpl implements MobileNavService{@Autowiredprivate PlatNavRepository platNavRepository;@Autowiredprivate MobileNavRepository repository;@Overridepublic 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: 2Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1774c4e2]2017-11-22 21:39:29.572 [main] INFO  com.lming.chcservice.mapper.MobileNavMapperTest - N0000012017-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 - N0000022017-11-22 21:39:29.572 [main] INFO  com.lming.chcservice.mapper.MobileNavMapperTest - 预约
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息