com.tmsps.ne4spring.utils.generator.BeanGenerator Maven / Gradle / Ivy
package com.tmsps.ne4spring.utils.generator;
import java.util.Map;
import org.springframework.util.StringUtils;
/**
*======================================================
* @author zhangwei [email protected]
*------------------------------------------------------
* BeanGenerator BEAN生成器
*
*======================================================
*/
public class BeanGenerator {
// package
protected String packageTemplate = "package %s;\r\n\r\n";
// import not Map
protected String importNotMapTemplate = "import com.tmsps.ne4spring.annotation.NotMap;\r\n";
//import PK
protected String importPKTemplate = "import com.tmsps.ne4spring.annotation.PK;\r\n";
//import table
protected String importTableTemplate = "import com.tmsps.ne4spring.annotation.Table;\r\n";
//import model
protected String importModelTemplate = "import com.tmsps.ne4spring.orm.model.DataModel;\r\n";
//注释
protected String commentTemplate = "\r\n/**\r\n * Generated by Ne4Spring \r\n*/\r\n\r\n";
//table注解
protected String classTableTemplate = "@Table(TableName=\"%s\")\r\n";
//头部
protected String classDefineTemplate = "public class %s extends DataModel {\r\n";
//序列化标识
protected String serialVersionUIDTemplate = "\r\n\t@NotMap\r\n\tprivate static final long serialVersionUID = 1L;\r\n";
//PK属性
protected String pkTemplate = "\t@PK\r\n";
//字段属性
protected String attributeTemplate = "\tprivate %s %s;\r\n";
//set字段
protected String setterTemplate = "\tpublic void set%s(%s %s) {%n \t\tthis.%s = %s;\r\n\t}\r\n\r\n";
//get字段
protected String getterTemplate = "\tpublic %s get%s() {%n\t\treturn %s;%n\t}\r\n\r\n";
//endClass
protected String endTemplate = "}";
//******************************************//
public String beanPackageName;
public BeanGenerator(String beanPackageName){
this.beanPackageName = beanPackageName;
}
public void getBeanContent(TableMeta tableMeta) {
StringBuilder sb = new StringBuilder();
//calss pack 部分
sb.append(String.format(this.packageTemplate, beanPackageName));
//引入not map 注解
sb.append(this.importNotMapTemplate);
//是否引入PK注解
if(!tableMeta.idName.equals("")&&tableMeta.idName.length()>0){
sb.append(this.importPKTemplate);
}
sb.append(this.importTableTemplate);
//引入父类model
sb.append(this.importModelTemplate);
//注释
sb.append(this.commentTemplate);
//table注解
sb.append(String.format(this.classTableTemplate, tableMeta.tableName));
//头部
sb.append(String.format(this.classDefineTemplate, tableMeta.tableName));
//序列化
sb.append(this.serialVersionUIDTemplate);
//字段属性
for (Map colum : tableMeta.columInfo) {
String aName = colum.get("columnName");
String aType = colum.get("typeStr");
if(!tableMeta.idName.equals("")&&tableMeta.idName.equals(aName)){
sb.append(this.pkTemplate);
sb.append(String.format(this.attributeTemplate, aType,aName));
}else{
sb.append(String.format(this.attributeTemplate, aType,aName));
}
}
//set
for (Map colum : tableMeta.columInfo) {
String aName = colum.get("columnName");
String aType = colum.get("typeStr");
sb.append(String.format(this.setterTemplate, StringUtils.capitalize(aName),aType,aName,aName,aName));
}
//get
for (Map colum : tableMeta.columInfo) {
String aName = colum.get("columnName");
String aType = colum.get("typeStr");
sb.append(String.format(this.getterTemplate, aType,StringUtils.capitalize(aName),aName));
}
//end
sb.append(this.endTemplate);
tableMeta.beanContent = sb.toString();
}
}