通过实体类生成 sql 建表语句
2016-09-05 17:23
441 查看
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;
public class CreateTable {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
createTable(User.class, null);
// 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)+" )ENGINE =INNODB DEFAULT CHARSET= utf8;\r\n";
file = new File("WebContent/createTable/建表.txt");
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() ;
}
}
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;
public class CreateTable {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
createTable(User.class, null);
// 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)+" )ENGINE =INNODB DEFAULT CHARSET= utf8;\r\n";
file = new File("WebContent/createTable/建表.txt");
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() ;
}
}
相关文章推荐
- 通过SQL语句生成日历表格
- 通过excel生成SQL语句完成数据库操作
- MySQL通过SQL语句来直接生成新表
- 通过读取excel文件生成sql语句
- java通过实体类生成sql、dao、service文件
- Entity Framework 4.0 :通过实体类生成sql脚本
- 通过sql语句生成MySQl数据字典
- 通过已有SQL语句,生成数据库模型PDM
- 通过excel生成SQL语句完成数据库操作
- 利用自定义的SQL语句生成类代替实体类的使用
- 通过一个函数或sql语句,随机生成任意6个字母组成的字符
- 反射+注释,根据实体类对象生成SQL语句工具类
- 通过java反射实现对javabean生成各种sql语句
- 通过SQL语句或数据库,生成数据库模型PDM方法详解
- 生成实体类和数据库操作类(基于SQL语句而不是存储过程)的CodeSmith模板
- 通过一个函数或sql语句,随机生成任意6个字母组成的字符
- java通过sql代码生成Dao和实体类
- 通过java反射实现对javabean生成各种sql语句
- 用PowerDesigner将SQL语句生成实体类
- 【分享】通过Excel生成批量SQL语句,处理大量数据的好办法