自动生成工具类
- 路径需要根据自己的本地路径进行修改
package com.jeff.tianti.common.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Dao、Service层代码自动生成工具类
* @author SuperGu
* @since 2015-12-09
*/
public class GenCodeUtil {
//公共部分
private static final String RT_1 = "\r\n";
private static final String RT_2 = RT_1+RT_1;
private static final String BLANK_1 =" ";
//注释部分
private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";
private static final String ANNOTATION_DESC = "@desc ";
private static final String ANNOTATION_DATE = "@date ";
/**
* 创建查询信息封装DTO(用于后台的查询信息封装)
* @param c实体类
* @param commonPackage 基础包:如com.jeff.tianti.info
* @param author 作者
* @param desc 描述
* @throws IOException
*/
public static void createQueryDTO(Class c,String commonPackage,String author) throws IOException{
String cName = c.getName();
String dtoPath = "";
if(author == null || author.equals("")){
author = "Administrator";
}
if(commonPackage != null && !commonPackage.equals("")){
System.out.println("System.getProperty(\"user.dir\")+++++++++"+System.getProperty("user.dir"));
String fileName = System.getProperty("user.dir") + "/tianti-cms/src/main/java/com/jeff/tianti/cms/"/*src/main/java/"*/ + dtoPath+"/dto"
+ "/" + getLastChar(cName) + "QueryDTO.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+commonPackage+".dto"+";"+RT_2+"import com.jeff.tianti.common.dto.CommonQueryDTO;"+RT_2
+"/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR_PARAMTER+ author +RT_1
+BLANK_1+"*"+BLANK_1+ANNOTATION_DESC +getLastChar(cName)+"QueryDTO"+BLANK_1+RT_1
+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1
+"public class " +getLastChar(cName) +"QueryDTO extends CommonQueryDTO{"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}else{
System.out.println("创建"+getLastChar(cName)+"QueryDTO失败,原因是commonPackage为空!");
}
}
/**
* 创建查询信息封装DTO(用于前台的查询信息封装)
* @param c实体类
* @param commonPackage 基础包:如com.jeff.tianti.info
* @param author 作者
* @param desc 描述
* @throws IOException
*/
public static void createFrontQueryDTO(Class c,String commonPackage,String author) throws IOException{
String cName = c.getName();
String dtoPath = "";
if(author == null || author.equals("")){
author = "Administrator";
}
if(commonPackage != null && !commonPackage.equals("")){
dtoPath = commonPackage.replace(".", "/");
String fileName = System.getProperty("user.dir") + "/src/main/java/" + dtoPath+"/dto"
+ "/" + getLastChar(cName) + "FrontQueryDTO.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+commonPackage+".dto"+";"+RT_2+"import com.jeff.tianti.common.dto.CommonQueryDTO;"+RT_2
+"/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR_PARAMTER+ author +RT_1
+BLANK_1+"*"+BLANK_1+ANNOTATION_DESC +getLastChar(cName)+"QueryDTO"+BLANK_1+RT_1
+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1
+"public class " +getLastChar(cName) +"FrontQueryDTO extends CommonQueryDTO{"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}else{
System.out.println("创建"+getLastChar(cName)+"FrontQueryDTO失败,原因是commonPackage为空!");
}
}
/**
* 创建Dao接口
* @param c实体类
* @param commonPackage 基础包:如com.jeff.tianti.info
* @param author 作者
* @param desc 描述
* @throws IOException
*/
public static void createDaoInterface(Class c,String commonPackage,String author) throws IOException{
String cName = c.getName();
String daoPath = "";
if(author == null || author.equals("")){
author = "Administrator";
}
if(commonPackage != null && !commonPackage.equals("")){
daoPath = commonPackage.replace(".", "/");
String fileName = System.getProperty("user.dir") + "/tianti-cms/src/main/java/"/*src/main/java/"*/ + daoPath+"/dao"
+ "/" + getLastChar(cName) + "Dao.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+commonPackage+".dao"+";"+RT_2+"import "+commonPackage+".entity"+"."+getLastChar(cName)+";"+RT_1+"import com.jeff.tianti.common.dao.CommonDao;"+RT_2
+"/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR_PARAMTER+ author +RT_1
+BLANK_1+"*"+BLANK_1+ANNOTATION_DESC +getLastChar(cName)+"Dao接口"+BLANK_1+RT_1
+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1
+&