分享

Mybatis3分页, 基于Mybatis Generator插件生成分页语句

 凌氏 2014-03-21

    Mybatis Generator插件物理分页,适用于targetRuntime="MyBatis3"

通过继承generator工具的PluginAdapter抽象类来扩展插件,具体参考文档 http://ibatis./docs/tools/ibator/reference/pluggingIn.html



  1. package com.fxhx.gamelog.common.plugin;  
  2. import java.util.List;  
  3.   
  4. import org.mybatis.generator.api.CommentGenerator;  
  5. import org.mybatis.generator.api.IntrospectedTable;  
  6. import org.mybatis.generator.api.PluginAdapter;  
  7. import org.mybatis.generator.api.ShellRunner;  
  8. import org.mybatis.generator.api.dom.java.Field;  
  9. import org.mybatis.generator.api.dom.java.JavaVisibility;  
  10. import org.mybatis.generator.api.dom.java.Method;  
  11. import org.mybatis.generator.api.dom.java.Parameter;  
  12. import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;  
  13. import org.mybatis.generator.api.dom.java.TopLevelClass;  
  14. import org.mybatis.generator.api.dom.xml.Attribute;  
  15. import org.mybatis.generator.api.dom.xml.TextElement;  
  16. import org.mybatis.generator.api.dom.xml.XmlElement;  
  17.   
  18. /** 
  19.  * <P>File name : PaginationPlugin.java </P> 
  20.  * <P>Author : fly </P>  
  21.  * <P>Date : 2013-7-2 上午11:50:45 </P> 
  22.  */  
  23. public class PaginationPlugin extends PluginAdapter {  
  24.     @Override  
  25.     public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,  
  26.             IntrospectedTable introspectedTable) {  
  27.         // add field, getter, setter for limit clause  
  28.         addLimit(topLevelClass, introspectedTable, "limitStart");  
  29.         addLimit(topLevelClass, introspectedTable, "limitEnd");  
  30.         return super.modelExampleClassGenerated(topLevelClass,  
  31.                 introspectedTable);  
  32.     }  
  33.     @Override  
  34.     public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(  
  35.             XmlElement element, IntrospectedTable introspectedTable) {  
  36. //      XmlElement isParameterPresenteElemen = (XmlElement) element  
  37. //              .getElements().get(element.getElements().size() - 1);  
  38.         XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$  
  39.         isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart>=0")); //$NON-NLS-1$ //$NON-NLS-2$  
  40. //      isNotNullElement.addAttribute(new Attribute("compareValue", "0")); //$NON-NLS-1$ //$NON-NLS-2$  
  41.         isNotNullElement.addElement(new TextElement(  
  42.                 "limit #{limitStart} , #{limitEnd}"));  
  43. //      isParameterPresenteElemen.addElement(isNotNullElement);  
  44.         element.addElement(isNotNullElement);  
  45.         return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,  
  46.                 introspectedTable);  
  47.     }  
  48.     private void addLimit(TopLevelClass topLevelClass,  
  49.             IntrospectedTable introspectedTable, String name) {  
  50.         CommentGenerator commentGenerator = context.getCommentGenerator();  
  51.         Field field = new Field();  
  52.         field.setVisibility(JavaVisibility.PROTECTED);  
  53. //      field.setType(FullyQualifiedJavaType.getIntInstance());  
  54.         field.setType(PrimitiveTypeWrapper.getIntegerInstance());  
  55.         field.setName(name);  
  56. //      field.setInitializationString("-1");  
  57.         commentGenerator.addFieldComment(field, introspectedTable);  
  58.         topLevelClass.addField(field);  
  59.         char c = name.charAt(0);  
  60.         String camel = Character.toUpperCase(c) + name.substring(1);  
  61.         Method method = new Method();  
  62.         method.setVisibility(JavaVisibility.PUBLIC);  
  63.         method.setName("set" + camel);  
  64.         method.addParameter(new Parameter(PrimitiveTypeWrapper.getIntegerInstance(), name));  
  65.         method.addBodyLine("this." + name + "=" + name + ";");  
  66.         commentGenerator.addGeneralMethodComment(method, introspectedTable);  
  67.         topLevelClass.addMethod(method);  
  68.         method = new Method();  
  69.         method.setVisibility(JavaVisibility.PUBLIC);  
  70.         method.setReturnType(PrimitiveTypeWrapper.getIntegerInstance());  
  71.         method.setName("get" + camel);  
  72.         method.addBodyLine("return " + name + ";");  
  73.         commentGenerator.addGeneralMethodComment(method, introspectedTable);  
  74.         topLevelClass.addMethod(method);  
  75.     }  
  76.     /** 
  77.      * This plugin is always valid - no properties are required 
  78.      */  
  79.     public boolean validate(List<String> warnings) {  
  80.         return true;  
  81.     }  
  82.   
  83. }  




接着在generatorConfig.xml文件里引入插件:

  1. com.fxhx.gamelog.common.plugin.PaginationPlugin  

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE generatorConfiguration PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN" "http:///dtd/mybatis-generator-config_1_0.dtd" >  
  3. <generatorConfiguration >  
  4. <classPathEntry location="E:\workspace\mysql-connector-java-5.0.8-bin.jar" />   
  5. <!-- targetRuntime = Ibatis2Java5  -->  
  6.   <context id="context1" targetRuntime="MyBatis3" >  
  7.     <!-- 这里引入扩展插件 -->   
  8.     <plugin type="com.fxhx.gamelog.common.plugin.PaginationPlugin" />    
  9.       
  10.     <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/p1LogManager?useUnicode=true&characterEncoding=utf8" userId="root" password="123" />  
  11.     <javaModelGenerator targetPackage="com.fxhx.gamelog.entity" targetProject="LogDataSys/src/main/java" />  
  12.     <sqlMapGenerator targetPackage="com.fxhx.gamelog.mapper" targetProject="LogDataSys/src/main/java" />  
  13.     <javaClientGenerator targetPackage="com.fxhx.gamelog.mapper" targetProject="LogDataSys/src/main/java" type="XMLMAPPER" />  
  14.   
  15.     <table schema="" tableName="consume_log"  domainObjectName="ConsumeLog" />  
  16.   </context>  
  17. </generatorConfiguration>  



此工程需要mybatis-generator-core.jar包

  1. maven:  
  1. <dependency>  
  2.         <groupId>org.mybatis.generator</groupId>  
  3.         <artifactId>mybatis-generator-core</artifactId>  
  4.         <version>1.3.2</version>  
  5.         <type>jar</type>  
  6.         <scope>test</scope>  
  7.  </dependency>  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多