package com.cjlu.util;
import org.apache.commons.lang.StringUtils;
import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.*;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ProjectName: slops-aca
* @Description: mybatis 代码自动生成插件
* @author chenyong chenyong@shulidata.com
* @date 2015/12/8 17:29
*/
public class MybatisPlugin extends PluginAdapter {
/**
* 是否启用字段复合函数查询,默认不启用
*/
private static boolean isOpenFunction = false;
/**
* 是否启用支持如update set 字段=表达式(如:'字段+1')这种场景
*/
private static boolean isOpenExpression = false;
public static void generate() {
String config = MybatisPlugin.class.getClassLoader().getResource("mybatisgenerator/generatorConfig.xml").getFile();
String[] arg = { "-configfile", config, "-overwrite" };
ShellRunner.main(arg);
}
public static void main(String[] args) {
generate();
}
@Override
public boolean sqlMapResultMapWithoutBLOBsElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
if (isOpenFunction == false) {
return true;
}
for (IntrospectedColumn introspectedColumn : introspectedColumns) {
XmlElement xmlElement = new XmlElement("result");
Attribute attributeColumn = new Attribute("column", "max_" + introspectedColumn.getActualColumnName());
Attribute attributeProPerty = new Attribute("property",
"max" + StringUtils.capitalize(introspectedColumn.getJavaProperty()));
Attribute attributeJdbcType = new Attribute("jdbcType", "BIGINT");
xmlElement.addAttribute(attributeColumn);
xmlElement.addAttribute(attributeProPerty);
xmlElement.addAttribute(attributeJdbcType);
XmlElement xmlElementMinId = new XmlElement("result");
Attribute attributeColumnMinId = new Attribute("column", "min_" + introspectedColumn.getActualColumnName());
Attribute attributeProPertyMinId = new Attribute("property",
"min" + StringUtils.capitalize(introspectedColumn.getJavaProperty()));
Attribute attributeJdbcTypeMinId = new Attribute("jdbcType", "BIGINT");
xmlElementMinId.addAttribute(attributeColumnMinId);
xmlElementMinId.addAttribute(attributeProPertyMinId);
xmlElementMinId.addAttribute(attributeJdbcTypeMinId);
XmlElement xmlElementAvgId = new XmlElement("result");
Attribute attributeColumnAvgId = new Attribute("column", "avg_" + introspectedColumn.getActualColumnName());
Attribute attributeProPertyAvgId = new Attribute("property",
"avg" + StringUtils.capitalize(introspectedColumn.getJavaProperty()));
Attribute attributeJdbcTypeAvgId = new Attribute("jdbcType", "BIGINT");
xmlElementAvgId.addAttribute(attributeColumnAvgId);
xmlElementAvgId.addAttribute(attributeProPertyAvgId);
xmlElementAvgId.addAttribute(attributeJdbcTypeAvgId);
XmlElement xmlElementCountId = new XmlElement("result");
Attribute attributeColumnCountId = new Attribute("column",
"count_" + introspectedColumn.getActualColumnName());
Attribute attributeProPertyCountId = new Attribute("property",
"count" + StringUtils.capitalize(introspectedColumn.getJavaProperty()));
Attribute attributeJdbcTypeCountId = new Attribute("jdbcType", "BIGINT");
xmlElementCountId.addAttribute(attributeColumnCountId);
xmlElementCountId.addAttribute(attributeProPertyCountId);
xmlElementCountId.addAttribute(attributeJdbcTypeCountId);
XmlElement xmlElementSumId = new XmlElement("result");
Attribute attributeColumnSumId = new Attribute("column", "sum_" + introspectedColumn.getActualColumnName());
Attribute attributeProPertySumId = new Attribute("property",
"sum" + StringUtils.capitalize(introspectedColumn.getJavaProperty()));
Attribute attributeJdbcTypeSumId = new Attribute("jdbcType", introspectedColumn.getJdbcTypeName());
xmlElementSumId.addAttribute(attributeColumnSumId);
xmlElementSumId.addAttribute(attributeProPertySumId);
xmlElementSumId.addAttribute(attributeJdbcTypeSumId);
element.getElements().add(1, xmlElement);
element.getElements().add(2, xmlElementMinId);
element.getElements().add(3, xmlElementAvgId);
element.getElements().add(4, xmlElementCountId);
element.getElements().add(5, xmlElementSumId);
}
return true;
}
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
/**
* mybaties自动生成mapper的时候是追加操作,但是我们平常都是删除重新生成, 所以需要扩展删除原来的mapper让mybaties再生成一个
**/
List<GeneratedXmlFile> generatedXmlFiles = introspectedTable.getGeneratedXmlFiles();
for (GeneratedFile generatedFile : generatedXmlFiles) {
generatedFile.getFormattedContent();
File project = new File(generatedFile.getTargetProject() + generatedFile.getTargetPackage());
File file = new File(project, generatedFile.getFileName());
if (file.exists()) {
file.delete();
}
}
addDomainExampleClassFields(topLevelClass, introspectedTable);
return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
}
@Override
public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
// Attribute attribute1 = new Attribute("useGeneratedKeys", "true");
Attribute attribute2 = new Attribute("keyProperty", "id");
// element.addAttribute(attribute1);
element.addAttribute(attribute2);
return true;
}
@Override
public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
// Attribute attribute1 = new Attribute("useGeneratedKeys", "true");
Attribute attribute2 = new Attribute("keyProperty", "id");
// element.addAttribute(attribute1);
element.addAttribute(attribute2);
return true;
}
@Override
public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
return true;
}
@Override
public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, Interface interfaze,
IntrospectedTable introspectedTable) {
method.getParameters().get(0).addAnnotation("@Param(\"record\")");
return super.clientUpdateByPrimaryKeySelectiveMethodGenerated(method, interfaze, introspectedTable);
}
@Override
public boolean sqlMapDeleteByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
generateSqlMapDelete(element, introsp