ExcelUtil.java : https://blog.csdn.net/Lxinccode/article/details/97279040
说明 : xls文件第一行可以不需要,字段名、字段类型、字段含义和字段说明位置可以配置
创建xls文件,内容如下
字段名 | 字段类型 | 长度 | 字段含义 | 字段说明 | 必选 |
name | String | 10 | 姓名 | 人的姓名 | 必须 |
age | Integer | 2 | 年龄 | 人的年龄 | 必须 |
生成的代码如下
@ApiModelProperty("字段含义,说明:字段说明")
private null 字段名;
@ApiModelProperty("姓名,说明:人的姓名")
private String name;
@ApiModelProperty("年龄,说明:人的年龄")
private Integer age;
工具代码如下
import cn.nordrassil.util.ExcelUtil;
import cn.nordrassil.util.StringUtil;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @Date: 2020/9/16 15:08
* @Description: 根据文档生成实体类
*/
public class GenerateAttributeServer {
private final static Map<String, String> map = new HashMap<String, String>() {{
put("JSONString", "String");
put("String", "String");
put("VarChar2", "String");
put("Varchar2", "String");
put("varchar", "String");
put("int", "Integer");
put("Int", "Integer");
put("Integer", "Integer");
put("Double", "Double");
put("Long", "Long");
}};
private final static Integer ZI_DUAN_MING = 0;
private final static Integer ZI_DUAN_LEI_XING = 1;
private final static Integer ZI_DUAN_HAN_YI = 3;
private final static Integer ZI_DUAN_SHUO_MING = 4;
public static void main(String[] args) {
test1();
}
private static void test1() {
List<String> obj = new LinkedList<>();
List<List<String>> lists = ExcelUtil.readXls("/Users/leyili/Desktop/test1.xls");
StringBuilder stringBuilder = new StringBuilder();
for (List<String> list : lists) {
if (list.size() == 0)
break;
int i = 0;
for (String l : list) {
if (l.equals(""))
i++;
}
if (i == list.size())
continue;
String descrbe = list.get(ZI_DUAN_HAN_YI);
if (list.size() > ZI_DUAN_SHUO_MING && !list.get(ZI_DUAN_SHUO_MING).equals("")){
descrbe += ",说明:"+list.get(ZI_DUAN_SHUO_MING);
}
descrbe = descrbe.replaceAll("\"", "");
descrbe = descrbe.replaceAll("\n",",");
stringBuilder.append("@ApiModelProperty(\"" + descrbe + "\")\n");
stringBuilder.append("private ");
if (list.get(ZI_DUAN_LEI_XING).indexOf("JSONString_") != -1) {
String type = list.get(ZI_DUAN_LEI_XING).split("_")[1];
switch (type) {
case "S":
stringBuilder.append("String");
break;
case "I":
stringBuilder.append("Integer");
break;
case "LI":
stringBuilder.append("List<Integer>");
break;
case "LLI":
stringBuilder.append("List<List<Integer>>");
break;
default:
break;
}
} else if (list.get(ZI_DUAN_LEI_XING).equals("JsonArray")) {
String o = StringUtil.oneToUp(list.get(ZI_DUAN_MING));
stringBuilder.append("List<"+o+"> ");
obj.add(o);
} else if (list.get(ZI_DUAN_LEI_XING).equals("JSONObject") || list.get(ZI_DUAN_LEI_XING).equals("Json") || list.get(ZI_DUAN_LEI_XING).equals("JsonObject")) {
String o = StringUtil.oneToUp(list.get(ZI_DUAN_MING));
stringBuilder.append(o);
obj.add(o);
} else if (list.get(ZI_DUAN_LEI_XING).indexOf("JSONString_") != -1) {
String type = list.get(ZI_DUAN_LEI_XING).split("_")[1];
switch (type) {
case "LS":
stringBuilder.append("List<String>");
break;
case "LI":
stringBuilder.append("List<Integer>");
default:
break;
}
} else {
stringBuilder.append(map.get(list.get(ZI_DUAN_LEI_XING)));
}
stringBuilder.append(" ").append(list.get(ZI_DUAN_MING)).append(";\n\n");
}
if (obj.size() >0){
for (String s : obj) {
stringBuilder.append("@Setter\n").append("@Getter\npublic static class ").append(s).append("{\n");
stringBuilder.append("\n}\n");
}
}
System.err.println(stringBuilder);
}
}
@ApiModelProperty 是 swagger,maven坐标如下
<springfox-swagger.version>2.9.2</springfox-swagger.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
<exclusions>
<exclusion>
<artifactId>mapstruct</artifactId>
<groupId>org.mapstruct</groupId>
</exclusion>
</exclusions>
</dependency>
END。