剑道第一仙

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

将java实体类转为数据库sql

 

复制代码
package com.qr.util;
/**  

* 创建时间:2023年8月15日 下午3:24:23  
* 文件名称:CreateTableSql.java  
* 类说明:  
*/


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.xml.bind.annotation.XmlElement;

import com.qr.entity.Sffbusdocmain;
import com.qr.entity.Sffdocdetail;
import com.qr.entity.Sffdocsub;
import com.qr.entity.Sffsubbusdocmain;

import com.qr.entity.Book;
public class CreateTableSql { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { createTable(Book.class, null); } public static void createTable(Class obj,String tableName) throws IOException{ Field[] fields = null; fields = obj.getDeclaredFields(); Class annotationType = null; Object param = null; String column = null; XmlElement xmlElement = null; StringBuilder sb = null; sb = new StringBuilder(50); if(tableName==null||tableName.equals("")){ //未传表明默认用类名 tableName = obj.getName(); tableName = tableName.substring(tableName.lastIndexOf(".")+1); } sb.append("\r\ndrop table if exists ").append(tableName).append(";\r\n"); sb.append("create table ").append(tableName).append(" ( \r\n"); System.out.println(tableName); boolean firstId = true; File file = null; for(Field f:fields){ column = f.getName(); sb.append(column).append(" "); System.out.println(column+","+f.getType()); param = f.getType(); // sb.append(column);//一般第一个是主键 if(param instanceof Integer){ sb.append(" INTEGER "); }else{ sb.append(" VARCHAR(30) ");//根据需要自行修改 } // if(firstId){//类型转换 // sb.append(" PRIMARY KEY "); // firstId = false; // } //获取字段中包含fieldMeta的注解 //2、获取属性上的所有注释 Annotation[] allAnnotations = f.getAnnotations(); for(Annotation an : allAnnotations){ sb.append(" COMMIT '"); xmlElement = (XmlElement)an; annotationType = an.annotationType(); param = ((XmlElement) an).name(); System.out.println("属性【"+f.getName()+"-----的注释类型有: " + param); sb.append(param).append("'"); } sb.append(",\n "); } String sql = null; sql = sb.toString(); sql = sb.substring(0, sql.length()-1)+" );"; // sql = sb.substring(0, sql.length()-1)+" )ENGINE =INNODB DEFAULT CHARSET= utf8;\r\n"; file = new File("WebContent/createTable/createtable.sql"); //文件保存路径与命名 if (!file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { } } if (!file.exists()) { file.createNewFile(); } System.out.println("文件路径:"+file.getAbsolutePath()); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true))); out.write(sql) ; out.flush(); out.close() ; } }
复制代码

 

public class Book{
        private String bookname;
        private String bookid;
    }

 

posted on   剑道第一仙  阅读(429)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示