MyBatis 入门(六)--分页查询(1)

不使用mybatis的RowBounds方式,网络上说这个功能不怎么好了。

一、根本原理

不管是自己实现还是利用插件方式,其根本就是利用:

  • mysql的 limit beginrow , size(条数)
  • ORACEL的 rownum >= 1 and rownum <= 10(1-10的记录)

二、原生方式

  • 定义分页的基类Page类
package com.elements.user;

public class Page {    
    //定义分页的默认大小
    protected static int OnePageSize=5;
        //当前页数
    private int nowPage=1;
        //开始的行
    private int startRow=0;
    {
        startRow=(nowPage-1)*OnePageSize;
    }
        //结束的行
    private int endRow=0;
    {
        endRow=nowPage*OnePageSize;
    }
        //总的页数
    private int AllPages=0;

    public int getNowPage() {
        return nowPage;
    }
    public void setNowPage(int nowPage) {
        this.nowPage = nowPage;
        startRow=(nowPage-1)*OnePageSize;
        endRow=nowPage*OnePageSize;
    }
}
  • 用户类继承分页基类
package com.elements.user.model;

import java.util.Date;

import com.elements.user.Page;

public class User extends Page{   
    private Integer userid;   
    private String username;  
    private String useremail;  
    private String createtime;   
    private String updatetime;

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getUseremail() {
        return useremail;
    }

    public void setUseremail(String useremail) {
        this.useremail = useremail == null ? null : useremail.trim();
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime ;
    }

    public String getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(String updatetime) {
        this.updatetime = updatetime;
    }
    @Override
    public String toString() {
        return "User [userid=" + userid + ", username=" + username
                + ", useremail=" + useremail + ", createtime=" + createtime
                + ", updatetime=" + updatetime + "]\n";
    }


}
  • mapper配置的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.elements.user.dao.UserMapper" >


  <resultMap id="BaseResultMap" type="com.elements.user.model.User" >
    <id column="userId" property="userid" jdbcType="INTEGER" />
    <result column="UserName" property="username" jdbcType="VARCHAR" />
    <result column="UserEmail" property="useremail" jdbcType="VARCHAR" />
    <result column="createTime" property="createtime" jdbcType="VARCHAR" />
    <result column="updateTime" property="updatetime" jdbcType="VARCHAR" />
  </resultMap>


  <sql id="Base_Column_List" >
    userId, UserName, UserEmail, createTime, updateTime
  </sql>

  <select id="selectUserPage" parameterType="com.elements.user.model.User" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user 
     limit #{startRow},#{OnePageSize}
  </select>

</mapper>
  • 测试类
package com.elements.user;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.elements.user.dao.UserMapper;
import com.elements.user.model.User;

public class TestUserPage {

    @Test
    public void TestUserPageSelect() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();
        try {


            UserMapper user = (UserMapper) session
                    .getMapper(UserMapper.class);

            User u=new User();
            List<User> list =user.selectUserPage(u);

            System.out.println("-----page1-------");
            System.out.println(list);

            u.setNowPage(2);
            list =user.selectUserPage(u);

            System.out.println("-----page2-------");
            System.out.println(list);

        } finally {
            session.close();
        }

    }

}                

输出结果

-----page1-------
[User [userid=1, username=火柴盒, useremail=likewindy, createtime=null, updatetime=null]
, User [userid=7, username=aaaaa, useremail=aaaaa, createtime=null, updatetime=null]
, User [userid=8, username=5, useremail=6, createtime=null, updatetime=null]
, User [userid=9, username=5, useremail=6, createtime=null, updatetime=null]
, User [userid=10, username=5, useremail=6, createtime=null, updatetime=null]
]
-----page2-------
[User [userid=11, username=5, useremail=6, createtime=null, updatetime=null]
, User [userid=12, username=5, useremail=6, createtime=null, updatetime=null]
, User [userid=13, username=5, useremail=6, createtime=null, updatetime=null]
, User [userid=14, username=5, useremail=6, createtime=null, updatetime=null]
, User [userid=15, username=5, useremail=6, createtime=null, updatetime=null]
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值