python读取excel自动化生成sql建表语句和java实体类字段

1、首先准备一个excel文件:

 

id type name
id int 学号
name string 姓名
age int 年龄
sex string 性别
weight decimal(20,4) 体重
score decimal(20,4) 分数

2、直接生成java字段和注释:

import pandas as pd

df = pd.read_excel('test.xlsx', sheet_name='Sheet1')


for i in range(len(df.values)):
    data_type = ''
    if df['type'][i] == 'bigint':
        data_type = "Long"
    elif df['type'][i] == 'int':
        data_type = "Integer"
    elif df['type'][i] == 'string':
        data_type = "String"
    elif df['type'][i] == 'decimal(20,4)':
        data_type = "Double"

    strs = "/**\n* " + df['name'][i] + "\n*/\n"
    str_entity = strs + 'private' + ' ' + data_type + ' ' + df['id'][i] + ';'

    print(str_entity)


3、生成建表语句:

import pandas as pd

df = pd.read_excel('test.xlsx', sheet_name='Sheet1')

str1 = 'create table student' + ' ' + '(' + '\n'
str2 = ")"
for i in range(len(df.values)):
    data_type = ''
    if df['type'][i] == 'bigint':
        data_type = "bigint(20)"
    elif df['type'][i] == 'int':
        data_type = "int(11)"
    elif df['type'][i] == 'string':
        data_type = "varchar(20)"
    elif df['type'][i] == 'decimal(20,4)':
        data_type = "decimal(20,4)"
    str = '`' + df['id'][i] + '`' + ' ' + data_type + ' ' + 'NOT NULL COMMENT' + ' ' + "'" + df['name'][i] + "'" + ','
    str1 = str1 + str + '\n'
# 删除末尾换行符和","
str1 = str1[:-2]
str1 = str1 + str2 + ';'
print(str1)


结果:

python读取excel自动化生成sql建表语句和java实体类字段_第1张图片

可以根据实际需求增加主键或其它约束。

你可能感兴趣的:(java,自动化,sql)