1. 程式人生 > >快速生成java實體類

快速生成java實體類

需求描述:當你有很多介面需要開發時,並且接口裡有很多欄位。可以把文件裡的欄位名跟描述直接拷到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