MyBatis MySQL Oracle 分页插件

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

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

    3196 引用 • 8215 回帖
  • MyBatis

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

    173 引用 • 414 回帖 • 367 关注

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • Dubbo

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

    60 引用 • 82 回帖 • 610 关注
  • ReactiveX

    ReactiveX 是一个专注于异步编程与控制可观察数据(或者事件)流的 API。它组合了观察者模式,迭代器模式和函数式编程的优秀思想。

    1 引用 • 2 回帖 • 179 关注
  • DevOps

    DevOps(Development 和 Operations 的组合词)是一组过程、方法与系统的统称,用于促进开发(应用程序/软件工程)、技术运营和质量保障(QA)部门之间的沟通、协作与整合。

    57 引用 • 25 回帖 • 6 关注
  • SpaceVim

    SpaceVim 是一个社区驱动的模块化 vim/neovim 配置集合,以模块的方式组织管理插件以
    及相关配置,为不同的语言开发量身定制了相关的开发模块,该模块提供代码自动补全,
    语法检查、格式化、调试、REPL 等特性。用户仅需载入相关语言的模块即可得到一个开箱
    即用的 Vim-IDE。

    3 引用 • 31 回帖 • 118 关注
  • 负能量

    上帝为你关上了一扇门,然后就去睡觉了....努力不一定能成功,但不努力一定很轻松 (° ー °〃)

    89 引用 • 1243 回帖 • 415 关注
  • 资讯

    资讯是用户因为及时地获得它并利用它而能够在相对短的时间内给自己带来价值的信息,资讯有时效性和地域性。

    56 引用 • 85 回帖
  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1063 引用 • 3455 回帖 • 164 关注
  • AWS
    11 引用 • 28 回帖 • 12 关注
  • 禅道

    禅道是一款国产的开源项目管理软件,她的核心管理思想基于敏捷方法 scrum,内置了产品管理和项目管理,同时又根据国内研发现状补充了测试管理、计划管理、发布管理、文档管理、事务管理等功能,在一个软件中就可以将软件研发中的需求、任务、bug、用例、计划、发布等要素有序的跟踪管理起来,完整地覆盖了项目管理的核心流程。

    6 引用 • 15 回帖 • 30 关注
  • SVN

    SVN 是 Subversion 的简称,是一个开放源代码的版本控制系统,相较于 RCS、CVS,它采用了分支管理系统,它的设计目标就是取代 CVS。

    29 引用 • 98 回帖 • 690 关注
  • Hexo

    Hexo 是一款快速、简洁且高效的博客框架,使用 Node.js 编写。

    22 引用 • 148 回帖 • 12 关注
  • CongSec

    本标签主要用于分享网络空间安全专业的学习笔记

    1 引用 • 1 回帖 • 29 关注
  • Ubuntu

    Ubuntu(友帮拓、优般图、乌班图)是一个以桌面应用为主的 Linux 操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词,意思是“人性”、“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。Ubuntu 的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。

    127 引用 • 169 回帖
  • IDEA

    IDEA 全称 IntelliJ IDEA,是一款 Java 语言开发的集成环境,在业界被公认为最好的 Java 开发工具之一。IDEA 是 JetBrains 公司的产品,这家公司总部位于捷克共和国的首都布拉格,开发人员以严谨著称的东欧程序员为主。

    181 引用 • 400 回帖
  • BND

    BND(Baidu Netdisk Downloader)是一款图形界面的百度网盘不限速下载器,支持 Windows、Linux 和 Mac,详细介绍请看这里

    107 引用 • 1281 回帖 • 35 关注
  • 外包

    有空闲时间是接外包好呢还是学习好呢?

    26 引用 • 233 回帖 • 3 关注
  • jQuery

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

    63 引用 • 134 回帖 • 734 关注
  • 倾城之链
    23 引用 • 66 回帖 • 162 关注
  • 音乐

    你听到信仰的声音了么?

    62 引用 • 512 回帖
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    952 引用 • 944 回帖
  • CloudFoundry

    Cloud Foundry 是 VMware 推出的业界第一个开源 PaaS 云平台,它支持多种框架、语言、运行时环境、云平台及应用服务,使开发人员能够在几秒钟内进行应用程序的部署和扩展,无需担心任何基础架构的问题。

    5 引用 • 18 回帖 • 179 关注
  • ActiveMQ

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

    19 引用 • 13 回帖 • 678 关注
  • Notion

    Notion - The all-in-one workspace for your notes, tasks, wikis, and databases.

    10 引用 • 76 回帖
  • RESTful

    一种软件架构设计风格而不是标准,提供了一组设计原则和约束条件,主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

    30 引用 • 114 回帖 • 9 关注
  • jsoup

    jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址、HTML 文本内容。它提供了一套非常省力的 API,可通过 DOM,CSS 以及类似于 jQuery 的操作方法来取出和操作数据。

    6 引用 • 1 回帖 • 487 关注
  • Q&A

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

    9551 引用 • 43498 回帖 • 102 关注
  • 服务

    提供一个服务绝不仅仅是简单的把硬件和软件累加在一起,它包括了服务的可靠性、服务的标准化、以及对服务的监控、维护、技术支持等。

    41 引用 • 24 回帖 • 3 关注