import os import shutil from util.CommonUtil import * from util.MysqlHelper import * # ==============配置区开始========================================== # 要生成的数据表 tables = ['t_base_class'] # 类名 namespace = 'BaseClass' # 中文描述 tableMemo = '班级' # 数据库 host = '10.10.14.187' port = 22066 database = 'gtzz_base_db' user = 'root' password = 'DsideaL147258369' # 作者 author = '黄海' # 包名 packageName = 'com.dsideal.baseService.' + namespace # ==============配置区结束========================================== if __name__ == '__main__': # 删除目录 out_path = 'java' try: if os.path.exists(out_path): shutil.rmtree(out_path) # 创建目录 os.mkdir(out_path) except: pass # 生成时间 today = datetime.date.today() CurrentDate = today.strftime('%Y-%m-%d') # 实例化mysql数据库实例 db = MySQLHelper(host, port, database, user, password) # 是不是存在sort_id字段 sort_id_exists = False # 遍历生成 for table in tables: # 函数里面的参数 add_Params = '' update_Params = '' delete_Params = '' addRecordParams = '' # set record字符串 setRecordAddParams = '' setRecordUpdateParams = '' # 功能名称 FunctionName = getClassName(table) # 表的注释 sql = "select TABLE_COMMENT from information_schema.TABLES where table_schema = '%s' and table_name = '%s' " % ( database, table) dt = db.query(sql) tableComment = '未填写表注释说明,请维护一下!' if dt is not None and len(dt) > 0: tableComment = dt[0]['TABLE_COMMENT'] # 主键 pk = None sql = "select column_name FROM information_schema.COLUMNS where table_schema = schema() and column_key = 'PRI' " \ "and table_schema='%s' and table_name = '%s'" % (database, table) dt = db.query(sql) if dt is not None and len(dt) > 0: pk = str(dt[0]["column_name"]).lower() # 生成 sql = "select column_name 列名, data_type 字段类型, column_comment 字段注释 from information_schema.columns where table_name = '%s' and table_schema = '%s'" % ( table, database) dt = db.query(sql) # 1、生成Controller代码 with open('template\\Controller.template', 'r', encoding='utf-8') as f: templateString = f.read() # 处理替换# swagger 替换生成增加,修改,删除,分页的标签文字提示 templateString = templateString.replace('#(Author)', author) templateString = templateString.replace('#(add_remark)', '增加' + namespace + '记录') templateString = templateString.replace('#(add_summary)', '增加' + namespace + '记录') templateString = templateString.replace('#(update_remark)', '修改' + namespace + '记录') templateString = templateString.replace('#(update_summary)', '修改' + namespace + '记录') templateString = templateString.replace('#(delete_remark)', '删除' + namespace + '记录') templateString = templateString.replace('#(delete_summary)', '删除' + namespace + '记录') templateString = templateString.replace('#(page_remark)', '分页' + namespace + '记录') templateString = templateString.replace('#(page_summary)', '分页' + namespace + '记录') templateString = templateString.replace('#(ClassMemo)', tableMemo) # 开始循环 c = 0 for row in dt: c = c + 1 colName = str(row['列名']).lower() remark = row["字段注释"] data_type = row["字段类型"] # 1、Controller层代码 # 变更一个变量类型 if data_type == 'varchar': data_type = 'String' # 是否存在sort_id if colName == "sort_id": sort_id_exists = True # 增加 if colName != pk: add_Params = add_Params + ',' + data_type + ' ' + colName add_Params = delFirstDouHao(add_Params) # 修改 update_Params = update_Params + ',' + data_type + ' ' + colName update_Params = delFirstDouHao(update_Params) # 删除时必须是主键 if colName == pk: delete_Params = delete_Params + ',' + data_type + ' ' + colName delete_Params = delFirstDouHao(delete_Params) # 填充字符串 addRecordParams = add_Params + ',' + colName # set Record字符串 setRecordAddParams = setRecordAddParams + '\r' + '\t\trecord.set("' + colName + '",' + colName + ');' if colName != pk: setRecordUpdateParams = setRecordUpdateParams + '\r' + '\t\trecord.set("' + colName + '",' + colName + ');' # 替换类名 templateString = templateString.replace('#(ClassName)', FunctionName) templateString = templateString.replace('#(FirstLowClassName)', namespace[0].lower() + namespace[1:]) # 替换变量 templateString = templateString.replace("#(add_Params)", add_Params) templateString = templateString.replace("#(update_Params)", update_Params) templateString = templateString.replace("#(delete_Params)", delete_Params) # 向model层传递参数 add_Params_formodel = add_Params.replace('int ', '').replace('String ', '') templateString = templateString.replace('#(add_Params_formodel)', add_Params_formodel) update_Params_formodel = update_Params.replace('int ', '').replace('String ', '') templateString = templateString.replace('#(update_Params_formodel)', update_Params_formodel) delete_Params_formodel = delete_Params.replace('int ', '').replace('String ', '') templateString = templateString.replace('#(delete_Params_formodel)', delete_Params_formodel) # 生成时间 templateString = templateString.replace('#(CurrentTime)', CurrentDate) # 替换包名 templateString = templateString.replace('#(packageName)', packageName) # 替换中文描述 templateString = templateString.replace('#(ClassMemo)', tableMemo) # 表注释 # templateString = templateString.replace('#(ApiRemark)', tableComment) # 写入文件 with open(out_path + "\\" + namespace + 'Controller.java', 'w', encoding='utf-8') as f: f.write(templateString) # 2、生成Model代码 with open('template\\Model.template', 'r', encoding='utf-8') as f: templateString = f.read() # 作者 templateString = templateString.replace('#(Author)', author) templateString = templateString.replace('#(CurrentTime)', CurrentDate) templateString = templateString.replace('#(ClassName)', FunctionName) templateString = templateString.replace('#(className)', namespace) templateString = templateString.replace("#(table_name)", table) templateString = templateString.replace("#(pk)", pk) # 函数参数 templateString = templateString.replace("#(add_Params)", add_Params) templateString = templateString.replace('#(update_Params)', update_Params) templateString = templateString.replace('#(delete_Params)', delete_Params) # 表注释 # templateString = templateString.replace('#(ApiRemark)', tableComment) # 生成recordModel # templateString = templateString.replace('#(addRecordParams)', addRecordParams) templateString = templateString.replace('#(setRecordAddParams)', setRecordUpdateParams) templateString = templateString.replace('#(setRecordUpdateParams)', setRecordUpdateParams) # 替换包名 templateString = templateString.replace('#(packageName)', packageName) templateString = templateString.replace('#(ClassMemo)', tableMemo) if sort_id_exists: templateString = templateString.replace("#(sort_condition)", " order by sort_id") else: templateString = templateString.replace("#(sort_condition)", "") # 写入文件 with open(out_path + "\\" + namespace + 'Model.java', 'w', encoding='utf-8') as f: f.write(templateString) # 3、生成sql层代码 with open('template\\Sql.template', 'r', encoding='utf-8') as f: templateString = f.read() templateString = templateString.replace('#(ClassName)', FunctionName) templateString = templateString.replace('#(table_name)', table) templateString = templateString.replace('#(update_Params_formodel)', update_Params_formodel) if sort_id_exists: templateString = templateString.replace('#(sort_id)', 'sort_id') else: templateString = templateString.replace('#(sort_id)', pk) templateString = templateString.replace('#(ClassMemo)', tableMemo) # 替换包名 templateString = templateString.replace('#(packageName)', packageName) # 写入文件 with open(out_path + "\\" + namespace + '.sql', 'w', encoding='utf-8') as f: f.write(templateString) # 数据库关闭 db.close() logInfo('恭喜,所有操作成功完成!')