package com.example.service; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.example.annotation.BuildColumn; import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; /** * Created by tony on 2022/07/20. */ @Slf4j public class SqlGenerator { public static Map<String, String> javaProperty2SqlColumnMap = new HashMap<>(); //下边是对应的oracle的生成语句,类型都是oracle,如果是mysql还需要改。 static { javaProperty2SqlColumnMap.put("Integer", "int(9)"); javaProperty2SqlColumnMap.put("Short", "int(4)"); javaProperty2SqlColumnMap.put("Long", "int(18)"); javaProperty2SqlColumnMap.put("BigDecimal", "decimal(18,6)"); javaProperty2SqlColumnMap.put("BigInteger", "int(18)"); javaProperty2SqlColumnMap.put("Double", "double(22,2)"); javaProperty2SqlColumnMap.put("Float", "double(22,2)"); javaProperty2SqlColumnMap.put("Boolean", "tinyint(1)"); javaProperty2SqlColumnMap.put("Timestamp", "timestamp"); javaProperty2SqlColumnMap.put("Date", "date"); javaProperty2SqlColumnMap.put("String", "varchar(255)"); javaProperty2SqlColumnMap.put("Object", "varchar(255)"); } public static String createTableSql(Class<?> clz){ // 判断类上是否有次注解 boolean clzHasAnno = clz.isAnnotationPresent(TableName.class); String tableName = null; if (clzHasAnno) { // 获取类上的注解 TableName annotation = clz.getAnnotation(TableName.class); // 输出注解上的类名 String tableNameAnno = annotation.value(); if (tableNameAnno != null && !"".equals(tableNameAnno)) { tableName = tableNameAnno; } else { throw new RuntimeException("没有类名"); } } Field[] fields; fields = clz.getDeclaredFields(); //获取声明的字段,包括私有公有 String param ; String column; StringBuilder sb; sb = new StringBuilder(50); sb.append("create table `").append(tableName).append("`").append(" ("); String priKeyMain = null; boolean hasKeyField = false; //是否有主键 for (Field f : fields) { column = "`" + f.getName() + "`"; if (column.equals("serialVersionUID")) { continue; } String priKey = null; String auto_increment = null;//自增 String initValue=""; //初始化默认值 String comment=""; //字段备注 if (!hasKeyField) { boolean ifKeyField = f.isAnnotationPresent(TableId.class);//判断是否是主键 if (ifKeyField) { priKey = "`" + f.getName() + "`"; priKeyMain = priKey; hasKeyField = true; TableId annotation = f.getAnnotation(TableId.class); int key = annotation.type().getKey();//0自增,1none,2.. if (key == 0) { //自增 auto_increment = "not null auto_increment".toUpperCase(); } } } boolean fieldHasAnno = f.isAnnotationPresent(TableField.class); if (fieldHasAnno) { TableField fieldAnno = f.getAnnotation(TableField.class); //输出注解属性 String name = fieldAnno.value(); if (name != null && !"".equals(name)) { column = "`" + name + "`"; //如果有替换名,替换掉字段名 } if (!fieldAnno.exist()) { //过滤不存在的字段 continue; } } fieldHasAnno = f.isAnnotationPresent(BuildColumn.class); if (fieldHasAnno) { BuildColumn fieldAnno = f.getAnnotation(BuildColumn.class); //输出注解属性 initValue = fieldAnno.initValue(); comment = fieldAnno.comment(); } param = f.getType().getSimpleName();//获取字段数据类型 sb.append(column); //三种情况 分别处理 if (priKey != null && auto_increment != null) { sb.append(" ").append(javaProperty2SqlColumnMap.get("Long")).append(" "); sb.append(auto_increment); //是主键且自增 } else if (column.equalsIgnoreCase("`createTime`") && param.equalsIgnoreCase("timestamp")) { sb.append(" ").append(javaProperty2SqlColumnMap.get(param)).append(" "); sb.append(" NULL DEFAULT CURRENT_TIMESTAMP"); }else { sb.append(" ").append(javaProperty2SqlColumnMap.get(param)).append(" "); if (param.equalsIgnoreCase("timestamp")){ sb.append("NULL DEFAULT NULL "); }else if (initValue!=null && !"".equals(initValue)){ sb.append("DEFAULT '").append(initValue).append("' "); } if (comment!=null && !"".equals(comment)){ sb.append("COMMENT '").append(comment).append("' "); } } sb.append(", "); } if (hasKeyField) {//如果有主键 sb.append("PRIMARY KEY (").append(priKeyMain).append(") USING BTREE, "); } String sql = sb.toString(); //去掉最后一个逗号 int lastIndex = sql.lastIndexOf(","); sql = sql.substring(0, lastIndex) + sql.substring(lastIndex + 1); sql = sql.substring(0, sql.length() - 1) + " );"; System.out.println("#创建 "+tableName+" 表sql:"); System.out.println(sql); return sql; } }
拼接创建表SQL
最新推荐文章于 2023-12-22 11:29:26 发布