
date.yetao.maven.all.mybatis.PlusVerNoPlugin Maven / Gradle / Ivy
/*
* Copyright 2017 yetao <[email protected]>.
*
* 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 date.yetao.maven.all.mybatis;
import date.yetao.maven.all.util.CodeNameTools;
import date.yetao.maven.all.util.LogUtils;
import date.yetao.maven.all.util.StringTools;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.List;
import java.util.Properties;
/**
* 增加plusVerNo方法的插件
* 属性verNo:数据版本号,数据库中
* 属性verNoInJava:数据版本号,类中属性
* 属性logLevel:日志级别
*
* @author yetao <[email protected]>
*/
public class PlusVerNoPlugin extends PluginAdapter {
/**
* 标记版本号字段
*/
private static final String VER_NO = "ver_no";
/**
* 数据库中版本号字段的名称
*/
private String verNo;
/**
* 类中版本号字段的名称
*/
private String verNoInJava;
public PlusVerNoPlugin() {
super();
}
@Override
public boolean validate(List warnings) {
// this plugin is always valid
return true;
}
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
// 加载配置文件
verNo = (String) properties.get("verNo");
verNo = StringTools.hasText(verNo)? verNo.trim():VER_NO;
verNoInJava = (String) properties.get("verNoInJava");
verNoInJava = StringTools.hasText(verNoInJava)? verNoInJava.trim():CodeNameTools.toFirstLowerCamelCase(verNo);
// 加载配置文件
String logLevel = (String) properties.get("logLevel");
if (logLevel != null) {
switch (logLevel.toLowerCase()) {
case "debug":
LogUtils.setLevel(LogUtils.LEVEL_DEBUG);
break;
case "info":
LogUtils.setLevel(LogUtils.LEVEL_INFO);
break;
case "warn":
LogUtils.setLevel(LogUtils.LEVEL_WARN);
break;
case "error":
LogUtils.setLevel(LogUtils.LEVEL_ERROR);
break;
}
}
}
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
// 判断是否需要添加deleteUseFlag方法
List allColumns = introspectedTable.getAllColumns();
if (allColumns == null || allColumns.isEmpty()) {
return true;
}
// 判断接口是否为null
if (interfaze == null) {
return true;
}
for (IntrospectedColumn col : allColumns) {
if (verNo.equalsIgnoreCase(col.getActualColumnName())) {
// 找到了就添加方法
Method method = new Method();
method.setName("plus"+StringTools.toUpperCaseFirstOne(verNoInJava));
method.setReturnType(new FullyQualifiedJavaType("int"));
List primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
IntrospectedColumn priCol = primaryKeyColumns.get(0);
Parameter para = new Parameter(priCol.getFullyQualifiedJavaType(), priCol.getJavaProperty());
method.addParameter(para);
// 添加注释
method.addJavaDocLine("/**");
method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
method.addJavaDocLine(" * This method corresponds to the database table "+introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
method.addAnnotation(" *");
method.addAnnotation(" * @mbg.generated");
method.addAnnotation(" */");
interfaze.addMethod(method);
return true;
}
}
return true;
}
/**
* 增加deleteUseFlag的Mapper
*
*
* @param document
* @param introspectedTable
* @return
*/
@Override
public boolean sqlMapDocumentGenerated(Document document,
IntrospectedTable introspectedTable) {
// 判断是否需要添加deleteUseFlag方法
List allColumns = introspectedTable.getAllColumns();
if (allColumns == null || allColumns.isEmpty()) {
return true;
}
for (IntrospectedColumn col : allColumns) {
if (verNo.equalsIgnoreCase(col.getActualColumnName())) {
// 找到了就添加Mapper
XmlElement root = document.getRootElement();
XmlElement plus = new XmlElement("update");
plus.addAttribute(new Attribute("id", "plus"+StringTools.toUpperCaseFirstOne(verNoInJava)));
List primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
IntrospectedColumn priCol = primaryKeyColumns.get(0);
String colName = priCol.getActualColumnName();
plus.addAttribute(new Attribute("parameterType", priCol.getFullyQualifiedJavaType().getFullyQualifiedName()));
plus.addElement(new TextElement(""));
plus.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
plus.addElement(new TextElement("set " + verNo + " = " + verNo + "+1"));
plus.addElement(new TextElement("where " + colName + " = #{" + priCol.getJavaProperty() + ",jdbcType=" + priCol.getJdbcTypeName() + "}"));
root.addElement(plus);
return true;
}
}
return true;
}
}