当前位置 : 主页 > 编程语言 > java >

MyBatis-generator-limit-plugin 增加分页查询语句

来源:互联网 收集:自由互联 发布时间:2021-06-28
generatorConfig.xml MyBatisGeneratorUtils.java package com.school.zhibotest.util;import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.
generatorConfig.xml
 



 
	
   
    
    
    
     
    
    
    
    
     
    
    
    
     
     
     
     
    
    
    
     
     
    
    
    
     
     
    
    
    
    
    
    
    
    
    
  

 
MyBatisGeneratorUtils.java
package com.school.zhibotest.util;

import java.io.File;    
import java.io.IOException;    
import java.sql.SQLException;    
import java.util.ArrayList;    
import java.util.List;    

import org.mybatis.generator.api.MyBatisGenerator;    
import org.mybatis.generator.config.Configuration;    
import org.mybatis.generator.config.xml.ConfigurationParser;    
import org.mybatis.generator.exception.InvalidConfigurationException;    
import org.mybatis.generator.exception.XMLParserException;    
import org.mybatis.generator.internal.DefaultShellCallback;    

/**
 * 反向生成工具类
 * @author qiaoliang
 *
 */
public class MyBatisGeneratorUtils {    

    public static void main(String[] args) {    
        try {    
            List
 
   warnings = new ArrayList
  
   (); boolean overwrite = true; File configFile = new File(MyBatisGeneratorUtils.class.getResource("/generatorConfig.xml").getFile()); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); System.err.println("---------请刷新!!!-----------"); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
  
 
MySQLLimitPlugin.java
package com.school.zhibotest.util;


import java.util.List;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

/**
 * 

【】【2017年11月30日 上午9:49:02】

* @author QL * */ public class MySQLLimitPlugin extends PluginAdapter { @Override public boolean validate(List list) { return true; } /** * 为每个Example类添加limitStart和limitEnd属性已经set、get方法 */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); Field limit = new Field(); limit.setName("limitStart"); limit.setVisibility(JavaVisibility.PRIVATE); limit.setType(integerWrapper); topLevelClass.addField(limit); Method setLimit = new Method(); setLimit.setVisibility(JavaVisibility.PUBLIC); setLimit.setName("setLimitStart"); setLimit.addParameter(new Parameter(integerWrapper, "limitStart")); setLimit.addBodyLine("this.limitStart = limitStart;"); topLevelClass.addMethod(setLimit); Method getLimit = new Method(); getLimit.setVisibility(JavaVisibility.PUBLIC); getLimit.setReturnType(integerWrapper); getLimit.setName("getLimitStart"); getLimit.addBodyLine("return limitStart;"); topLevelClass.addMethod(getLimit); Field offset = new Field(); offset.setName("limitEnd"); offset.setVisibility(JavaVisibility.PRIVATE); offset.setType(integerWrapper); topLevelClass.addField(offset); Method setOffset = new Method(); setOffset.setVisibility(JavaVisibility.PUBLIC); setOffset.setName("setLimitEnd"); setOffset.addParameter(new Parameter(integerWrapper, "limitEnd")); setOffset.addBodyLine("this.limitEnd = limitEnd;"); topLevelClass.addMethod(setOffset); Method getOffset = new Method(); getOffset.setVisibility(JavaVisibility.PUBLIC); getOffset.setReturnType(integerWrapper); getOffset.setName("getLimitEnd"); getOffset.addBodyLine("return limitEnd;"); topLevelClass.addMethod(getOffset); return true; } /** * 为Mapper.xml的selectByExample添加limit */ @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement ifLimitNotNullElement = new XmlElement("if"); ifLimitNotNullElement.addAttribute(new Attribute("test", "limitEnd != null")); XmlElement ifOffsetNotNullElement = new XmlElement("if"); ifOffsetNotNullElement.addAttribute(new Attribute("test", "limitStart != null")); ifOffsetNotNullElement.addElement(new TextElement("limit ${limitStart}, ${limitEnd}")); ifLimitNotNullElement.addElement(ifOffsetNotNullElement); XmlElement ifOffsetNullElement = new XmlElement("if"); ifOffsetNullElement.addAttribute(new Attribute("test", "limitStart == null")); ifOffsetNullElement.addElement(new TextElement("limit ${limitEnd}")); ifLimitNotNullElement.addElement(ifOffsetNullElement); element.addElement(ifLimitNotNullElement); return true; } }
pom.xml
 
  
  
   4.0.0
  
  
  
   com.school
  
  
  
   zhibo
  
  
  
   war
  
  
  
   0.0.1-SNAPSHOT
  
  
  
   zhibo Maven Webapp
  
  
  
   http://maven.apache.org
  
  
   
   
    4.3
    
  
  
  
  
   
    
    
     xxg-repository
     
    
     http://repo.maven.xxgblog.com/
     
    
  
  
   
    
    
    
     org.webjars.bower
     
    
     jquery
     
    
     3.0.0-alpha1
     
    
    
    
    
     org.apache.httpcomponents
     
    
     httpclient
     
    
     ${httpclient.version}
     
    
    
    
     org.apache.httpcomponents
     
    
     httpmime
     
    
     ${httpclient.version}
     
    
    
    
     org.apache.httpcomponents
     
    
     httpasyncclient
     
    
     4.1
     
    
    
    
     org.json
     
    
     json
     
    
     20140107
     
    
    
    
     junit
     
    
     junit
     
    
     3.8.1
     
    
     test
     
    
    
    
    
     commons-codec
     
    
     commons-codec
     
    
     1.10
     
    
    
    
    
     commons-httpclient
     
    
     commons-httpclient
     
    
     3.1
     
    
    
    
    
     javax.servlet
     
    
     javax.servlet-api
     
    
     3.1.0
     
    
     provided
     
    
    
    
    
     org.mybatis.generator
     
    
     mybatis-generator-core
     
    
     1.3.5
     
    
    
    
     jstl
     
    
     jstl
     
    
     1.2
     
    
    
    
     taglibs
     
    
     standard
     
    
     1.1.2
     
    
    
    
    
    
     mysql
     
    
     mysql-connector-java
     
    
     5.1.37
     
    
     runtime
     
    
    
    
     c3p0
     
    
     c3p0
     
    
     0.9.1.2
     
    
    
    
    
     org.mybatis
     
    
     mybatis
     
    
     3.3.0
     
    
    
    
     org.mybatis
     
    
     mybatis-spring
     
    
     1.2.3
     
    
    
    
    
     taglibs
     
    
     standard
     
    
     1.1.2
     
    
    
    
     jstl
     
    
     jstl
     
    
     1.2
     
    
    
    
     com.fasterxml.jackson.core
     
    
     jackson-databind
     
    
     2.5.4
     
    
    
    
     javax.servlet
     
    
     javax.servlet-api
     
    
     3.1.0
     
    
    
    
    
    
     org.springframework
     
    
     spring-core
     
    
     4.1.7.RELEASE
     
    
    
    
     org.springframework
     
    
     spring-beans
     
    
     4.1.7.RELEASE
     
    
    
    
     org.springframework
     
    
     spring-context
     
    
     4.1.7.RELEASE
     
    
    
    
    
     org.springframework
     
    
     spring-jdbc
     
    
     4.1.7.RELEASE
     
    
    
    
     org.springframework
     
    
     spring-tx
     
    
     4.1.7.RELEASE
     
    
    
    
    
     org.springframework
     
    
     spring-web
     
    
     4.1.7.RELEASE
     
    
    
    
     org.springframework
     
    
     spring-webmvc
     
    
     4.1.7.RELEASE
     
    
    
    
    
     org.springframework
     
    
     spring-test
     
    
     4.1.7.RELEASE
     
    
    
    
    
     redis.clients
     
    
     jedis
     
    
     2.7.3
     
    
    
    
     com.dyuproject.protostuff
     
    
     protostuff-core
     
    
     1.0.8
     
    
    
    
     com.dyuproject.protostuff
     
    
     protostuff-runtime
     
    
     1.0.8
     
    
    
    
    
     commons-collections
     
    
     commons-collections
     
    
     3.2
     
    
    
    
     org.bgee.log4jdbc-log4j2
     
    
     log4jdbc-log4j2-jdbc4.1
     
    
     1.16
     
    
    
    
     org.slf4j
     
    
     slf4j-api
     
    
     1.7.13
     
    
    
    
     org.slf4j
     
    
     slf4j-log4j12
     
    
     1.7.13
     
    
    
    
     
    
     org.logback-extensions
     
    
     logback-ext-spring
     
    
     0.1.4
     
    
    
    
    
     org.slf4j
     
    
     slf4j-api
     
    
     1.6.4
     
    
    
    
     org.slf4j
     
    
     jcl-over-slf4j
     
    
     1.6.4
     
    
    
    
    
     ch.qos.logback
     
    
     logback-core
     
    
     1.1.6
     
    
    
    
     ch.qos.logback
     
    
     logback-access
     
    
     1.1.6
     
    
    
    
     ch.qos.logback
     
    
     logback-classic
     
    
     1.1.6
     
    
  
  
   
   
    zhibo
    
    
     
     
      org.mortbay.jetty
      
     
      jetty-maven-plugin
      
     
      8.1.14.v20131031
      
      
      
       30
       
       
       
        /
        
       
       
      
       src/main/resources/jetty-deploy.xml
       
      
     
     
     
     
      org.apache.maven.plugins
      
     
      maven-compiler-plugin
      
     
      3.1
      
      
      1.8 
      
       1.8
       
      
     
     
     
      org.mybatis.generator
      
     
      mybatis-generator-maven-plugin
      
     
      1.3.2
      
      
       
       
        mysql
        
       
        mysql-connector-java
        
       
        5.1.34
        
       
       
       
        com.xxg
        
       
        mybatis-generator-plugin
        
       
        1.0.0
        
       
      
      
      
       true
       
      
     
    
  

 
gistfile1.txt
用法

将该插件添加
 到MyBatis Generator配置文件中。


 
    
   
    ... 
  

 
与Maven一起运行

的pom.xml


 
    
   
   
    xxg-repository
    
   
    http://repo.maven.xxgblog.com/
    
  

 

 
	
   
    
    
     org.mybatis.generator
     
    
     mybatis-generator-maven-plugin
     
    
     1.3.2
     
     
      
      
       mysql
       
      
       mysql-connector-java
       
      
       5.1.34
       
      
      
      
       com.xxg
       
      
       mbg-limit-plugin
       
      
       1.0.0
       
      
     
     
     
      true
      
     
    
  

 
运行mvn mybatis-generator:generator生成java和xml文件。
网友评论