Java代码实体类生成SQL语句(Java实体类转数据库)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java代码实体类⽣成SQL语句(Java实体类转数据库)
有的时候把数据库删了,如果照着实体类重新创建数据库的话⽐较⿇烦,可以使⽤这个⼯具,把代码复制到项⽬⾥⾯设置⼀下即可把Java代码中的实体类转换为SQL语句输出为⼀个⽂件,打开执⾏命令即可。
代码如下:
package Main;
import javax.xml.bind.annotation.XmlElement;
import java.io.*;
import ng.annotation.Annotation;
import ng.reflect.Field;
public class GenerateSqlFromEntityUtil {
public static void main(String[] a) {
// 实体类的位置
Class klass = er.class;
// ⽣成的sql语句的位置
String outputPath = "D:/outSql/User.txt";
generateTableSql(klass, outputPath, null);
System.out.println("⽣成结束");
}
public static void writeFile(String content, String outputPath) {
File file = new File(outputPath);
System.out.println("⽂件路径: " + file.getAbsolutePath());
// 输出⽂件的路径
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter out = null;
try {
// 如果⽂件存在,就删除
if (file.exists()) {
file.delete();
}
file.createNewFile();
fos = new FileOutputStream(file, true);
osw = new OutputStreamWriter(fos);
out = new BufferedWriter(osw);
out.write(content);
// 清空缓冲流,把缓冲流⾥的⽂本数据写⼊到⽬标⽂件⾥
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void generateTableSql(Class obj, String outputPath, String tableName) { // tableName 如果是 null,就⽤类名做表名
if (tableName == null || tableName.equals("")) {
tableName = obj.getName();
tableName = tableName.substring(stIndexOf(".") + 1);
}
// 表名⽤⼤写字母
tableName = tableName.toUpperCase();
Field[] fields = obj.getDeclaredFields();
Object param;
String column;
StringBuilder sb = new StringBuilder();
sb.append("drop table if exists ").append(tableName).append(";\r\n");
sb.append("\r\n");
sb.append("create table ").append(tableName).append("(\r\n");
System.out.println(tableName);
boolean firstId = true;
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
column = f.getName();
System.out.println(column + ", " + f.getType().getSimpleName());
param = f.getType();
sb.append(column); // ⼀般第⼀个是主键
if (param instanceof Integer) {
sb.append(" INTEGER ");
} else {
// 注意:根据需要,⾃⾏修改 varchar 的长度。
这⾥设定为长度等于 50
int length = 50;
sb.append(" VARCHAR(" + length + ")");
}
if (firstId == true) {
sb.append(" PRIMARY KEY ");
firstId = false;
}
// 获取字段中包含 fieldMeta 的注解
// 获取属性的所有注释
Annotation[] allAnnotations = f.getAnnotations();
XmlElement xmlElement = null;
Class annotationType = null;
for (Annotation an : allAnnotations) {
sb.append(" COMMIT '");
sb.append(" COMMIT '");
xmlElement = (XmlElement) an;
annotationType = an.annotationType();
param = ((XmlElement) an).name();
System.out.println("属性 " + f.getName() + " ----- 的注释类型有: " + param);
sb.append(param).append("'");
}
if (i != fields.length - 1) { // 最后⼀个属性后⾯不加逗号
sb.append(", ");
}
sb.append("\n");
}
String sql = sb.toString();
sql = sb.substring(0, sql.length() - 1) + "\n) " + "ENGINE = INNODB DEFAULT CHARSET = utf8;"; writeFile(sql, outputPath);
}
}。