SpringBoot2+Mybatis3+pagehelper分页demo

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

1 pom依赖,pagehelper 配置

1.1 pom添加pagehelper 依赖

 
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.12
        
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 {
    @Bean
    public 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: postgresql
  reasonable: true
  support-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;

}

对应的表结构

SpringBoot2+Mybatis3+pagehelper分页demo_第1张图片

3 Dao层和xml

3.1 dao

package com.bpe.video.mapper;
import java.util.List;
import java.util.Map;

/**
 * 

*

* */ public interface TestMapper { /** * * @return Long */ List> find(); }

3.2 对应xml




    

4 service 和实现类

4.1 service

package com.bpe.video.service;
import java.util.Map;

/**
 */
public interface ITestService  {
   Map 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 {
	@Resource
	TestMapper testMapper;
	@Override
	public Map findPage(){
		MapresultMap=new HashMap<>();
		PageHelper.startPage(1, 3);//第一条开始取数,取三条数据
		List> list = testMapper.find();
		PageInfo> pageInfo = new PageInfo>(list);
		resultMap.put("count",pageInfo.getTotal());//获得总数
		resultMap.put("data",list);
		return resultMap;
	}

}

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

SpringBoot2+Mybatis3+pagehelper分页demo_第2张图片

 

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 {
	@Autowired
	private TestServiceImpl service;

	@Test
	public void findPage() {
		System.out.println(("----- selectAll method test ------"));
		Map 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的三条数据

对比数据库数据:

SpringBoot2+Mybatis3+pagehelper分页demo_第3张图片

 

 

你可能感兴趣的:(java)