this.tablename = prop.getProperty("tablename").toString();
this.packageOutPath = prop.getProperty("packageOutPath").toString();
this.authorName = prop.getProperty("authorName").toString();
}
// 创建多个实体类
private void genEntity(ListString tablenames, Connection conn) {
// 使用第归生成文件
for (String tablename : tablenames) {
this.genEntity(tablename, conn);
}
}
// 创建单个实体类
private void genEntity(String tablename, Connection conn) {
String sql = "select * from " + tablename;
PreparedStatement pstmt = null;
ResultSetMetaData rsmd = null;
try {
pstmt = DB.getPStmt(conn, sql);
rsmd = pstmt.getMetaData();
int size = rsmd.getColumnCount(); // 统计列
colnames = new ArrayListString();
colTypes = new ArrayListString();
for (int i = 0; isize; i++) {
colnames.add(rsmd.getColumnName(i + 1));
colTypes.add(rsmd.getColumnTypeName(i + 1));
if (colTypes.get(i).equalsIgnoreCase("datetime")) {
f_util = true;
}
if (colTypes.get(i).equalsIgnoreCase("image")
|| colTypes.get(i).equalsIgnoreCase("text")) {
f_sql = true;
}
【java代码生成实体类 java代码生成uml类图】}
System.out.println(colnames);
System.out.println(colTypes);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
DB.close(pstmt);
}
// 在内存中生成代码
String content = parse(tablename);
// 写入到文件中
try {
File directory = new File("");
String outputPath = directory.getAbsolutePath() + "/src/"
+ this.packageOutPath.replace(".", "/") + "/";
System.out.println("写出的路径:" + outputPath);
// 检测路径是否存在,不存在就创建路径
File path = new File(outputPath);
if (!path.exists()!path.isDirectory()) {
path.mkdir();
System.out.println(path.exists());
}
// 创建文件
outputPath += initcap(tablename) + ".java";
File file = new File(outputPath);
if (!file.exists()) {
file.createNewFile();
}
// 写出到硬盘
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getAllEntityTable(Connection conn, ListString tablenames) {
ResultSet rs = null;
try {
DatabaseMetaData dmd = (DatabaseMetaData) conn.getMetaData();
/*
* TABLE_CAT String = 表类别(可为 null)
* TABLE_SCHEM String = 表模式(可为null)
* TABLE_NAME String = 表名称
* TABLE_TYPE String = 表类型
*/
rs = dmd.getTables(null, null, "%", null);
while (rs.next()) {
tablenames.add(rs.getString("TABLE_NAME"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
*
* @param tablename
* @return
*/
private String parse(String tablename) {
StringBuffer sb = new StringBuffer();
// 判断是否导入工具包
if (f_util) {
sb.append("import java.util.Date;\r\n");
}
if (f_sql) {
sb.append("import java.sql.*;\r\n");
}
sb.append("package " + this.packageOutPath + ";\r\n");
sb.append("\r\n");
// 注释部分
sb.append("/**\r\n");
sb.append("* " + tablename + " 实体类\r\n");
sb.append("* " + new Date() + " " + this.authorName + "\r\n");
sb.append("*/ \r\n");
// 实体部分
sb.append("\r\n\r\npublic class " + initcap(tablename) + "{\r\n");
processAllAttrs(sb);// 属性
推荐阅读
- 斗鱼直播直播游戏黑屏,斗鱼直播游戏黑屏怎么办
- 数据导入到hbase的api有哪些,hbase导入数据方法
- 角色扮演超市钱币游戏攻略,幼儿角色扮演超市游戏
- 新郑运营网站搭建咨询热线,新郑客服
- linux找文件的命令 linux找文件夹命令
- chatgpt券商研报,券商研报模板
- 关于linux安装flutter的信息
- 微信直播大哥风华清泉,微信直播中的大哥
- 如何查看php数据库密码 php怎么查询数据库