MyBatis MySQL Oracle 分页插件

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

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 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3203 引用 • 8217 回帖
  • MyBatis

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

    173 引用 • 414 回帖 • 357 关注

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • 微服务

    微服务架构是一种架构模式,它提倡将单一应用划分成一组小的服务。服务之间互相协调,互相配合,为用户提供最终价值。每个服务运行在独立的进程中。服务于服务之间才用轻量级的通信机制互相沟通。每个服务都围绕着具体业务构建,能够被独立的部署。

    96 引用 • 155 回帖 • 1 关注
  • TGIF

    Thank God It's Friday! 感谢老天,总算到星期五啦!

    292 引用 • 4495 回帖 • 664 关注
  • HHKB

    HHKB 是富士通的 Happy Hacking 系列电容键盘。电容键盘即无接点静电电容式键盘(Capacitive Keyboard)。

    5 引用 • 74 回帖 • 523 关注
  • GitLab

    GitLab 是利用 Ruby 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面操作公开或私有项目。

    46 引用 • 72 回帖
  • 周末

    星期六到星期天晚,实行五天工作制后,指每周的最后两天。再过几年可能就是三天了。

    14 引用 • 297 回帖 • 3 关注
  • CodeMirror
    2 引用 • 17 回帖 • 176 关注
  • 黑曜石

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

    A second brain, for you, forever.

    26 引用 • 264 回帖
  • 又拍云

    又拍云是国内领先的 CDN 服务提供商,国家工信部认证通过的“可信云”,乌云众测平台认证的“安全云”,为移动时代的创业者提供新一代的 CDN 加速服务。

    20 引用 • 37 回帖 • 569 关注
  • 深度学习

    深度学习(Deep Learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。

    43 引用 • 44 回帖
  • 链书

    链书(Chainbook)是 B3log 开源社区提供的区块链纸质书交易平台,通过 B3T 实现共享激励与价值链。可将你的闲置书籍上架到链书,我们共同构建这个全新的交易平台,让闲置书籍继续发挥它的价值。

    链书社

    链书目前已经下线,也许以后还有计划重制上线。

    14 引用 • 257 回帖 • 4 关注
  • CSDN

    CSDN (Chinese Software Developer Network) 创立于 1999 年,是中国的 IT 社区和服务平台,为中国的软件开发者和 IT 从业者提供知识传播、职业发展、软件开发等全生命周期服务,满足他们在职业发展中学习及共享知识和信息、建立职业发展社交圈、通过软件开发实现技术商业化等刚性需求。

    14 引用 • 155 回帖
  • Vditor

    Vditor 是一款浏览器端的 Markdown 编辑器,支持所见即所得、即时渲染(类似 Typora)和分屏预览模式。它使用 TypeScript 实现,支持原生 JavaScript、Vue、React 和 Angular。

    377 引用 • 1865 回帖 • 2 关注
  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    346 引用 • 760 回帖
  • FreeMarker

    FreeMarker 是一款好用且功能强大的 Java 模版引擎。

    23 引用 • 20 回帖 • 472 关注
  • Sphinx

    Sphinx 是一个基于 SQL 的全文检索引擎,可以结合 MySQL、PostgreSQL 做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。

    1 引用 • 223 关注
  • Webswing

    Webswing 是一个能将任何 Swing 应用通过纯 HTML5 运行在浏览器中的 Web 服务器,详细介绍请看 将 Java Swing 应用变成 Web 应用

    1 引用 • 15 回帖 • 648 关注
  • 程序员

    程序员是从事程序开发、程序维护的专业人员。

    593 引用 • 3533 回帖
  • Hibernate

    Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。

    39 引用 • 103 回帖 • 726 关注
  • OAuth

    OAuth 协议为用户资源的授权提供了一个安全的、开放而又简易的标准。与以往的授权方式不同之处是 oAuth 的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用户的用户名与密码就可以申请获得该用户资源的授权,因此 oAuth 是安全的。oAuth 是 Open Authorization 的简写。

    36 引用 • 103 回帖 • 36 关注
  • 安装

    你若安好,便是晴天。

    132 引用 • 1184 回帖 • 1 关注
  • Unity

    Unity 是由 Unity Technologies 开发的一个让开发者可以轻松创建诸如 2D、3D 多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

    25 引用 • 7 回帖 • 119 关注
  • 百度

    百度(Nasdaq:BIDU)是全球最大的中文搜索引擎、最大的中文网站。2000 年 1 月由李彦宏创立于北京中关村,致力于向人们提供“简单,可依赖”的信息获取方式。“百度”二字源于中国宋朝词人辛弃疾的《青玉案·元夕》词句“众里寻他千百度”,象征着百度对中文信息检索技术的执著追求。

    63 引用 • 785 回帖 • 66 关注
  • IPFS

    IPFS(InterPlanetary File System,星际文件系统)是永久的、去中心化保存和共享文件的方法,这是一种内容可寻址、版本化、点对点超媒体的分布式协议。请浏览 IPFS 入门笔记了解更多细节。

    20 引用 • 245 回帖 • 238 关注
  • Solo

    Solo 是一款小而美的开源博客系统,专为程序员设计。Solo 有着非常活跃的社区,可将文章作为帖子推送到社区,来自社区的回帖将作为博客评论进行联动(具体细节请浏览 B3log 构思 - 分布式社区网络)。

    这是一种全新的网络社区体验,让热爱记录和分享的你不再感到孤单!

    1444 引用 • 10083 回帖 • 504 关注
  • 友情链接

    确认过眼神后的灵魂连接,站在链在!

    24 引用 • 373 回帖 • 1 关注
  • Anytype
    3 引用 • 31 回帖 • 30 关注
  • DNSPod

    DNSPod 建立于 2006 年 3 月份,是一款免费智能 DNS 产品。 DNSPod 可以为同时有电信、网通、教育网服务器的网站提供智能的解析,让电信用户访问电信的服务器,网通的用户访问网通的服务器,教育网的用户访问教育网的服务器,达到互联互通的效果。

    6 引用 • 26 回帖 • 529 关注