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

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

There is a newer version: 1.0.13
Show newest version
package net.sourceforge.jweb.mybatis.generator.plugins;
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * 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.
 */
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

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

/**
 * Adds "Builder" as inner-class to the generated model class. 
Using builder's fluent interface model objects can * be created in more efficient and intuitive way.

Example configuration:
*
 * <generatorConfiguration>
 *  <context ...>
 *
 *      <plugin type="net.sourceforge.jweb.mybatis.generator.plugins.ModelBuilderPlugin"/>
 *      ...
 *
 *  </context>
 * </generatorConfiguration>
 * 
*


Alternative you can specify custom class name for the generated builder class and exclude specific * classes from generation process using regular expression.
*
 * <generatorConfiguration>
 *  <context ...>
 *
 *      <plugin type="net.sourceforge.jweb.mybatis.generator.plugins.ModelBuilderPlugin">
 *          <property name="builderClassName" value="MyBuilder" />
 *          <property name="excludeClassNamesRegexp" value="com.mycompany.*My.*Class" />
 *      <plugin>
 *      ...
 *
 *  </context>
 * </generatorConfiguration>
 * 
*

Properties:
  • builderClassName (optional) : the name of the method to * generate. Default: Builder
  • excludeClassNamesRegexp (optional): * classes to exclude from generation as regular expression. Default: none
* */ public class ModelBuilderPlugin extends PluginAdapter { private Config config; /** * {@inheritDoc} */ public boolean validate(List warnings) { this.initConfig(); return true; } /** * {@inheritDoc} */ @Override public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (config.shouldExclude(topLevelClass.getType())) return true; List fields = topLevelClass.getFields(); InnerClass innerClass = new InnerClass(config.builderClassName); innerClass.setVisibility(JavaVisibility.PUBLIC); innerClass.setStatic(true); context.getCommentGenerator().addClassComment(innerClass, introspectedTable); Field f = new Field("obj", topLevelClass.getType()); f.setVisibility(JavaVisibility.PRIVATE); innerClass.addField(f); Method constructor = new Method(config.builderClassName); constructor.setVisibility(JavaVisibility.PUBLIC); constructor.setConstructor(true); constructor.addBodyLine(new StringBuilder("this.obj = new ") .append(topLevelClass.getType().getShortName()).append("();").toString()); innerClass.addMethod(constructor); for (Field field : fields) { if (field.isStatic()) continue; Method method = new Method(field.getName()); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(innerClass.getType()); method.addParameter(new Parameter(field.getType(), field.getName())); method.addBodyLine(new StringBuilder().append("obj.").append(field.getName()) .append(" = ").append(field.getName()).append(";").toString()); method.addBodyLine(new StringBuilder().append("return this;").toString()); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); innerClass.addMethod(method); } Method build = new Method("build"); build.setReturnType(topLevelClass.getType()); build.setVisibility(JavaVisibility.PUBLIC); build.addBodyLine("return this.obj;"); context.getCommentGenerator().addGeneralMethodComment(build, introspectedTable); innerClass.addMethod(build); topLevelClass.addInnerClass(innerClass); return true; } private void initConfig() { if (this.config == null) this.config = new Config(getProperties()); } private static final class Config extends BasePluginConfig { private static final String builderClassNameKey = "builderClassName"; private String builderClassName; private Config(Properties props) { super(props); this.builderClassName = props.getProperty(builderClassNameKey, "Builder"); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy