Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362023
  • 博文数量: 97
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 763
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-12 09:11
文章分类

全部博文(97)

文章存档

2020年(1)

2019年(1)

2018年(4)

2017年(16)

2016年(61)

2015年(1)

2013年(3)

2006年(10)

我的朋友

分类: Java

2016-04-28 10:23:36

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

一、根本原理 

不管是自己实现还是利用插件方式,其根本就是利用:
  •  mysql的   limit   beginrow , size(条数) 
  •  ORACEL的   rownum >= 1   and rownum <= 10(1-10的记录)

二、原生方式 

    •   定义需要继承Page类                
  1. package com.elements.user;

  2. public class Page {
  3.     
  4.     //定义分页的默认大小
  5.     protected static int OnePageSize=5;
  6.     
  7.     //当前页数
  8.     private int nowPage=1;
  9.     
  10.     //开始的行
  11.     private int startRow=0;
  12.     {
  13.         startRow=(nowPage-1)*OnePageSize;
  14.     }
  15.     
  16.     //结束的行
  17.     private int endRow=0;
  18.     {
  19.         endRow=nowPage*OnePageSize;
  20.     }
  21.     
  22.     //总的页数
  23.     private int AllPages=0;

  24.     public int getNowPage() {
  25.         return nowPage;
  26.     }

  27.     public void setNowPage(int nowPage) {
  28.         this.nowPage = nowPage;
  29.         startRow=(nowPage-1)*OnePageSize;
  30.         endRow=nowPage*OnePageSize;
  31.     }
  32. }
    • 定义使用的用户类
      这里继承了我们的Page类                   
  1. package com.elements.user.model;

  2. import java.util.Date;

  3. import com.elements.user.Page;

  4. public class User extends Page{
  5.     /**
  6.      * This field was generated by MyBatis Generator.
  7.      * This field corresponds to the database column user.userId
  8.      *
  9.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  10.      */
  11.     private Integer userid;

  12.     /**
  13.      * This field was generated by MyBatis Generator.
  14.      * This field corresponds to the database column user.UserName
  15.      *
  16.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  17.      */
  18.     private String username;

  19.     /**
  20.      * This field was generated by MyBatis Generator.
  21.      * This field corresponds to the database column user.UserEmail
  22.      *
  23.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  24.      */
  25.     private String useremail;

  26.     /**
  27.      * This field was generated by MyBatis Generator.
  28.      * This field corresponds to the database column user.createTime
  29.      *
  30.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  31.      */
  32.     private String createtime;

  33.     /**
  34.      * This field was generated by MyBatis Generator.
  35.      * This field corresponds to the database column user.updateTime
  36.      *
  37.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  38.      */
  39.     private String updatetime;

  40.     /**
  41.      * This method was generated by MyBatis Generator.
  42.      * This method returns the value of the database column user.userId
  43.      *
  44.      * @return the value of user.userId
  45.      *
  46.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  47.      */
  48.     public Integer getUserid() {
  49.         return userid;
  50.     }

  51.     /**
  52.      * This method was generated by MyBatis Generator.
  53.      * This method sets the value of the database column user.userId
  54.      *
  55.      * @param userid the value for user.userId
  56.      *
  57.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  58.      */
  59.     public void setUserid(Integer userid) {
  60.         this.userid = userid;
  61.     }

  62.     /**
  63.      * This method was generated by MyBatis Generator.
  64.      * This method returns the value of the database column user.UserName
  65.      *
  66.      * @return the value of user.UserName
  67.      *
  68.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  69.      */
  70.     public String getUsername() {
  71.         return username;
  72.     }

  73.     /**
  74.      * This method was generated by MyBatis Generator.
  75.      * This method sets the value of the database column user.UserName
  76.      *
  77.      * @param username the value for user.UserName
  78.      *
  79.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  80.      */
  81.     public void setUsername(String username) {
  82.         this.username = username == null ? null : username.trim();
  83.     }

  84.     /**
  85.      * This method was generated by MyBatis Generator.
  86.      * This method returns the value of the database column user.UserEmail
  87.      *
  88.      * @return the value of user.UserEmail
  89.      *
  90.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  91.      */
  92.     public String getUseremail() {
  93.         return useremail;
  94.     }

  95.     /**
  96.      * This method was generated by MyBatis Generator.
  97.      * This method sets the value of the database column user.UserEmail
  98.      *
  99.      * @param useremail the value for user.UserEmail
  100.      *
  101.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  102.      */
  103.     public void setUseremail(String useremail) {
  104.         this.useremail = useremail == null ? null : useremail.trim();
  105.     }

  106.     /**
  107.      * This method was generated by MyBatis Generator.
  108.      * This method returns the value of the database column user.createTime
  109.      *
  110.      * @return the value of user.createTime
  111.      *
  112.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  113.      */
  114.     public String getCreatetime() {
  115.         return createtime;
  116.     }

  117.     /**
  118.      * This method was generated by MyBatis Generator.
  119.      * This method sets the value of the database column user.createTime
  120.      *
  121.      * @param createtime the value for user.createTime
  122.      *
  123.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  124.      */
  125.     public void setCreatetime(String createtime) {
  126.         this.createtime = createtime ;
  127.     }

  128.     /**
  129.      * This method was generated by MyBatis Generator.
  130.      * This method returns the value of the database column user.updateTime
  131.      *
  132.      * @return the value of user.updateTime
  133.      *
  134.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  135.      */
  136.     public String getUpdatetime() {
  137.         return updatetime;
  138.     }

  139.     /**
  140.      * This method was generated by MyBatis Generator.
  141.      * This method sets the value of the database column user.updateTime
  142.      *
  143.      * @param updatetime the value for user.updateTime
  144.      *
  145.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  146.      */
  147.     public void setUpdatetime(String updatetime) {
  148.         this.updatetime = updatetime;
  149.     }

  150.     @Override
  151.     public String toString() {
  152.         return "User [userid=" + userid + ", username=" + username
  153.                 + ", useremail=" + useremail + ", createtime=" + createtime
  154.                 + ", updatetime=" + updatetime + "]\n";
  155.     }
  156.     
  157.     
  158. }

    • mapper配置的XML类     
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.elements.user.dao.UserMapper" >

  4.  
  5.   <resultMap id="BaseResultMap" type="com.elements.user.model.User" >
  6.     <id column="userId" property="userid" jdbcType="INTEGER" />
  7.     <result column="UserName" property="username" jdbcType="VARCHAR" />
  8.     <result column="UserEmail" property="useremail" jdbcType="VARCHAR" />
  9.     <result column="createTime" property="createtime" jdbcType="VARCHAR" />
  10.     <result column="updateTime" property="updatetime" jdbcType="VARCHAR" />
  11.   </resultMap>
  12.   
  13.   
  14.   <sql id="Base_Column_List" >
  15.     userId, UserName, UserEmail, createTime, updateTime
  16.   </sql>
  17.   
  18.   <select id="selectUserPage" parameterType="com.elements.user.model.User" resultMap="BaseResultMap">
  19.     select
  20.     <include refid="Base_Column_List" />
  21.     from user
  22.      limit #{startRow},#{OnePageSize}
  23.   </select>
  24.   
  25. </mapper>


  26.   
  1. package com.elements.user;

  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.List;

  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9. import org.junit.Test;
  10. import com.elements.user.dao.UserMapper;
  11. import com.elements.user.model.User;

  12. public class TestUserPage {

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

  19.         SqlSession session = sqlSessionFactory.openSession();
  20.         try {
  21.  
  22.             
  23.             UserMapper user = (UserMapper) session
  24.                     .getMapper(UserMapper.class);
  25.  
  26.             User u=new User();
  27.             List<User> list =user.selectUserPage(u);
  28.             
  29.             System.out.println("-----page1-------");
  30.             System.out.println(list);
  31.             
  32.             u.setNowPage(2);
  33.             list =user.selectUserPage(u);
  34.             
  35.             System.out.println("-----page2-------");
  36.             System.out.println(list);

  37.         } finally {
  38.             session.close();
  39.         }

  40.     }

  41. }                

  • 输出结果  

    1. -----page1-------
    2. [User [userid=1, username=火柴盒, useremail=likewindy, createtime=null, updatetime=null]
    3. , User [userid=7, username=aaaaa, useremail=aaaaa, createtime=null, updatetime=null]
    4. , User [userid=8, username=5, useremail=6, createtime=null, updatetime=null]
    5. , User [userid=9, username=5, useremail=6, createtime=null, updatetime=null]
    6. , User [userid=10, username=5, useremail=6, createtime=null, updatetime=null]
    7. ]
    8. -----page2-------
    9. [User [userid=11, username=5, useremail=6, createtime=null, updatetime=null]
    10. , User [userid=12, username=5, useremail=6, createtime=null, updatetime=null]
    11. , User [userid=13, username=5, useremail=6, createtime=null, updatetime=null]
    12. , User [userid=14, username=5, useremail=6, createtime=null, updatetime=null]
    13. , User [userid=15, username=5, useremail=6, createtime=null, updatetime=null]
    14. ]

  项目代码地址: http://pan.baidu.com/s/1jINH3LW  
   缺点:当使用复合查询(链接表查询的时候就需要你手工对结果集进行限制 ),而这就是插件可以做到的事情。                                                                          
 

阅读(1826) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~