1. 程式人生 > >Mybatis3分頁, 基於Mybatis Generator外掛生成MYSQL分頁語句

Mybatis3分頁, 基於Mybatis Generator外掛生成MYSQL分頁語句

http://ibatis.apache.org/docs/tools/ibator/reference/pluggingIn.html

Mybatis Generator外掛物理分頁,適用於targetRuntime="MyBatis3"

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


接著在generatorConfig.xml檔案裡引入外掛:
  1. com.fxhx.gamelog.common.plugin.PaginationPlugin  
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
  3. <generatorConfiguration>
  4. <classPathEntrylocation="E:\workspace\mysql-connector-java-5.0.8-bin.jar"/>
  5. <!-- targetRuntime = Ibatis2Java5  -->
  6.   <contextid="context1"targetRuntime="MyBatis3">
  7.     <!-- 這裡引入擴充套件外掛 -->
  8.     <plugintype="com.fxhx.gamelog.common.plugin.PaginationPlugin"/>
  9.     <jdbcConnectiondriverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/p1LogManager?useUnicode=true&characterEncoding=utf8"userId="root"password="123"/>
  10.     <javaModelGeneratortargetPackage="com.fxhx.gamelog.entity"targetProject="LogDataSys/src/main/java"/>
  11.     <sqlMapGeneratortargetPackage="com.fxhx.gamelog.mapper"targetProject="LogDataSys/src/main/java"/>
  12.     <javaClientGeneratortargetPackage="com.fxhx.gamelog.mapper"targetProject="LogDataSys/src/main/java"type="XMLMAPPER"/>
  13.     <tableschema=""tableName="consume_log"domainObjectName="ConsumeLog"/>
  14.   </context>
  15. </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>