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 - 预约
相关文章推荐
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- Mybatis分页插件 - PageHelper与springBoot的整合
- spring-boot 集成mybatis的分页插件PageHelper和Generator
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- springboot如何集成mybatis的pagehelper分页插件
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法
- SpringBoot中使用MyBatis,使用PageHelper进行分页查询以及碰到的错误
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页
- SpringBoot集成分页插件PageHelper
- SpringBoot+Mybatis+PageHelper简化分页实现
- SpringBoot分页插件PageHelper
- Maven+Spring + Mybatis 使用 PageHelper 插件分页