package org.example;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ParseModelToJavaBean {
private static final String CLASS_PATH = "src/main/java/org/example/po";
private static Map<String,String> toJavaType ;
// 对应数据库类型
private static Map<String,String> toDbType;
static{
//初始化类型转换
toJavaType = new HashMap<>();
toJavaType.put("STRING","String");
toJavaType.put("DATETIME","Date");
toDbType = new HashMap<>();
// 自行定义类型转换
toDbType.put("STRING","DB.STRING");
toDbType.put("DATETIME","DB.DATETIME");
}
public static void main(String[] args) {
Configuration configuration = new Configuration();
Writer out = null;
try {
configuration.setDirectoryForTemplateLoading(new File(new File("").getAbsolutePath()+File.separator+"file"));
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("classPath", "org.example.po");
dataMap.put("className", "User");
List<PropertyEntity> properties = parseExcel("test.xlsx");
transportPropertyType(properties);
dataMap.put("properties", properties);
Template template = configuration.getTemplate("test.ftl");
File docFile = new File(CLASS_PATH + "\\" + "User.java");
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
template.process(dataMap, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.flush();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static List<PropertyEntity> parseExcel(String entityFileName){
List<PropertyEntity> propertyEntities = new ArrayList<>();
String fileName = new File("").getAbsolutePath()+File.separator+"file"+File.separator+"entity"+File.separator+entityFileName;
System.out.println(fileName);
EasyExcel.read(fileName, PropertyEntity.class, new PageReadListener<PropertyEntity>(dataList -> {
for (PropertyEntity entity : dataList) {
propertyEntities.add(entity);
}
})).sheet().doRead();
return propertyEntities;
}
/**
* 转换属性
* @param properties
*/
public static void transportPropertyType(List<PropertyEntity> properties){
properties.forEach(propertyEntity -> {
propertyEntity.setPropertyJavaType(toJavaType.get(propertyEntity.getPropertyType()));
propertyEntity.setPropertyDbType(toDbType.get(propertyEntity.getPropertyType()));
String fieldName = propertyEntity.getOrgFieldName();
propertyEntity.setPropertyName(toHump(fieldName));
});
}
public static String toHump(String fieldName){
// 驼峰命名
String[] keyArray = fieldName.toLowerCase().split("_");
if (keyArray.length > 1) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keyArray.length; i++) {
String ks = keyArray[i];
if (!"".equals(ks)) {
if (i == 0) {
sb.append(ks);
} else {
int c = ks.charAt(0);
if (c >= 97 && c <= 122) {
int v = c - 32;
sb.append((char) v);
if (ks.length() > 1) {
sb.append(ks.substring(1));
}
} else {
sb.append(ks);
}
}
}
}
return sb.toString();
}
return fieldName;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
通过读取解析固定模板中的内容,生成JavaBean类。 除了生成Bean ,也可以生成其他类,可自行扩展。 这种场景比较局限,项目开发中用到的不多,如果定义类有几十、表字段很多,同时应对项目初期,表结构表更需求较多,频繁修改调整。此时,如果拥有一个自己的小工具,绝对事半功倍,也希望大家在日常工作中,对这样的工作能多做积累。 模板解析,也可以替换成从word,或者数据库中读取解析。 这两种定义来源本代码中没有,需要下载后自行扩展。 生成对象内容可通过模板自定义,详情查看文章,也可直接下载代码。 https://blog.csdn.net/oJuHao12345/article/details/126872806
资源详情
资源评论
资源推荐
收起资源包目录
autoInitJavaBean.rar (15个子文件)
autoInitJavaBean
pom.xml 3KB
src
test
java
org
example
main
java
org
example
ParseModelToJavaBean.java 4KB
po
User.java 1011B
OwnerAnno.java 825B
PropertyEntity.java 974B
.idea
uiDesigner.xml 9KB
misc.xml 526B
jarRepositories.xml 864B
encodings.xml 191B
compiler.xml 613B
workspace.xml 7KB
.gitignore 184B
inspectionProfiles
Project_Default.xml 1KB
file
test.ftl 537B
entity
test.xlsx 9KB
共 15 条
- 1
每天都要有成长
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0