快速生成java实体类
程序员文章站
2022-06-14 23:09:35
...
需求描述:当你有很多接口需要开发时,并且接口里有很多字段。可以把文档里的字段名跟描述直接拷到excel中,用工具类直接生成java实体 。
工具类:
package com.hns.util;
import com.hns.handler.impl.WebServiceHandler;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import javax.jws.WebMethod;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* Copyright (C), 2015-2018
* FileName:
* Author: fangzg
* Date: 2018/9/11
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
public class ClassGenerator {
private static final String path ="C:\\CLASS\\";
private static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception{
classGeneratorByTemplateExcel();
}
/**
* 根据excel模版创建java实体
*/
public static void classGeneratorByTemplateExcel(){
try{
File file =new File("src/main/resources/templates/entityTemplate.xlsx");
InputStream is = new FileInputStream(file);
Workbook workBook = new HSSFWorkbook(is);
Sheet sheet = workBook.getSheetAt(0);
StringBuilder getSb = null;
StringBuilder jsonSb =null;
Row row;
Cell cell;
int sheetCount =1;
int startRow=0;
while(true){
row = sheet.getRow(1);
startRow =3;
if (row!=null&& PubUtil.isNotEmpty(row.getCell(0).getStringCellValue())) {
String entityName = row.getCell(0).getStringCellValue();
if(entityName==null||"".equals(entityName))break;
File classFile = new File(path+entityName+".java");
FileOutputStream in = new FileOutputStream(classFile);
System.out.println(entityName);
append("package com.hns.vo;");
append("import com.alibaba.fastjson.JSONObject;");
append("import javax.persistence.*;");
append("import java.math.BigDecimal;");
append("/**\n" +
" * Copyright (C), 2015-2018\n" +
" * FileName:\n" +
" * Author: fangzg\n" +
" * Date: 2018/9/11\n" +
" * Description:\n" +
" * History:\n" +
" * <author> <time> <version> <desc>\n" +
" * 作者姓名 修改时间 版本号 描述\n" +
" */");
sb.append("public class ");
sb.append(entityName);
append(" extends BaseVo {");
getSb = new StringBuilder();
jsonSb = new StringBuilder();
jsonSb.append("\tpublic JSONObject ackJSONObject() {\r\n");
jsonSb.append("\t\tJSONObject json = new JSONObject();\r\n");
while(true){
if(sheet.getRow(startRow)==null){
break;
}
String desc = sheet.getRow(startRow).getCell(0).getStringCellValue();
String type = sheet.getRow(startRow).getCell(1).getStringCellValue();
String field = sheet.getRow(startRow).getCell(2).getStringCellValue();
append("\r\n\tprivate "+type+" "+field+"; //"+desc);
getSb.append("\r\n\tpublic "+type+" get"+upperCase(field)+"(){\r\n");
getSb.append("\t\treturn "+field+";\r\n");
getSb.append("\t}\r\n");
getSb.append("\r\n\tpublic void set"+upperCase(field)+"("+type+" "+field+"){\r\n");
getSb.append("\t\tthis."+field+"="+field+";\r\n");
getSb.append("\t}\r\n");
jsonSb.append("\t\tjson.put(\""+field+"\","+field+");\r\n");
startRow++;
}
append(getSb.toString());
jsonSb.append("\t\treturn json;\r\n");
jsonSb.append("\t}");
append(jsonSb.toString());
append("}");
in.write(sb.toString().getBytes());
sb =new StringBuilder();
}else{
break;
}
sheet = workBook.getSheetAt(sheetCount);
sheetCount++;
}
}catch (Exception e){
e.printStackTrace();
}
}
public static String upperCase(String str) {
char[] ch = str.toCharArray();
if (ch[0] >= 'a' && ch[0] <= 'z') {
ch[0] = (char) (ch[0] - 32);
}
return new String(ch);
}
private static void append (String str){
sb.append(str).append("\r\n");
}
}
pom.xml添加依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
excel模版格式:
类名 | ||||
Class1 | ||||
字段描述 | 字段类型 | 字段属性 | ||
名称 | String | name | ||
年龄 | int | age |