我是靠谱客的博主 纯情棉花糖,最近开发中收集的这篇文章主要介绍实体类创建数据库字段,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package top.zjtie.qingningmaicai3;

import com.google.common.collect.Lists;
import top.zjtie.qingningmaicai3.entity.*;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author QianJiajie
 * @date 2020-11-07 15:20
 */
public class DaoGenUtils {
    public static void genParam(List<String> params) {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        sb1.append(" where ");
        for (String p : params) {
            sb.append("@Param(" + p + ") " + p + ",");
            sb1.append(p + " = #{" + p + "} and ");
        }
        System.out.println(sb);
        System.out.println(sb1);
    }

    public static void genDataSchema(Class clazz) {

        if (clazz == null) {
            return;
        }
        String tableName = "Green_" + clazz.getSimpleName().substring(5);
        //获取关联的所有类,本类以及所有父类
        boolean ret = true;
        List<Class> clazzs = new ArrayList<Class>();
        //需要关联父类时候打开这个注释
        while (ret) {
            clazzs.add(clazz);
            clazz = clazz.getSuperclass();
            if (clazz == null || clazz == Object.class) {
                break;
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE `" + tableName + "` (n");


        for (int i = 0; i < clazzs.size(); i++) {
            Field[] declaredFields = clazzs.get(i).getDeclaredFields();
            List<Field> declaredFieldsList = Lists.newArrayList(declaredFields);
            declaredFieldsList = declaredFieldsList.stream()
                    .collect(Collectors.toList());

            Field[] fields = declaredFieldsList.toArray(new Field[0]);
            int length = fields.length;
            for (int j = 0; j < length; j++) {
                Field field = fields[j];
                int mod = field.getModifiers();
                //过滤 static 和 final 类型
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                String fieldName = field.getName();
                String schemaStr = "`" + fieldName + "` " + getSchemaType(field) + " n";
                if (j == length-1){
                    schemaStr = schemaStr.replace(",", "");
                }
                sb.append("  " +schemaStr);

            }
        }
        sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;n");
        System.out.println(sb);
    }

    private static String getSchemaType(Field field) {

        Class<?> fieldType = field.getType();
        if (fieldType == String.class) {
            return "VARCHAR(50) NOT NULL,";
        } else if (fieldType == Double.class || fieldType == double.class || fieldType == BigDecimal.class) {
            return "BIGDECIMAL(10,2) NOT NULL,";
        } else if (fieldType == Integer.class || fieldType == int.class) {
            if (field.getName().toLowerCase().contains("type") || field.getName().toLowerCase().contains("status")) {
                return "TINYINT(4) NOT NULL,";
            } else {
                return "INT(11) NOT NULL,";
            }
        } else if (fieldType == Date.class) {
            if (field.getName().contains("add")) {
                return "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,";
            } else if (field.getName().contains("update")) {
                return "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,";
            }
            return "timestamp,";
        }
        return "VARCHAR(50) NOT NULL,";
    }

    public static void main(String[] args) {
        //genParam(Lists.newArrayList("orderId", "ticketAmount", "speedPackAmount", "convertStatus", "realTicketGroupId", "failType"));
        genDataSchema(GreenAds.class);
    }
}

最后

以上就是纯情棉花糖为你收集整理的实体类创建数据库字段的全部内容,希望文章能够帮你解决实体类创建数据库字段所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部