All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.jweb.mybatis.generator.plugins.TableSubstitutionPlugin Maven / Gradle / Ivy

There is a newer version: 1.0.13
Show newest version
package net.sourceforge.jweb.mybatis.generator.plugins;

import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.FullyQualifiedTable;
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.Interface;
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.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.logging.Log;
import org.mybatis.generator.logging.LogFactory;
/**
 * //通过反射修改掉runtimeTableName的名字,使得可以查询表结构一样,但是表是动态的情况,例如table_yyyy_MM表
 * @author [email protected]
 *
 */
public class TableSubstitutionPlugin  extends PluginAdapter {
	private final static Log LOG=LogFactory.getLog(TableSubstitutionPlugin.class);
	private static final String RECUR_MARK = "__SchemaSubstitutionPlugin__recur__mark__";

	public boolean validate(List paramList) {
		return true;
	}

	public void initialized(IntrospectedTable introspectedTable) {
		if(!isPluginEnabled(introspectedTable)){
			return ;
		}
		//通过反射修改掉runtimeTableName的名字 加上${namespace}前缀
		final FullyQualifiedTable fqt = introspectedTable.getFullyQualifiedTable();
		try {
			java.lang.reflect.Field isf=fqt.getClass().getDeclaredField("runtimeTableName");
			isf.setAccessible(true);
			isf.set(fqt, "${namespace}");
			
			java.lang.reflect.Field rsf=fqt.getClass().getDeclaredField("introspectedTableName");
			rsf.setAccessible(true);
			rsf.set(fqt, "${namespace}");
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		//this is one duplicated invocation, the purpose is to trigger calling calculateXmlAttributes, 
		//final to ullyQualifiedTable.getFullyQualifiedTableNameAtRuntime()
		Object mark=introspectedTable.getAttribute(RECUR_MARK);
		if(mark==null){
			introspectedTable.setAttribute(RECUR_MARK, RECUR_MARK);
			introspectedTable.initialize();
		}
	}
	//给方法签名增加参数和annotation
	@Override
	public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
		for(Parameter p:method.getParameters()){
			p.getAnnotations().add("@Param(\""+p.getName()+"\")");
		}
		Parameter p=new Parameter(FullyQualifiedJavaType.getStringInstance(), "namespace");
		p.addAnnotation("@Param(\"namespace\")");
		method.addParameter(p);
		return true;
	}
	//给方法签名增加参数和annotation
	@Override
	public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		for(Parameter p:method.getParameters()){
			p.getAnnotations().add("@Param(\""+p.getName()+"\")");
		}
		Parameter p=new Parameter(FullyQualifiedJavaType.getStringInstance(), "namespace");
		p.addAnnotation("@Param(\"namespace\")");
		method.addParameter(p);
		return true;
	}

	//修改parameterType从具体的pojo变为map
	@Override
	public boolean sqlMapDeleteByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
		List attrs=element.getAttributes();
		for(int i=0;i attrs=element.getAttributes();
		for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy