public class CreateBeanAuto{
private List<Map> columnList = new ArrayList<Map>();
private Map<String, String> contentMap = new HashMap<String, String>();
private static Map<String, String> dateTypeMap = new HashMap<String, String>();
static {
dateTypeMap.put("VARCHAR2", "String");
dateTypeMap.put("CLOB", "String");
dateTypeMap.put("NUMBER", "long");
dateTypeMap.put("DATE", "Date");
dateTypeMap.put("BLOB", "Blob");
}
public void create(String fileUrl, List<String> tableNameList, Map<String, String> entityName) {
for (int i = 0; i < tableNameList.size(); i++) {
String columnSql = "select column_name,data_type from user_tab_cols where table_name = upper('" + tableNameList.get(i) + "')";
columnList = 根据自己的实际项目架构查询columnSql;
contentMap.put("PACKAGE", "package com.hfits.util.entity;\r\r");
contentMap.put("DATEIMPORT", "import java.util.Date;\r");
contentMap.put("BLOBIMPORT", "import oracle.sql.BLOB;\r");
contentMap.put("BASEIMPORT", "import org.nutz.dao.entity.annotation.Column;\r" +
"import org.nutz.dao.entity.annotation.Index;\r" +
"import org.nutz.dao.entity.annotation.Table;\r" +
"import org.nutz.dao.entity.annotation.TableIndexes;\r\r");
contentMap.put("TABLE", "@Table(\"" + tableNameList.get(i).toUpperCase() + "\")\r");
contentMap.put("TABLEINDEXES", "@TableIndexes({@Index(name = \"" + tableNameList.get(i).toUpperCase() + "_PK" + "\", fields = {\"id\"}, unique = false)})\r");
contentMap.put("CLASSNAME", "public class " + entityName.get(tableNameList.get(i)) + " {\r");
Map<String, String> columnMap = convert(columnList);
String javaContent = contentMap.get("PACKAGE");
if ("1".equals(columnMap.get("havingDATE"))) {
javaContent += contentMap.get("DATEIMPORT");
}
if ("1".equals(columnMap.get("havingBLOB"))) {
javaContent += contentMap.get("BLOBIMPORT");
}
columnMap.remove("havingDATE");
columnMap.remove("havingBLOB");
columnMap.remove("havingNUMBER");
javaContent += contentMap.get("BASEIMPORT")
+ contentMap.get("TABLE")
+ contentMap.get("TABLEINDEXES")
+ contentMap.get("CLASSNAME");
String codeContent = "";
String methodContent = "";
for (Map.Entry<String, String> entry : columnMap.entrySet()) {
String column = entry.getKey().toLowerCase();
String columnDataType = entry.getValue();
codeContent += " @Column(\"" + column + "\")\r";
codeContent += " private " + dateTypeMap.get(columnDataType) + " " + column.replace("_","") + ";" +
"\r\r";
methodContent += " public " + dateTypeMap.get(columnDataType) + " get" + toUpperCaseFirstOne
(column).replace("_","") + "(){\r ";
methodContent += " return " + column.replace("_","") + ";\r";
methodContent += " }\r\r";
methodContent += " public " + dateTypeMap.get(columnDataType) + " set" + toUpperCaseFirstOne(column).replace("_","") + "(" + dateTypeMap.get(columnDataType) + " " + column.replace("_","") + ") {\r";
methodContent += " this." + column.replace("_","") + " = " + column.replace("_","") + ";\r";
methodContent += " return " + column.replace("_","") + ";\r";
methodContent += " }\r\r";
}
javaContent += codeContent + methodContent + "}";
String url = fileUrl + entityName.get(tableNameList.get(i)) + ".java";
File file = new File(url);
FileWriter fw = null;
if (file.exists()) {
file.delete();
}
if (!file.exists()) {
try {
file.createNewFile();
fw = new FileWriter(file, true);
} catch (IOException e) {
e.printStackTrace();
}
}
PrintWriter pw = new PrintWriter(fw);
pw.println(javaContent);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Map<String, String> convert(List<Map> columnList) {
Map<String, String> result = new HashMap<String, String>();
result.put("havingDATE", "0");
result.put("havingBLOB", "0");
for (Map map : columnList) {
result.put(StringUtils.null2String(map.get("column_name")), StringUtils.null2String(map.get("data_type")));
if ("DATE".equals(map.get("data_type"))) {
result.put("havingDATE", "1");
}
if ("BLOB".equals(map.get("data_type"))) {
result.put("havingBLOB", "1");
}
}
return result;
}
// 首字母转大写
private String toUpperCaseFirstOne(String s) {
if (Character.isUpperCase(s.charAt(0))) {
return s;
} else {
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
}
}
}
//测试
public String test() {
Map<String, Object> response = new HashMap<String, Object>();
List<String> tableNameList = new ArrayList<String>();
tableNameList.add(表名);
Map<String, String> entityName = new HashMap<String, String>();
entityName.put(表名, 类名);
createBeanAuto.create("H:\\test", tableNameList, entityName);
response.put("code", "10000");
response.put("msg", "创建实体类成功!");
return JSON.toJSONString(response);
}
java oracle下自动生成实体类工具类
最新推荐文章于 2023-02-14 15:03:54 发布