利用freemark生成实体类

1.需求场景

数据报表大量实体类生成,模板化,降低维护难度。

2.项目环境

普通java项目 freemark

3.实现步骤

(1)导入freemark jar包(使用版本FreeMarker_2.3.23)

(2)构建模板文件,比如 entity层

entity.ftl

package ${entity.javaPackage};
 
/**
 * 
 * TODO ${entity.javaDoc}
 * @author admin
 *
 */
public class ${entity.className}
{
    /********** 属性 ***********/
<#list entity.properties as property>
    private ${property.javaType} ${property.propertyName};//${property.propertyComment}
</#list>
    /********** get/set ***********/
<#list entity.properties as property>
    public ${property.javaType} get${property.propertyName?cap_first}() {
        return ${property.propertyName};
    }
 
    public void set${property.propertyName?cap_first}(${property.javaType} ${property.propertyName}) {
        this.${property.propertyName} = ${property.propertyName};
    }
</#list>
}

(3)生成java文件帮助类

public class AutoUtil {
/***
 * 
* @Title: createEntity 
* @Description: TODO 创建实体类
* @param propertyList 字段集合
* @param packageName 包全路径名称
* @param className 类名
* @param javaDoc 类注释
* @param path  文件生成路径
 */
public static void createEntity(Configuration cfg, List<Property> propertyList,String packageName,
                                   String className,String javaDoc,String path){
	 Map<String, Object> root = new HashMap<String, Object>();
	 try{ 
	 Template template = cfg.getTemplate("entity.ftl");
     Entity user = new Entity();
     user.setJavaPackage(packageName); // 创建包名
     user.setClassName(className);  // 创建类名
     user.setJavaDoc(javaDoc);  // 类注释
     // 将属性集合添加到实体对象中
     user.setProperties(propertyList);
     // 创建.java类文件
     File outDirFile = new File(path);
     if(!outDirFile.exists()){
         outDirFile.mkdir();
     }
     File javaFile = toJavaFilename(outDirFile, user.getJavaPackage(), user.getClassName());
     //数据
     root.put("entity", user);
     //写文件
     write2File(javaFile, template, root);
	}catch(Exception e){
		e.printStackTrace();
	}
}

/***
 * 
* @Title: write2File 
* @Description: TODO  将数据写入文件
* @param javaFile 文件对象
* @param template  模板对象
* @param root 数据
 */
public static void write2File(File javaFile, Template template,Map<String,Object> root){
	try{
	Writer javaWriter = new FileWriter(javaFile);
    template.process(root, javaWriter);
    javaWriter.flush();
    System.out.println("文件生成路径:" + javaFile.getCanonicalPath());
    javaWriter.close();
    
    
    // 输出到Console控制台
    //Writer out = new OutputStreamWriter(System.out);
   // template.process(root, out);
   // out.flush();
   // out.close();
	}catch(Exception e){
		e.printStackTrace();
	}
}

/**
 * 创建.java文件所在路径 和 返回.java文件File对象
 * @param outDirFile 生成文件路径
 * @param javaPackage java包名
 * @param javaClassName java类名
 * @return
 */
public static File toJavaFilename(File outDirFile, String javaPackage, String javaClassName) {
    String packageSubPath = javaPackage.replace('.', '/');
    File packagePath = new File(outDirFile, packageSubPath);
    File file = new File(packagePath, javaClassName + ".java");
    if(!packagePath.exists()){
        packagePath.mkdirs();
    }
    return file;
}

/**
 * 
* @Title: getTableProperty2Bean 
* @Description: TODO  生成 Bean
* @param field 字段
* @param column 列名
* @return
 */
public static List<Property> getTableProperty2Bean(List<String> field,List<String> column){
	List<Property> propertyList = new ArrayList<Property>();
	for (int i = 0; i < field.size(); i++) {
		Property  p = new Property("String",field.get(i),column.get(i));
		propertyList.add(p);
	}
	return  propertyList;
}



/**
 * 
* @Title: array2List 
* @Description: TODO 数组转list
* @param array
 */
public static List<String> array2List(String array[]){
	List<String> list = new ArrayList<String>();
	for (int i = 0; i < array.length; i++) {
		list.add(array[i]);
	}
	return list;
}

}

(4)测试类

package com.dx.main;
public class Test {
	public static void main(String[] args) {
		Configuration cfg = new Configuration();
		try {
			// 加载模板路径
			cfg.setDirectoryForTemplateLoading(new File("./template"));
			cfg.setObjectWrapper(new DefaultObjectWrapper());
			String path="项目绝对路径";
		        
			String col ="日期,列名1,列名2";
			String fd="time,col1,col2";
			String field[]=fd.split(",");
			String column[] = col.split(",");
			List<String> fieldarr =AutoUtil.array2List(field); 
			List<String> columnarr =AutoUtil.array2List(column);
			//参数信息
			String packageSuffix=".最后一级包名";
			String beanName="TestBean";
			String comment="注释";
			String beanPackage="com.dx.bean"+packageSuffix;
			//生成java bean
			List<Property> propertyList = AutoUtil.getTableProperty2Bean(fieldarr, columnarr);
			//生成文件
			AutoUtil.createEntity(cfg, propertyList,beanPackage, beanName, comment,path);
			 
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	
	 
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值