定制MyBatis Generator输出内容

  1. 实体Bean的文件名变更。

package plugin;
import java.util.List;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;


public class BeanClassNamePlugin extends PluginAdapter {
    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }
    @Override
    public void initialized(IntrospectedTable table) {
        super.initialized(table);
        String name = table.getBaseRecordType();
        table.setBaseRecordType(name + "Dto");
    }
}

2.特定接口实现。

package plugin;
import java.util.List;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;


public class BeanImplementationPlugin extends PluginAdapter {
    
    private FullyQualifiedJavaType bean;
    public BeanImplementationPlugin() {
        bean = new FullyQualifiedJavaType(
                "net.kronosjp.jyukutyo.MyInterface");
    }
    
    public boolean validate(List<String> warnings) {
        return true;
    }
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        implementBean(
                topLevelClass,
                introspectedTable.getFullyQualifiedTable());
        return true;
    }
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        implementBean(
                topLevelClass,
                introspectedTable.getFullyQualifiedTable());
        return true;
    }
    public boolean modelRecordWithBLOBsClassGenerated(
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        implementBean(
                topLevelClass,
                introspectedTable.getFullyQualifiedTable());
        return true;
    }
    protected void implementBean(TopLevelClass topLevelClass,
            FullyQualifiedTable table) {
        topLevelClass.addImportedType(bean);
        topLevelClass.addSuperInterface(bean);
    }
    
}

3.Mapper类名变更。

package plugin;
import java.util.List;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
public class MapperClassNamePlugin extends PluginAdapter {
 @Override
 public boolean validate(List<String> warnings) {
  return true;
 }
 @Override
 public void initialized(IntrospectedTable table) {
  super.initialized(table);
  
  String name = table.getMyBatis3JavaMapperType();
  table.setMyBatis3JavaMapperType(name.replaceAll("Mapper$", "Dao"));
 }
}

使用:

  <plugin type="plugin.MapperClassNamePlugin" />
  <plugin type="plugin.DtoExtendedPlugin" />

你可能感兴趣的:(mybatis,generator)