MyBatis MySQL Oracle 分页插件

本贴最后更新于 4467 天前,其中的信息可能已经时移俗易

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

 

/**
 * 分页对象.
 * 
 * @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 &gt; 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 &gt; 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(&quot;begin=&quot;).append(begin).append(&quot;, end=&quot;) .append(end).append(&quot;, length=&quot;).append(length).append(&quot;, totalRecords=&quot;).append( totalRecords).append(&quot;, pageNo=&quot;).append(pageNo).append(&quot;, pageCount=&quot;) .append(pageCount); return builder.toString(); }

}

 

/**
 * 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, &quot;page&quot;); return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } @Override public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { XmlElement parentElement = document.getRootElement(); // 产生分页语句前半部分 XmlElement paginationPrefixElement = new XmlElement(&quot;sql&quot;); paginationPrefixElement.addAttribute(new Attribute(&quot;id&quot;, &quot;OracleDialectPrefix&quot;)); XmlElement pageStart = new XmlElement(&quot;if&quot;); pageStart.addAttribute(new Attribute(&quot;test&quot;, &quot;page != null&quot;)); pageStart.addElement(new TextElement( &quot;select * from ( select row_.*, rownum rownum_ from ( &quot;)); paginationPrefixElement.addElement(pageStart); parentElement.addElement(paginationPrefixElement); // 产生分页语句后半部分 XmlElement paginationSuffixElement = new XmlElement(&quot;sql&quot;); paginationSuffixElement.addAttribute(new Attribute(&quot;id&quot;, &quot;OracleDialectSuffix&quot;)); XmlElement pageEnd = new XmlElement(&quot;if&quot;); pageEnd.addAttribute(new Attribute(&quot;test&quot;, &quot;page != null&quot;)); pageEnd .addElement(new TextElement( &quot;&lt;![CDATA[ ) row_ where rownum &lt;= #{page.end} ) where rownum_ &gt; #{page.begin} ]]&gt;&quot;)); paginationSuffixElement.addElement(pageEnd); parentElement.addElement(paginationSuffixElement); return super.sqlMapDocumentGenerated(document, introspectedTable); } @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement pageStart = new XmlElement(&quot;include&quot;); //$NON-NLS-1$ pageStart.addAttribute(new Attribute(&quot;refid&quot;, &quot;OracleDialectPrefix&quot;)); element.getElements().add(0, pageStart); XmlElement isNotNullElement = new XmlElement(&quot;include&quot;); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute(&quot;refid&quot;, &quot;OracleDialectSuffix&quot;)); 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( &quot;com.yuanxin.framework.mybatis.Page&quot;)); CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(new FullyQualifiedJavaType(&quot;com.yuanxin.framework.mybatis.Page&quot;)); 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(&quot;set&quot; + camel); method.addParameter(new Parameter(new FullyQualifiedJavaType( &quot;com.yuanxin.framework.mybatis.Page&quot;), name)); method.addBodyLine(&quot;this.&quot; + name + &quot;=&quot; + name + &quot;;&quot;); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(new FullyQualifiedJavaType(&quot;com.yuanxin.framework.mybatis.Page&quot;)); method.setName(&quot;get&quot; + camel); method.addBodyLine(&quot;return &quot; + name + &quot;;&quot;); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } /** * This plugin is always valid - no properties are required */ @Override public boolean validate(List&lt;String&gt; warnings) { return true; }

}

 

/**
 * 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, &quot;page&quot;); return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement page = new XmlElement(&quot;if&quot;); page.addAttribute(new Attribute(&quot;test&quot;, &quot;page != null&quot;)); page.addElement(new TextElement(&quot;limit #{page.begin} , #{page.length}&quot;)); 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( &quot;com.yuanxin.framework.mybatis.Page&quot;)); CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(new FullyQualifiedJavaType(&quot;com.yuanxin.framework.mybatis.Page&quot;)); 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(&quot;set&quot; + camel); method.addParameter(new Parameter(new FullyQualifiedJavaType( &quot;com.yuanxin.framework.mybatis.Page&quot;), name)); method.addBodyLine(&quot;this.&quot; + name + &quot;=&quot; + name + &quot;;&quot;); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(new FullyQualifiedJavaType(&quot;com.yuanxin.framework.mybatis.Page&quot;)); method.setName(&quot;get&quot; + camel); method.addBodyLine(&quot;return &quot; + name + &quot;;&quot;); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } /** * This plugin is always valid - no properties are required */ public boolean validate(List&lt;String&gt; warnings) { return true; }

}

 

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

 

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3201 引用 • 8217 回帖
  • MyBatis

    MyBatis 本是 Apache 软件基金会 的一个开源项目 iBatis,2010 年这个项目由 Apache 软件基金会迁移到了 google code,并且改名为 MyBatis ,2013 年 11 月再次迁移到了 GitHub。

    173 引用 • 414 回帖 • 365 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • BAE

    百度应用引擎(Baidu App Engine)提供了 PHP、Java、Python 的执行环境,以及云存储、消息服务、云数据库等全面的云服务。它可以让开发者实现自动地部署和管理应用,并且提供动态扩容和负载均衡的运行环境,让开发者不用考虑高成本的运维工作,只需专注于业务逻辑,大大降低了开发者学习和迁移的成本。

    19 引用 • 75 回帖 • 680 关注
  • Dubbo

    Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是 [阿里巴巴] SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

    60 引用 • 82 回帖 • 614 关注
  • WebComponents

    Web Components 是 W3C 定义的标准,它给了前端开发者扩展浏览器标签的能力,可以方便地定制可复用组件,更好的进行模块化开发,解放了前端开发者的生产力。

    1 引用 • 12 关注
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 684 关注
  • 黑曜石

    黑曜石是一款强大的知识库工具,支持本地 Markdown 文件编辑,支持双向链接和关系图。

    A second brain, for you, forever.

    24 引用 • 246 回帖
  • 互联网

    互联网(Internet),又称网际网络,或音译因特网、英特网。互联网始于 1969 年美国的阿帕网,是网络与网络之间所串连成的庞大网络,这些网络以一组通用的协议相连,形成逻辑上的单一巨大国际网络。

    98 引用 • 367 回帖
  • Facebook

    Facebook 是一个联系朋友的社交工具。大家可以通过它和朋友、同事、同学以及周围的人保持互动交流,分享无限上传的图片,发布链接和视频,更可以增进对朋友的了解。

    4 引用 • 15 回帖 • 443 关注
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 378 关注
  • GAE

    Google App Engine(GAE)是 Google 管理的数据中心中用于 WEB 应用程序的开发和托管的平台。2008 年 4 月 发布第一个测试版本。目前支持 Python、Java 和 Go 开发部署。全球已有数十万的开发者在其上开发了众多的应用。

    14 引用 • 42 回帖 • 824 关注
  • 自由行
  • CodeMirror
    2 引用 • 17 回帖 • 172 关注
  • 房星科技

    房星网,我们不和没有钱的程序员谈理想,我们要让程序员又有理想又有钱。我们有雄厚的房地产行业线下资源,遍布昆明全城的 100 家门店、四千地产经纪人是我们坚实的后盾。

    6 引用 • 141 回帖 • 610 关注
  • jQuery

    jQuery 是一套跨浏览器的 JavaScript 库,强化 HTML 与 JavaScript 之间的操作。由 John Resig 在 2006 年 1 月的 BarCamp NYC 上释出第一个版本。全球约有 28% 的网站使用 jQuery,是非常受欢迎的 JavaScript 库。

    63 引用 • 134 回帖 • 735 关注
  • 博客

    记录并分享人生的经历。

    273 引用 • 2389 回帖
  • 设计模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

    201 引用 • 120 回帖 • 1 关注
  • Anytype
    3 引用 • 31 回帖 • 28 关注
  • Hadoop

    Hadoop 是由 Apache 基金会所开发的一个分布式系统基础架构。用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。

    93 引用 • 122 回帖 • 616 关注
  • App

    App(应用程序,Application 的缩写)一般指手机软件。

    91 引用 • 384 回帖
  • WebClipper

    Web Clipper 是一款浏览器剪藏扩展,它可以帮助你把网页内容剪藏到本地。

    3 引用 • 9 回帖
  • TensorFlow

    TensorFlow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。

    20 引用 • 19 回帖 • 2 关注
  • RemNote
    2 引用 • 16 回帖 • 26 关注
  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    26290 引用 • 109281 回帖
  • Q&A

    提问之前请先看《提问的智慧》,好的问题比好的答案更有价值。

    10112 引用 • 45917 回帖 • 63 关注
  • Swift

    Swift 是苹果于 2014 年 WWDC(苹果开发者大会)发布的开发语言,可与 Objective-C 共同运行于 Mac OS 和 iOS 平台,用于搭建基于苹果平台的应用程序。

    34 引用 • 37 回帖 • 559 关注
  • LaTeX

    LaTeX(音译“拉泰赫”)是一种基于 ΤΕΧ 的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在 20 世纪 80 年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由 TeX 所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。

    12 引用 • 59 回帖
  • OneDrive
    2 引用 • 4 关注
  • 游戏

    沉迷游戏伤身,强撸灰飞烟灭。

    187 引用 • 832 回帖