Skip to content

Instantly share code, notes, and snippets.

@donaldmunro
Created July 18, 2012 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldmunro/3135611 to your computer and use it in GitHub Desktop.
Save donaldmunro/3135611 to your computer and use it in GitHub Desktop.
MyBatis Generator plugin to give query Example classes a common root class.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nonet.mybatis.generator.plugins;
import java.util.List;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
/**
* MyBatis Generator plugin to give query Example classes a common root class.
*
* This plugin accepts one properties:
* <ul>
* <li><tt>exampleRootClass</tt> (required) the fully qualified class name for the base class
* </li>
* For example, to use foo.bar.ExampleBase as a base class:
* <code>
* <generatorConfiguration>
<classPathEntry location="/path/to/MyBatisExampleRootPlugin.jar" />
<context id="context1" >
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="nonet.mybatis.generator.plugins.ExampleClassRoot">
<property name="exampleRootClass" value="foo.bar.ExampleBase" />
</plugin>
.
.
</code>
* To run:
* java -classpath mybatis-generator-core-1.3.1.jar:MyBatisExampleRootPlugin.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml -overwrite -verbose
* @author Donald Munro
*/
public class ExampleClassRoot extends PluginAdapter
//=================================================
{
private String baseClassName;
public ExampleClassRoot() { }
@Override
public boolean validate(List<String> warnings)
//--------------------------------------------
{
baseClassName = properties.getProperty("exampleRootClass");
boolean isValid = stringHasValue(baseClassName);
if (! isValid)
warnings.add("ExampleClassRoot plugin: exampleRootClass property not found");
return isValid;
}
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
//---------------------------------------------------------------------------------------------------------
{
if (baseClassName != null)
{
FullyQualifiedJavaType superClass = new FullyQualifiedJavaType(baseClassName);
topLevelClass.addImportedType(superClass);
topLevelClass.setSuperClass(superClass);
System.out.println("Setting Example class " + topLevelClass.getType().getFullyQualifiedName() +
" super class to " + superClass.getFullyQualifiedName());
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment