欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis分页和PageHelper插件的使用

程序员文章站 2024-03-24 16:32:40
...

1,加载分页插件 pom.xml

<dependency>
    	<groupId>com.github.pagehelper</groupId>
    	<artifactId>pagehelper</artifactId>
   		<version>5.1.2</version>
	</dependency>

2,在mybatis-config.xml中配置

<plugins>
      <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  </plugins>

3,创建实体类

package cn.wx.pojo;

import java.util.List;

public class PageInfo {
    //每页显示的个数
    private int pageSize;
    //当前的页数
    private int pageNumber;
    //总页数
    private long total;
    //不确定给哪个对象进行分页
    private List<?> list;

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "PageInfo [pageSize=" + pageSize + ", pageNumber=" + pageNumber + ", total=" + total + ", list=" + list
                + "]";
    }

}

4,在SUserMapper.java中这样配置‘

public List<SUser> selByPage();

public long usersCount();

5,在SUserMapper.xml中配置

<select id="usersCount" resultType="long">
     select count(*) from user
  </select>
  
  <select id="selByPage" parameterType="map" resultType="user">
      select * from user limit #{pageStart},#{pageSize}
  </select>

6,SUserService

public PageInfo showpage(int pageSize, int pageNumber);

7,在SUserServiceImpl中配置

public PageInfo showpage(int pageSize, int pageNumber) {
		
		SqlSession sessionn = MybatisUtil.getSession();
		
		PageInfo pageinfo = new PageInfo();
		
		pageinfo.setPageSize(pageSize);
		pageinfo.setPageNumber(pageNumber);
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("pageStart", pageSize*(pageNumber -1));
		map.put("pageSize", pageSize);
		
		List<Object> lists = sessionn.selectList("cn.wx.mapper.UserMapper.selByPage",map);
		
		long count = sessionn.selectOne("cn.wx.mapper.UserMapper.usersCount");
		
		System.out.println(count);
		
		pageinfo.setTotal(count%pageSize == 0 ? count/pageSize:count/pageSize+1);
		
		return pageinfo;
	}

8,测试

UserService userService = new UserServiceImpl();
		
		PageInfo info = userService.showpage(2, 3);
		
		System.out.println(info);

测试结果如下:

mybatis分页和PageHelper插件的使用

以上我们只是把插件配置好了,但是没有使用,现在我们就在用PageHelper插件再测试下

import cn.wx.mapper.SUserMapper;
import cn.wx.pojo.PageInfo;
import cn.wx.pojo.SUser;
import cn.wx.service.UserService;
import cn.wx.service.serviceImpl.UserServiceImpl;
import cn.wx.utils.SqlSessionUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class Test12 {
    @org.junit.Test
            public void testDemo02(){
//        UserService userService = new UserServiceImpl();
//
//        PageInfo info = userService.showpage(2, 3);
//
//        System.out.println(info);
        SqlSession session = SqlSessionUtils.getSqlSession();
        SUserMapper mapper = session.getMapper(SUserMapper.class);
        PageHelper.startPage(3, 2);
        List<SUser> list = mapper.selUser();
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {

            System.out.println(list.get(i));
        }



    }


}

 测试结果如下:

mybatis分页和PageHelper插件的使用

达到了和没有使用插件同样的效果

 

相关标签: java web