经常会遇到项目需要加字段的事情,一个两个也就那么样,多了就比较烦。虽然框架里自带代码生成工具,脚手架基本上都有,但是,有时候需要改字段,加字段,数量还比较多的时候,再生成一次很不方便,所以现在我写了一个简易脚本,专门用来加字段用,方便快捷,废话不多说,直接上代码
# -*- coding: utf-8 -*-
import re
# 下划线转驼峰法函数
def underline_to_camel(underline_str):
components = underline_str.split('_')
return components[0] + ''.join(x.title() for x in components[1:])
# 建表语句
create_table_sql = """
CREATE TABLE `t_dw_physical_data` (
`id` bigint(20) NOT NULL COMMENT '主键',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名',
`height` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '身高',
`weight` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '体重',
`status` int(11) DEFAULT NULL COMMENT '状态',
`create_user` bigint(20) DEFAULT NULL COMMENT '创建人',
`create_dept` bigint(20) DEFAULT NULL COMMENT '创建部门',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_user` bigint(20) DEFAULT NULL COMMENT '修改人',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
`is_deleted` int(11) DEFAULT '0' COMMENT '是否已删除',
`import_batch` bigint(20) DEFAULT NULL,
`gender` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '性别',
`id_card_no` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '身份证号',
`spine` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '脊柱',
`surgical_history` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '外科病史',
`head_neck` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '头颈部',
`chest_abdomen` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '胸腹部',
`limbs_joints` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '四肢关节',
`genitourinary` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '泌尿生殖',
`anus` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '肛门',
`skin_tattoo` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '皮肤纹身',
`surgical_other` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '外科其他',
`diastolic_pressure` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '舒张压',
`internal_medicine_other` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '内科其他',
`stutter` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '口吃',
`lung` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '肺',
`nerve` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '神经',
`abdomen` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '腹部',
`cardiac_history` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '心脏病史',
`heart_rate` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '心率',
`blood_pressure` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '血压',
`heart` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '心脏',
`left_eye_corrected_vision` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '左眼矫正视力',
`right_eye_corrected_vision` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '右眼矫正视力',
`left_eye_unaided_vision` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '左眼裸眼视力',
`right_eye_unaided_vision` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '右眼裸眼视力',
`color_blindness` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '色盲',
`color_recognition_ability` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '单色识别能力',
`ophthalmic_history` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '眼科病史',
`color_weakness` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '色弱',
`ent_history` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '耳鼻咽喉科病史',
`ear` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '耳',
`olfaction` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '嗅觉',
`left_ear_hearing` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '左耳听力',
`right_ear_hearing` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '右耳听力',
`nose` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '鼻',
`throat` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '咽喉',
`tympanic_membrane_condition` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '鼓膜情况',
`ear_pressure_function` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '耳气压功能',
`dental_caries` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '龋齿',
`periodontitis` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '牙周炎',
`occlusion_teeth` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '咬合牙',
`missing_teeth` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '缺齿',
`irregular_teeth` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '牙列不齐',
`oral_other` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '口腔科其他',
`chest_x_ray` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '胸部X线',
`electrocardiogram` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '心电图',
`gynecological_ultrasound` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '妇科B超',
`abdominal_ultrasound` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '腹部B超',
`gynecological_history` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '妇科病史',
`gynecological_diseases` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '妇科疾病',
`menarche` date DEFAULT NULL COMMENT '初潮',
`last_period` date DEFAULT NULL COMMENT '末次月经',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='体能数据';
"""
# 解析建表语句,提取字段信息
# 剔除第一行表名并解析建表语句,提取字段信息
columns = re.findall(r"`(.*?)`\s(.*?)(?=\sCOMMENT '(.*?)')", create_table_sql.encode('utf-8').split(b"\n", 1)[1].decode('utf-8'), re.DOTALL)
# 生成entity类
# 生成entity类
entity_class = f"public class PhysicalData {{\n"
for column in columns:
field_name = column[0]
field_type = column[1]
java_type = "String" if "varchar" in field_type else field_type.split('(')[0].capitalize()
entity_class += f" @ApiModelProperty(value = \"{column[2]}\")\n"
entity_class += f" private {java_type} {underline_to_camel(field_name)};\n\n"
entity_class += "}"
# 生成MyBatis的XML文件
mybatis_xml = f"<?xml version='1.0' encoding='UTF-8' ?>\n"
mybatis_xml += "<mapper namespace='PhysicalDataMapper'>\n"
mybatis_xml += " <resultMap id='BaseResultMap' type='PhysicalData'>\n"
for column in columns:
field_name = column[0]
mybatis_xml += f" <result column='{field_name}' property='{field_name}'/>\n"
mybatis_xml += " </resultMap>\n"
mybatis_xml += "</mapper>"
# 生成Vue所需的column的JSON
# 生成Vue所需的column的JSON
# 生成Vue所需的column的JSON
vue_columns = "[\n"
for column in columns:
field_name = column[0]
field_type = column[1]
field_label = column[2]
camel_prop = underline_to_camel(field_name)
vue_columns += f" {{\n label: \"{field_label}\",\n prop: \"{camel_prop}\",\n type: \"input\",\n }},\n"
vue_columns += "]"
print("Entity类:\n", entity_class)
print("\nMyBatis的XML文件:\n", mybatis_xml)
print("\nVue所需的column的JSON:\n", vue_columns)
输出的结果如下
D:\pythonFile\venv\Scripts\python.exe D:\pythonFile\代码生成工具.py
Entity类:
public class PhysicalData {
@ApiModelProperty(value = "主键")
private tDwPhysicalData;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "身高")
private String height;
@ApiModelProperty(value = "体重")
private String weight;
@ApiModelProperty(value = "状态")
private Int status;
@ApiModelProperty(value = "创建人")
private Bigint createUser;
@ApiModelProperty(value = "创建部门")
private Bigint createDept;
@ApiModelProperty(value = "创建时间")
private Datetime default null createTime;
@ApiModelProperty(value = "修改人")
private Bigint updateUser;
@ApiModelProperty(value = "修改时间")
private Datetime default null updateTime;
@ApiModelProperty(value = "是否已删除")
private Int isDeleted;
@ApiModelProperty(value = "性别")
private String importBatch;
@ApiModelProperty(value = "身份证号")
private String idCardNo;
@ApiModelProperty(value = "脊柱")
private String spine;
@ApiModelProperty(value = "外科病史")
private String surgicalHistory;
@ApiModelProperty(value = "头颈部")
private String headNeck;
@ApiModelProperty(value = "胸腹部")
private String chestAbdomen;
@ApiModelProperty(value = "四肢关节")
private String limbsJoints;
@ApiModelProperty(value = "泌尿生殖")
private String genitourinary;
@ApiModelProperty(value = "肛门")
private String anus;
@ApiModelProperty(value = "皮肤纹身")
private String skinTattoo;
@ApiModelProperty(value = "外科其他")
private String surgicalOther;
@ApiModelProperty(value = "舒张压")
private String diastolicPressure;
@ApiModelProperty(value = "内科其他")
private String internalMedicineOther;
@ApiModelProperty(value = "口吃")
private String stutter;
@ApiModelProperty(value = "肺")
private String lung;
@ApiModelProperty(value = "神经")
private String nerve;
@ApiModelProperty(value = "腹部")
private String abdomen;
@ApiModelProperty(value = "心脏病史")
private String cardiacHistory;
@ApiModelProperty(value = "心率")
private String heartRate;
@ApiModelProperty(value = "血压")
private String bloodPressure;
@ApiModelProperty(value = "心脏")
private String heart;
@ApiModelProperty(value = "左眼矫正视力")
private String leftEyeCorrectedVision;
@ApiModelProperty(value = "右眼矫正视力")
private String rightEyeCorrectedVision;
@ApiModelProperty(value = "左眼裸眼视力")
private String leftEyeUnaidedVision;
@ApiModelProperty(value = "右眼裸眼视力")
private String rightEyeUnaidedVision;
@ApiModelProperty(value = "色盲")
private String colorBlindness;
@ApiModelProperty(value = "单色识别能力")
private String colorRecognitionAbility;
@ApiModelProperty(value = "眼科病史")
private String ophthalmicHistory;
@ApiModelProperty(value = "色弱")
private String colorWeakness;
@ApiModelProperty(value = "耳鼻咽喉科病史")
private String entHistory;
@ApiModelProperty(value = "耳")
private String ear;
@ApiModelProperty(value = "嗅觉")
private String olfaction;
@ApiModelProperty(value = "左耳听力")
private String leftEarHearing;
@ApiModelProperty(value = "右耳听力")
private String rightEarHearing;
@ApiModelProperty(value = "鼻")
private String nose;
@ApiModelProperty(value = "咽喉")
private String throat;
@ApiModelProperty(value = "鼓膜情况")
private String tympanicMembraneCondition;
@ApiModelProperty(value = "耳气压功能")
private String earPressureFunction;
@ApiModelProperty(value = "龋齿")
private String dentalCaries;
@ApiModelProperty(value = "牙周炎")
private String periodontitis;
@ApiModelProperty(value = "咬合牙")
private String occlusionTeeth;
@ApiModelProperty(value = "缺齿")
private String missingTeeth;
@ApiModelProperty(value = "牙列不齐")
private String irregularTeeth;
@ApiModelProperty(value = "口腔科其他")
private String oralOther;
@ApiModelProperty(value = "胸部X线")
private String chestXRay;
@ApiModelProperty(value = "心电图")
private String electrocardiogram;
@ApiModelProperty(value = "妇科B超")
private String gynecologicalUltrasound;
@ApiModelProperty(value = "腹部B超")
private String abdominalUltrasound;
@ApiModelProperty(value = "妇科病史")
private String gynecologicalHistory;
@ApiModelProperty(value = "妇科疾病")
private String gynecologicalDiseases;
@ApiModelProperty(value = "初潮")
private Date default null menarche;
@ApiModelProperty(value = "末次月经")
private Date default null lastPeriod;
}
MyBatis的XML文件:
<?xml version='1.0' encoding='UTF-8' ?>
<mapper namespace='PhysicalDataMapper'>
<resultMap id='BaseResultMap' type='PhysicalData'>
<result column='t_dw_physical_data' property='t_dw_physical_data'/>
<result column='name' property='name'/>
<result column='height' property='height'/>
<result column='weight' property='weight'/>
<result column='status' property='status'/>
<result column='create_user' property='create_user'/>
<result column='create_dept' property='create_dept'/>
<result column='create_time' property='create_time'/>
<result column='update_user' property='update_user'/>
<result column='update_time' property='update_time'/>
<result column='is_deleted' property='is_deleted'/>
<result column='import_batch' property='import_batch'/>
<result column='id_card_no' property='id_card_no'/>
<result column='spine' property='spine'/>
<result column='surgical_history' property='surgical_history'/>
<result column='head_neck' property='head_neck'/>
<result column='chest_abdomen' property='chest_abdomen'/>
<result column='limbs_joints' property='limbs_joints'/>
<result column='genitourinary' property='genitourinary'/>
<result column='anus' property='anus'/>
<result column='skin_tattoo' property='skin_tattoo'/>
<result column='surgical_other' property='surgical_other'/>
<result column='diastolic_pressure' property='diastolic_pressure'/>
<result column='internal_medicine_other' property='internal_medicine_other'/>
<result column='stutter' property='stutter'/>
<result column='lung' property='lung'/>
<result column='nerve' property='nerve'/>
<result column='abdomen' property='abdomen'/>
<result column='cardiac_history' property='cardiac_history'/>
<result column='heart_rate' property='heart_rate'/>
<result column='blood_pressure' property='blood_pressure'/>
<result column='heart' property='heart'/>
<result column='left_eye_corrected_vision' property='left_eye_corrected_vision'/>
<result column='right_eye_corrected_vision' property='right_eye_corrected_vision'/>
<result column='left_eye_unaided_vision' property='left_eye_unaided_vision'/>
<result column='right_eye_unaided_vision' property='right_eye_unaided_vision'/>
<result column='color_blindness' property='color_blindness'/>
<result column='color_recognition_ability' property='color_recognition_ability'/>
<result column='ophthalmic_history' property='ophthalmic_history'/>
<result column='color_weakness' property='color_weakness'/>
<result column='ent_history' property='ent_history'/>
<result column='ear' property='ear'/>
<result column='olfaction' property='olfaction'/>
<result column='left_ear_hearing' property='left_ear_hearing'/>
<result column='right_ear_hearing' property='right_ear_hearing'/>
<result column='nose' property='nose'/>
<result column='throat' property='throat'/>
<result column='tympanic_membrane_condition' property='tympanic_membrane_condition'/>
<result column='ear_pressure_function' property='ear_pressure_function'/>
<result column='dental_caries' property='dental_caries'/>
<result column='periodontitis' property='periodontitis'/>
<result column='occlusion_teeth' property='occlusion_teeth'/>
<result column='missing_teeth' property='missing_teeth'/>
<result column='irregular_teeth' property='irregular_teeth'/>
<result column='oral_other' property='oral_other'/>
<result column='chest_x_ray' property='chest_x_ray'/>
<result column='electrocardiogram' property='electrocardiogram'/>
<result column='gynecological_ultrasound' property='gynecological_ultrasound'/>
<result column='abdominal_ultrasound' property='abdominal_ultrasound'/>
<result column='gynecological_history' property='gynecological_history'/>
<result column='gynecological_diseases' property='gynecological_diseases'/>
<result column='menarche' property='menarche'/>
<result column='last_period' property='last_period'/>
</resultMap>
</mapper>
Vue所需的column的JSON:
[
{
label: "主键",
prop: "tDwPhysicalData",
type: "input",
},
{
label: "姓名",
prop: "name",
type: "input",
},
{
label: "身高",
prop: "height",
type: "input",
},
{
label: "体重",
prop: "weight",
type: "input",
},
{
label: "状态",
prop: "status",
type: "input",
},
{
label: "创建人",
prop: "createUser",
type: "input",
},
{
label: "创建部门",
prop: "createDept",
type: "input",
},
{
label: "创建时间",
prop: "createTime",
type: "input",
},
{
label: "修改人",
prop: "updateUser",
type: "input",
},
{
label: "修改时间",
prop: "updateTime",
type: "input",
},
{
label: "是否已删除",
prop: "isDeleted",
type: "input",
},
{
label: "性别",
prop: "importBatch",
type: "input",
},
{
label: "身份证号",
prop: "idCardNo",
type: "input",
},
{
label: "脊柱",
prop: "spine",
type: "input",
},
{
label: "外科病史",
prop: "surgicalHistory",
type: "input",
},
{
label: "头颈部",
prop: "headNeck",
type: "input",
},
{
label: "胸腹部",
prop: "chestAbdomen",
type: "input",
},
{
label: "四肢关节",
prop: "limbsJoints",
type: "input",
},
{
label: "泌尿生殖",
prop: "genitourinary",
type: "input",
},
{
label: "肛门",
prop: "anus",
type: "input",
},
{
label: "皮肤纹身",
prop: "skinTattoo",
type: "input",
},
{
label: "外科其他",
prop: "surgicalOther",
type: "input",
},
{
label: "舒张压",
prop: "diastolicPressure",
type: "input",
},
{
label: "内科其他",
prop: "internalMedicineOther",
type: "input",
},
{
label: "口吃",
prop: "stutter",
type: "input",
},
{
label: "肺",
prop: "lung",
type: "input",
},
{
label: "神经",
prop: "nerve",
type: "input",
},
{
label: "腹部",
prop: "abdomen",
type: "input",
},
{
label: "心脏病史",
prop: "cardiacHistory",
type: "input",
},
{
label: "心率",
prop: "heartRate",
type: "input",
},
{
label: "血压",
prop: "bloodPressure",
type: "input",
},
{
label: "心脏",
prop: "heart",
type: "input",
},
{
label: "左眼矫正视力",
prop: "leftEyeCorrectedVision",
type: "input",
},
{
label: "右眼矫正视力",
prop: "rightEyeCorrectedVision",
type: "input",
},
{
label: "左眼裸眼视力",
prop: "leftEyeUnaidedVision",
type: "input",
},
{
label: "右眼裸眼视力",
prop: "rightEyeUnaidedVision",
type: "input",
},
{
label: "色盲",
prop: "colorBlindness",
type: "input",
},
{
label: "单色识别能力",
prop: "colorRecognitionAbility",
type: "input",
},
{
label: "眼科病史",
prop: "ophthalmicHistory",
type: "input",
},
{
label: "色弱",
prop: "colorWeakness",
type: "input",
},
{
label: "耳鼻咽喉科病史",
prop: "entHistory",
type: "input",
},
{
label: "耳",
prop: "ear",
type: "input",
},
{
label: "嗅觉",
prop: "olfaction",
type: "input",
},
{
label: "左耳听力",
prop: "leftEarHearing",
type: "input",
},
{
label: "右耳听力",
prop: "rightEarHearing",
type: "input",
},
{
label: "鼻",
prop: "nose",
type: "input",
},
{
label: "咽喉",
prop: "throat",
type: "input",
},
{
label: "鼓膜情况",
prop: "tympanicMembraneCondition",
type: "input",
},
{
label: "耳气压功能",
prop: "earPressureFunction",
type: "input",
},
{
label: "龋齿",
prop: "dentalCaries",
type: "input",
},
{
label: "牙周炎",
prop: "periodontitis",
type: "input",
},
{
label: "咬合牙",
prop: "occlusionTeeth",
type: "input",
},
{
label: "缺齿",
prop: "missingTeeth",
type: "input",
},
{
label: "牙列不齐",
prop: "irregularTeeth",
type: "input",
},
{
label: "口腔科其他",
prop: "oralOther",
type: "input",
},
{
label: "胸部X线",
prop: "chestXRay",
type: "input",
},
{
label: "心电图",
prop: "electrocardiogram",
type: "input",
},
{
label: "妇科B超",
prop: "gynecologicalUltrasound",
type: "input",
},
{
label: "腹部B超",
prop: "abdominalUltrasound",
type: "input",
},
{
label: "妇科病史",
prop: "gynecologicalHistory",
type: "input",
},
{
label: "妇科疾病",
prop: "gynecologicalDiseases",
type: "input",
},
{
label: "初潮",
prop: "menarche",
type: "input",
},
{
label: "末次月经",
prop: "lastPeriod",
type: "input",
},
]
Process finished with exit code 0
到时候自己生成直接替换即可