package com.hanhanfilm.utils.nigel; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.generator.BaseModelGenerator; import com.jfinal.plugin.activerecord.generator.ColumnMeta; import com.jfinal.plugin.activerecord.generator.TableMeta; /** * https://www.hanhanfilm.com * Created by badouyuren on 2016/4/12. */ public class NigelBaseGenerator extends BaseModelGenerator { protected String classDefineTemplate = "/**%n" + " * Generated by badouyurenon %s.%n" + " */%n" + "@SuppressWarnings(\"serial\")%n" + "public abstract class %s<M extends %s<M>> extends Model<M> implements IBean {%n%n"; protected String privateVariableTemplate = "\tpublic String %s = \"%s\"; %n"; protected String setterTemplate = "\tpublic void %s(%s %s) {%n" + "\t\tset(this.%s, %s);%n" + "\t}%n%n"; protected String getterTemplate = "\tpublic %s %s() {%n" + "\t\treturn get(%s);%n" + "\t}%n%n"; public NigelBaseGenerator(String baseModelPackageName, String baseModelOutputDir) { super(baseModelPackageName, baseModelOutputDir); } @Override protected void genBaseModelContent(TableMeta tableMeta) { StringBuilder ret = new StringBuilder(); genPackage(ret); genImport(ret); genClassDefine(tableMeta, ret); for (ColumnMeta columnMeta : tableMeta.columnMetas) { genPrivateVariable(columnMeta, ret); } ret.append(String.format("%n")); for (ColumnMeta columnMeta : tableMeta.columnMetas) { genSetMethodName(columnMeta, ret); genGetMethodName(columnMeta, ret); } ret.append(String.format("}%n")); tableMeta.baseModelContent = ret.toString(); } @Override protected void genClassDefine(TableMeta tableMeta, StringBuilder ret) { ret.append(String.format(classDefineTemplate, NigelDate.getFullDatetimeStr(), tableMeta.baseModelName, tableMeta.baseModelName)); } /** * private String aaBb = "aa_bb"; * * @param columnMeta * @param ret */ protected void genPrivateVariable(ColumnMeta columnMeta, StringBuilder ret) { String privateVariableName = StrKit.firstCharToLowerCase(columnMeta.attrName); // 如果 privateVariableName 参数名为 java 语言关键字,则添加下划线前缀 "_" privateVariableName = javaKeyword.contains(privateVariableName) ? "_" + privateVariableName : privateVariableName; String privateVariable = String.format(privateVariableTemplate, privateVariableName, columnMeta.name); ret.append(privateVariable); } @Override protected void genSetMethodName(ColumnMeta columnMeta, StringBuilder ret) { String setterMethodName = "set" + StrKit.firstCharToUpperCase(columnMeta.attrName); // 如果 setter 参数名为 java 语言关键字,则添加下划线前缀 "_" String argName = javaKeyword.contains(columnMeta.attrName) ? "_" + columnMeta.attrName : columnMeta.attrName; String setter = String.format(setterTemplate, setterMethodName, columnMeta.javaType, argName, argName, argName); ret.append(setter); } @Override protected void genGetMethodName(ColumnMeta columnMeta, StringBuilder ret) { String privateVariableName = StrKit.firstCharToLowerCase(columnMeta.attrName); // 如果 privateVariableName 参数名为 java 语言关键字,则添加下划线前缀 "_" privateVariableName = javaKeyword.contains(privateVariableName) ? "_" + privateVariableName : privateVariableName; String getterMethodName = "get" + StrKit.firstCharToUpperCase(columnMeta.attrName); String getter = String.format(getterTemplate, columnMeta.javaType, getterMethodName, privateVariableName); ret.append(getter); } }