MyBatis MySQL Oracle 分页插件

MyBatis 的 MySQL、Oracle 分页插件,使用相同的分页接口。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
  * 分页对象.
  *
  * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  * @version 1.0.1.0, Oct 6, 2012
  */
public final class Page implements Serializable {
 
     /**
      * 默认的序列化版本 id.
      */
     private static final long serialVersionUID = 1L;
     /**
      * 分页查询开始记录位置.
      */
     private int               begin;
     /**
      * 分页查看下结束位置.
      */
     private int               end;
     /**
      * 每页显示记录数.
      */
     private int               length           = 20 ;
     /**
      * 查询结果总记录数.
      */
     private int               totalRecords;
     /**
      * 当前页码.
      */
     private int               pageNo;
     /**
      * 总共页数.
      */
     private int               pageCount;
 
     public Page() {
     }
 
     /**
      * 构造函数.
      *
      * @param begin
      * @param length
      */
     public Page( int begin, int length) {
         this .begin = begin;
         this .length = length;
         this .end = this .begin + this .length;
         this .pageNo = ( int ) Math.floor(( this .begin * 1 .0d) / this .length) + 1 ;
     }
 
     /**
      * @param begin
      * @param length
      * @param count
      */
     public Page( int begin, int length, int totalRecords) {
         this (begin, length);
         this .totalRecords = totalRecords;
     }
 
     /**
      * 设置页数,自动计算数据范围.
      *
      * @param pageNo
      */
     public Page( int pageNo) {
         this .pageNo = pageNo;
         pageNo = pageNo > 0 ? pageNo : 1 ;
         this .begin = this .length * (pageNo - 1 );
         this .end = this .length * pageNo;
     }
 
     /**
      * @return the begin
      */
     public int getBegin() {
         return begin;
     }
 
     /**
      * @return the end
      */
     public int getEnd() {
         return end;
     }
 
     /**
      * @param end
      *            the end to set
      */
     public void setEnd( int end) {
         this .end = end;
     }
 
     /**
      * @param begin
      *            the begin to set
      */
     public void setBegin( int begin) {
         this .begin = begin;
         if ( this .length != 0 ) {
             this .pageNo = ( int ) Math.floor(( this .begin * 1 .0d) / this .length) + 1 ;
         }
     }
 
     /**
      * @return the length
      */
     public int getLength() {
         return length;
     }
 
     /**
      * @param length
      *            the length to set
      */
     public void setLength( int length) {
         this .length = length;
         if ( this .begin != 0 ) {
             this .pageNo = ( int ) Math.floor(( this .begin * 1 .0d) / this .length) + 1 ;
         }
     }
 
     /**
      * @return the totalRecords
      */
     public int getTotalRecords() {
         return totalRecords;
     }
 
     /**
      * @param totalRecords
      *            the totalRecords to set
      */
     public void setTotalRecords( int totalRecords) {
         this .totalRecords = totalRecords;
         this .pageCount = ( int ) Math.floor(( this .totalRecords * 1 .0d) / this .length);
         if ( this .totalRecords % this .length != 0 ) {
             this .pageCount++;
         }
     }
 
     /**
      * @return the pageNo
      */
     public int getPageNo() {
         return pageNo;
     }
 
     /**
      * @param pageNo
      *            the pageNo to set
      */
     public void setPageNo( int pageNo) {
         this .pageNo = pageNo;
         pageNo = pageNo > 0 ? pageNo : 1 ;
         this .begin = this .length * (pageNo - 1 );
         this .end = this .length * pageNo;
     }
 
     /**
      * @return the pageCount
      */
     public int getPageCount() {
         if (pageCount == 0 ) {
             return 1 ;
         }
         return pageCount;
     }
 
     /**
      * @param pageCount
      *            the pageCount to set
      */
     public void setPageCount( int pageCount) {
         this .pageCount = pageCount;
     }
 
     @Override
     public String toString() {
         final StringBuilder builder = new StringBuilder( "begin=" ).append(begin).append( ", end=" )
                 .append(end).append( ", length=" ).append(length).append( ", totalRecords=" ).append(
                         totalRecords).append( ", pageNo=" ).append(pageNo).append( ", pageCount=" )
                 .append(pageCount);
 
         return builder.toString();
     }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
  * Oracle 分页生成插件。
  *
  * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  * @version 1.0.0.0, May 31, 2012
  */
public class OraclePaginationPlugin extends PluginAdapter {
 
     @Override
     public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
             IntrospectedTable introspectedTable) {
         // add field, getter, setter for limit clause
         addPage(topLevelClass, introspectedTable, "page" );
         return super .modelExampleClassGenerated(topLevelClass, introspectedTable);
     }
 
     @Override
     public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
         XmlElement parentElement = document.getRootElement();
 
         // 产生分页语句前半部分
         XmlElement paginationPrefixElement = new XmlElement( "sql" );
         paginationPrefixElement.addAttribute( new Attribute( "id" , "OracleDialectPrefix" ));
         XmlElement pageStart = new XmlElement( "if" );
         pageStart.addAttribute( new Attribute( "test" , "page != null" ));
         pageStart.addElement( new TextElement(
                 "select * from ( select row_.*, rownum rownum_ from ( " ));
         paginationPrefixElement.addElement(pageStart);
         parentElement.addElement(paginationPrefixElement);
 
         // 产生分页语句后半部分
         XmlElement paginationSuffixElement = new XmlElement( "sql" );
         paginationSuffixElement.addAttribute( new Attribute( "id" , "OracleDialectSuffix" ));
         XmlElement pageEnd = new XmlElement( "if" );
         pageEnd.addAttribute( new Attribute( "test" , "page != null" ));
         pageEnd
                 .addElement( new TextElement(
                         "<![CDATA[ ) row_  where rownum <= #{page.end} ) where rownum_ > #{page.begin}  ]]>" ));
         paginationSuffixElement.addElement(pageEnd);
         parentElement.addElement(paginationSuffixElement);
 
         return super .sqlMapDocumentGenerated(document, introspectedTable);
     }
 
     @Override
     public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
             IntrospectedTable introspectedTable) {
 
         XmlElement pageStart = new XmlElement( "include" ); //$NON-NLS-1$  
         pageStart.addAttribute( new Attribute( "refid" , "OracleDialectPrefix" ));
         element.getElements().add( 0 , pageStart);
 
         XmlElement isNotNullElement = new XmlElement( "include" ); //$NON-NLS-1$  
         isNotNullElement.addAttribute( new Attribute( "refid" , "OracleDialectSuffix" ));
         element.getElements().add(isNotNullElement);
 
         return super .sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
     }
 
     /**
      * @param topLevelClass
      * @param introspectedTable
      * @param name
      */
     private void addPage(TopLevelClass topLevelClass, IntrospectedTable introspectedTable,
             String name) {
         topLevelClass.addImportedType( new FullyQualifiedJavaType(
                 "com.yuanxin.framework.mybatis.Page" ));
         CommentGenerator commentGenerator = context.getCommentGenerator();
         Field field = new Field();
         field.setVisibility(JavaVisibility.PROTECTED);
         field.setType( new FullyQualifiedJavaType( "com.yuanxin.framework.mybatis.Page" ));
         field.setName(name);
         commentGenerator.addFieldComment(field, introspectedTable);
         topLevelClass.addField(field);
         char c = name.charAt( 0 );
         String camel = Character.toUpperCase(c) + name.substring( 1 );
         Method method = new Method();
         method.setVisibility(JavaVisibility.PUBLIC);
         method.setName( "set" + camel);
         method.addParameter( new Parameter( new FullyQualifiedJavaType(
                 "com.yuanxin.framework.mybatis.Page" ), name));
         method.addBodyLine( "this." + name + "=" + name + ";" );
         commentGenerator.addGeneralMethodComment(method, introspectedTable);
         topLevelClass.addMethod(method);
         method = new Method();
         method.setVisibility(JavaVisibility.PUBLIC);
         method.setReturnType( new FullyQualifiedJavaType( "com.yuanxin.framework.mybatis.Page" ));
         method.setName( "get" + camel);
         method.addBodyLine( "return " + name + ";" );
         commentGenerator.addGeneralMethodComment(method, introspectedTable);
         topLevelClass.addMethod(method);
     }
 
     /**
      * This plugin is always valid - no properties are required
      */
     @Override
     public boolean validate(List<String> warnings) {
         return true ;
     }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
  * MySQL 分页生成插件。
  *
  * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  * @version 1.0.0.1, Oct 10, 2012
  */
public final class MySQLPaginationPlugin extends PluginAdapter {
 
     @Override
     public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
             IntrospectedTable introspectedTable) {
         // add field, getter, setter for limit clause
         addPage(topLevelClass, introspectedTable, "page" );
         return super .modelExampleClassGenerated(topLevelClass, introspectedTable);
     }
 
     @Override
     public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
             IntrospectedTable introspectedTable) {
         XmlElement page = new XmlElement( "if" );
         page.addAttribute( new Attribute( "test" , "page != null" ));
         page.addElement( new TextElement( "limit #{page.begin} , #{page.length}" ));
         element.addElement(page);
 
         return super .sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
     }
 
     /**
      * @param topLevelClass
      * @param introspectedTable
      * @param name
      */
     private void addPage(TopLevelClass topLevelClass, IntrospectedTable introspectedTable,
             String name) {
         topLevelClass.addImportedType( new FullyQualifiedJavaType(
                 "com.yuanxin.framework.mybatis.Page" ));
         CommentGenerator commentGenerator = context.getCommentGenerator();
         Field field = new Field();
         field.setVisibility(JavaVisibility.PROTECTED);
         field.setType( new FullyQualifiedJavaType( "com.yuanxin.framework.mybatis.Page" ));
         field.setName(name);
         commentGenerator.addFieldComment(field, introspectedTable);
         topLevelClass.addField(field);
         char c = name.charAt( 0 );
         String camel = Character.toUpperCase(c) + name.substring( 1 );
         Method method = new Method();
         method.setVisibility(JavaVisibility.PUBLIC);
         method.setName( "set" + camel);
         method.addParameter( new Parameter( new FullyQualifiedJavaType(
                 "com.yuanxin.framework.mybatis.Page" ), name));
         method.addBodyLine( "this." + name + "=" + name + ";" );
         commentGenerator.addGeneralMethodComment(method, introspectedTable);
         topLevelClass.addMethod(method);
         method = new Method();
         method.setVisibility(JavaVisibility.PUBLIC);
         method.setReturnType( new FullyQualifiedJavaType( "com.yuanxin.framework.mybatis.Page" ));
         method.setName( "get" + camel);
         method.addBodyLine( "return " + name + ";" );
         commentGenerator.addGeneralMethodComment(method, introspectedTable);
         topLevelClass.addMethod(method);
     }
 
     /**
      * This plugin is always valid - no properties are required
      */
     public boolean validate(List<String> warnings) {
         return true ;
     }
}

 

使用时在 generatorConfig.xml 中配置对应的插件即可,最终,在生成的 Criteria 中就会存在 Page 字段,用于设置分页。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值