当前位置: 代码迷 >> 综合 >> SpringBoot2+Mybatis3+pagehelper分页demo
  详细解决方案

SpringBoot2+Mybatis3+pagehelper分页demo

热度:56   发布时间:2023-12-15 13:17:16.0

两年多没碰spring框架,最近项目刚换了SpringBoot2+Mybatis+Vue前后端分离的架构;对于一些基本的设置做一些记录,这里的分页用到了插件pagehelper,具体步骤如下:

1 pom依赖,pagehelper 配置

1.1 pom添加pagehelper 依赖

 <!-- 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.12</version></dependency>
1.2 pagehelper 配置
设置PageHelper 对象属性,新建class 配置Configuration注解:
package com.bpe.w008.config;import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Properties;@Configuration
public class PageConfiguration {@Beanpublic 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","postgresql");    //配置 数据库的方言properties.setProperty("params","count=countSql");   //pageHelper.setProperties(properties);return pageHelper;}}

当然也可以在property文件和yml中设置

#yml文件
pagehelper:dialect: postgresqlreasonable: truesupport-methods-arguments: true#properties 文件pagehelper.dialect=postgresql
pagehelper.reasonable=true
2 需要查询的实体类
package com.bpe.video.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;/*** Created by Administrator on 2019/7/11.*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("w008_test")
public class W008Test {/*** id*/@TableId(type = IdType.ID_WORKER)private Long id;/*** 创建人*/private String create_by;/*** 创建时间*/@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date create_date;/*** 更新人*/private String update_by;/*** 更新时间*/@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date update_date;private Long formulabaseinfo;private Long chkactiontab;}

对应的表结构

3 Dao层和xml

3.1 dao

package com.bpe.video.mapper;
import java.util.List;
import java.util.Map;/*** <p>* </p>**/
public interface TestMapper  {/**** @return Long*/List<Map<String,Object>> find();}

3.2 对应xml

<?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.bpe.video.mapper.TestMapper"><select id="find" resultType="Map">select id,formulabaseinfo from w008_test ORDER  BY id</select>
</mapper>

4 service 和实现类

4.1 service

package com.bpe.video.service;
import java.util.Map;/***/
public interface ITestService  {Map<String,Object> findPage();
}

4.2 serviceImpl

package com.bpe.video.service.impl;import com.bpe.video.mapper.TestMapper;
import com.bpe.video.service.IR21Service;
import com.bpe.video.service.ITestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TestServiceImpl implements ITestService {@ResourceTestMapper testMapper;@Overridepublic Map<String,Object> findPage(){Map<String,Object>resultMap=new HashMap<>();PageHelper.startPage(1, 3);//第一条开始取数,取三条数据List<Map<String,Object>> list = testMapper.find();PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);resultMap.put("count",pageInfo.getTotal());//获得总数resultMap.put("data",list);return resultMap;}}

其中 PageInfo 是继承自PageSerializable的类,里面有很多可以设置的参数属性,可以自行配置

 

5 测试类测试结果

package com.bpe.video.service.impl;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.Map;@RunWith(SpringRunner.class)
@SpringBootTest
public class TestTest {@Autowiredprivate TestServiceImpl service;@Testpublic void findPage() {System.out.println(("----- selectAll method test ------"));Map<String,Object> resultMap=service.findPage();System.out.println("count:"+resultMap.get("count"));System.out.println("data:"+resultMap.get("data"));}}

输出结果:

----- findPage method test ------
count:5
data:Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=5, pages=2, reasonable=false, pageSizeZero=false}[{formulabaseinfo=1, id=1}, {formulabaseinfo=2, id=2}, {formulabaseinfo=3, id=3}]

可以看出总共有5条记录,查询出了id为1,2,3的三条数据

对比数据库数据:

 

 

  相关解决方案