MyBatis 的 MySQL、Oracle 分页插件,使用相同的分页接口。
/** * 分页对象. * * @author <a href="mailto:[email protected]">Liang Ding</a> * @version 1.0.1.0, Oct 6, 2012 */ public final class Page implements Serializable {1/** 2 * 默认的序列化版本 id. 3 */ 4private static final long serialVersionUID = 1L; 5/** 6 * 分页查询开始记录位置. 7 */ 8private int begin; 9/** 10 * 分页查看下结束位置. 11 */ 12private int end; 13/** 14 * 每页显示记录数. 15 */ 16private int length = 20; 17/** 18 * 查询结果总记录数. 19 */ 20private int totalRecords; 21/** 22 * 当前页码. 23 */ 24private int pageNo; 25/** 26 * 总共页数. 27 */ 28private int pageCount; 29 30public Page() { 31} 32 33/** 34 * 构造函数. 35 * 36 * @param begin 37 * @param length 38 */ 39public Page(int begin, int length) { 40 this.begin = begin; 41 this.length = length; 42 this.end = this.begin + this.length; 43 this.pageNo = (int) Math.floor((this.begin * 1.0d) / this.length) + 1; 44} 45 46/** 47 * @param begin 48 * @param length 49 * @param count 50 */ 51public Page(int begin, int length, int totalRecords) { 52 this(begin, length); 53 this.totalRecords = totalRecords; 54} 55 56/** 57 * 设置页数,自动计算数据范围. 58 * 59 * @param pageNo 60 */ 61public Page(int pageNo) { 62 this.pageNo = pageNo; 63 pageNo = pageNo > 0 ? pageNo : 1; 64 this.begin = this.length * (pageNo - 1); 65 this.end = this.length * pageNo; 66} 67 68/** 69 * @return the begin 70 */ 71public int getBegin() { 72 return begin; 73} 74 75/** 76 * @return the end 77 */ 78public int getEnd() { 79 return end; 80} 81 82/** 83 * @param end 84 * the end to set 85 */ 86public void setEnd(int end) { 87 this.end = end; 88} 89 90/** 91 * @param begin 92 * the begin to set 93 */ 94public void setBegin(int begin) { 95 this.begin = begin; 96 if (this.length != 0) { 97 this.pageNo = (int) Math.floor((this.begin * 1.0d) / this.length) + 1; 98 } 99} 100 101/** 102 * @return the length 103 */ 104public int getLength() { 105 return length; 106} 107 108/** 109 * @param length 110 * the length to set 111 */ 112public void setLength(int length) { 113 this.length = length; 114 if (this.begin != 0) { 115 this.pageNo = (int) Math.floor((this.begin * 1.0d) / this.length) + 1; 116 } 117} 118 119/** 120 * @return the totalRecords 121 */ 122public int getTotalRecords() { 123 return totalRecords; 124} 125 126/** 127 * @param totalRecords 128 * the totalRecords to set 129 */ 130public void setTotalRecords(int totalRecords) { 131 this.totalRecords = totalRecords; 132 this.pageCount = (int) Math.floor((this.totalRecords * 1.0d) / this.length); 133 if (this.totalRecords % this.length != 0) { 134 this.pageCount++; 135 } 136} 137 138/** 139 * @return the pageNo 140 */ 141public int getPageNo() { 142 return pageNo; 143} 144 145/** 146 * @param pageNo 147 * the pageNo to set 148 */ 149public void setPageNo(int pageNo) { 150 this.pageNo = pageNo; 151 pageNo = pageNo > 0 ? pageNo : 1; 152 this.begin = this.length * (pageNo - 1); 153 this.end = this.length * pageNo; 154} 155 156/** 157 * @return the pageCount 158 */ 159public int getPageCount() { 160 if (pageCount == 0) { 161 return 1; 162 } 163 return pageCount; 164} 165 166/** 167 * @param pageCount 168 * the pageCount to set 169 */ 170public void setPageCount(int pageCount) { 171 this.pageCount = pageCount; 172} 173 174@Override 175public String toString() { 176 final StringBuilder builder = new StringBuilder("begin=").append(begin).append(", end=") 177 .append(end).append(", length=").append(length).append(", totalRecords=").append( 178 totalRecords).append(", pageNo=").append(pageNo).append(", pageCount=") 179 .append(pageCount); 180 181 return builder.toString(); 182}
}
/** * Oracle 分页生成插件。 * * @author <a href="mailto:[email protected]">Liang Ding</a> * @version 1.0.0.0, May 31, 2012 */ public class OraclePaginationPlugin extends PluginAdapter {1@Override 2public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, 3 IntrospectedTable introspectedTable) { 4 // add field, getter, setter for limit clause 5 addPage(topLevelClass, introspectedTable, "page"); 6 return super.modelExampleClassGenerated(topLevelClass, introspectedTable); 7} 8 9@Override 10public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { 11 XmlElement parentElement = document.getRootElement(); 12 13 // 产生分页语句前半部分 14 XmlElement paginationPrefixElement = new XmlElement("sql"); 15 paginationPrefixElement.addAttribute(new Attribute("id", "OracleDialectPrefix")); 16 XmlElement pageStart = new XmlElement("if"); 17 pageStart.addAttribute(new Attribute("test", "page != null")); 18 pageStart.addElement(new TextElement( 19 "select * from ( select row_.*, rownum rownum_ from ( ")); 20 paginationPrefixElement.addElement(pageStart); 21 parentElement.addElement(paginationPrefixElement); 22 23 // 产生分页语句后半部分 24 XmlElement paginationSuffixElement = new XmlElement("sql"); 25 paginationSuffixElement.addAttribute(new Attribute("id", "OracleDialectSuffix")); 26 XmlElement pageEnd = new XmlElement("if"); 27 pageEnd.addAttribute(new Attribute("test", "page != null")); 28 pageEnd 29 .addElement(new TextElement( 30 "<![CDATA[ ) row_ where rownum <= #{page.end} ) where rownum_ > #{page.begin} ]]>")); 31 paginationSuffixElement.addElement(pageEnd); 32 parentElement.addElement(paginationSuffixElement); 33 34 return super.sqlMapDocumentGenerated(document, introspectedTable); 35} 36 37@Override 38public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, 39 IntrospectedTable introspectedTable) { 40 41 XmlElement pageStart = new XmlElement("include"); //$NON-NLS-1$ 42 pageStart.addAttribute(new Attribute("refid", "OracleDialectPrefix")); 43 element.getElements().add(0, pageStart); 44 45 XmlElement isNotNullElement = new XmlElement("include"); //$NON-NLS-1$ 46 isNotNullElement.addAttribute(new Attribute("refid", "OracleDialectSuffix")); 47 element.getElements().add(isNotNullElement); 48 49 return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); 50} 51 52/** 53 * @param topLevelClass 54 * @param introspectedTable 55 * @param name 56 */ 57private void addPage(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, 58 String name) { 59 topLevelClass.addImportedType(new FullyQualifiedJavaType( 60 "com.yuanxin.framework.mybatis.Page")); 61 CommentGenerator commentGenerator = context.getCommentGenerator(); 62 Field field = new Field(); 63 field.setVisibility(JavaVisibility.PROTECTED); 64 field.setType(new FullyQualifiedJavaType("com.yuanxin.framework.mybatis.Page")); 65 field.setName(name); 66 commentGenerator.addFieldComment(field, introspectedTable); 67 topLevelClass.addField(field); 68 char c = name.charAt(0); 69 String camel = Character.toUpperCase(c) + name.substring(1); 70 Method method = new Method(); 71 method.setVisibility(JavaVisibility.PUBLIC); 72 method.setName("set" + camel); 73 method.addParameter(new Parameter(new FullyQualifiedJavaType( 74 "com.yuanxin.framework.mybatis.Page"), name)); 75 method.addBodyLine("this." + name + "=" + name + ";"); 76 commentGenerator.addGeneralMethodComment(method, introspectedTable); 77 topLevelClass.addMethod(method); 78 method = new Method(); 79 method.setVisibility(JavaVisibility.PUBLIC); 80 method.setReturnType(new FullyQualifiedJavaType("com.yuanxin.framework.mybatis.Page")); 81 method.setName("get" + camel); 82 method.addBodyLine("return " + name + ";"); 83 commentGenerator.addGeneralMethodComment(method, introspectedTable); 84 topLevelClass.addMethod(method); 85} 86 87/** 88 * This plugin is always valid - no properties are required 89 */ 90@Override 91public boolean validate(List<String> warnings) { 92 return true; 93}
}
/** * MySQL 分页生成插件。 * * @author <a href="mailto:[email protected]">Liang Ding</a> * @version 1.0.0.1, Oct 10, 2012 */ public final class MySQLPaginationPlugin extends PluginAdapter {1@Override 2public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, 3 IntrospectedTable introspectedTable) { 4 // add field, getter, setter for limit clause 5 addPage(topLevelClass, introspectedTable, "page"); 6 return super.modelExampleClassGenerated(topLevelClass, introspectedTable); 7} 8 9@Override 10public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, 11 IntrospectedTable introspectedTable) { 12 XmlElement page = new XmlElement("if"); 13 page.addAttribute(new Attribute("test", "page != null")); 14 page.addElement(new TextElement("limit #{page.begin} , #{page.length}")); 15 element.addElement(page); 16 17 return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); 18} 19 20/** 21 * @param topLevelClass 22 * @param introspectedTable 23 * @param name 24 */ 25private void addPage(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, 26 String name) { 27 topLevelClass.addImportedType(new FullyQualifiedJavaType( 28 "com.yuanxin.framework.mybatis.Page")); 29 CommentGenerator commentGenerator = context.getCommentGenerator(); 30 Field field = new Field(); 31 field.setVisibility(JavaVisibility.PROTECTED); 32 field.setType(new FullyQualifiedJavaType("com.yuanxin.framework.mybatis.Page")); 33 field.setName(name); 34 commentGenerator.addFieldComment(field, introspectedTable); 35 topLevelClass.addField(field); 36 char c = name.charAt(0); 37 String camel = Character.toUpperCase(c) + name.substring(1); 38 Method method = new Method(); 39 method.setVisibility(JavaVisibility.PUBLIC); 40 method.setName("set" + camel); 41 method.addParameter(new Parameter(new FullyQualifiedJavaType( 42 "com.yuanxin.framework.mybatis.Page"), name)); 43 method.addBodyLine("this." + name + "=" + name + ";"); 44 commentGenerator.addGeneralMethodComment(method, introspectedTable); 45 topLevelClass.addMethod(method); 46 method = new Method(); 47 method.setVisibility(JavaVisibility.PUBLIC); 48 method.setReturnType(new FullyQualifiedJavaType("com.yuanxin.framework.mybatis.Page")); 49 method.setName("get" + camel); 50 method.addBodyLine("return " + name + ";"); 51 commentGenerator.addGeneralMethodComment(method, introspectedTable); 52 topLevelClass.addMethod(method); 53} 54 55/** 56 * This plugin is always valid - no properties are required 57 */ 58public boolean validate(List<String> warnings) { 59 return true; 60}
}
使用时在 generatorConfig.xml 中配置对应的插件即可,最终,在生成的 Criteria 中就会存在 Page 字段,用于设置分页。