我觉得造轮子这件事情,是谁都可以做的。只不过做得好或者不好而已,用心了做得就要优雅一点。
之前用过java的代码生成器,什么pojodobodbo都能生成,于是我也来自己造一个轮子。
造轮子的事情是没必要做得,费神费心,还没人家做得好,那么我还是要做,就当是体验一把了,看看细节是怎么实现的。
前期准备:
- 一台装有python、mysql的机器和若干待生成的表。
- python版本:3.6.4
- python安装mysql模块:pip install pymysql。(python2安装:pip install mysql-python
- 目标语言:java
待生成表格:为了实现各种数据类型,我们定义一个包含多种数据类型的实体表t_model,数据结构如下。
drop table if exists t_model; create table t_model( f_id varchar(64) primary key not null, --varchar 主键 f_number int null, f_datetime datetime, f_double double )
目标格式:
package com.dyi.po; import java.util.Date; /** * 表t_model模型 * @author WYB * */ public class Model { private String id; private int number; private Date date; private double dble; public Model() { super(); } public Model(String id, int number, Date date, double dble) { super(); this.id = id; this.number = number; this.date = date; this.dble = dble; } /** * * @return */ public String getId() { return id; } public void setId(String id) { this.id = id; } /** * * @return */ public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } /** * * @return */ public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } /** * * @return */ public double getDble() { return dble; } public void setDble(double dble) { this.dble = dble; } }
开始编写脚本
第一步:查询表结构
sql = """ select column_name,data_type,character_maximum_length,column_key,column_comment from information_schema.`COLUMNS` where TABLE_NAME = "%s" """%tableName cursor.execute(sql) tableColumnList = cursor.fetchall()
第二步:分析列的类型
cursor.execute(sql) tableColumnList = cursor.fetchall() modelName = tableName modelName = modelName[modelName.find("_") + 1:] modelName = modelName[0].upper()+modelName[1:] fieldInfoList = [] for col in tableColumnList: colName = col[0] colType = col[1].lower() colLen = col[2] priKey = col[3] comment = col[4]
第三步:拆分字段名,处理细节,生成代码
import pymysql ##连接数据库 db = pymysql.connect("localhost","root","root","stagebo") cursor = db.cursor() def log(str): print(str) def getTableList(): log("开始查询所有数据表...") cursor.execute("show tables") tableList = cursor.fetchall() tList = [] for t in tableList: tList.append(t[0]) return tList def getTableInfo(tableName): log("开始获取表结构") sql = """ select column_name,data_type,character_maximum_length,column_key,column_comment from information_schema.`COLUMNS` where TABLE_NAME = "%s" """%tableName cursor.execute(sql) tableColumnList = cursor.fetchall() modelName = tableName modelName = modelName[modelName.find("_") + 1:] modelName = modelName[0].upper()+modelName[1:] fieldInfoList = [] for col in tableColumnList: colName = col[0] colType = col[1].lower() colLen = col[2] priKey = col[3] comment = col[4] #字段去掉“f_” colName = colName[colName.find("_")+1:] #colName = colName[0].upper()+colName[1:] #判断类型 type = "" if colType in ["varchar","nvarchar"]: type = "String" elif colType == "int": type = "int" elif colType in ["double","float"]: type = "double" pk = False if priKey == "PRI": pk = True fieldInfoList.append([colName,type,pk]) file = open("%s.java"%modelName, "w") code = """ package com.dyi.po; import java.util.*; /** * 表%s模型 * */ """ %tableName code += "public class %s {"%modelName for item in fieldInfoList: code += """ private %s %s; """%(item[1],item[0]) code +=""" /* * 空构造函数 */ public %s(){ super(); } """%modelName code += """ /** *全参数构造函数 */ public %s("""%modelName for item in fieldInfoList: code += "%s %s, "%(item[1],item[0]) code = code[:-1] code += """) { super();""" for item in fieldInfoList: code += """ this.%s = %s;"""%(item[0],item[0]) code += """ }""" for item in fieldInfoList: t = item[1] n = item[0] nu = n[0].upper()+n[1:] code += """ /** * * @return */ public %s get%s(){ return this.%s; } public void set%s(%s %s){ this.%s = %s; } """%(t,nu,n,nu,t,n,n,n) code += "}" file.write(code) file.flush() file.close() if __name__ == "__main__": #查询表 tableList = getTableList() #定义要导出的表 tableToScript = ["t_model"] #开始遍历 for tableName in tableToScript: if tableName not in tableList: continue print(tableName) getTableInfo(tableName)
结果展示
package com.dyi.po; import java.util.*; /** * 表t_model模型 * */ public class Model { private String id; private int number; private date; private double dble; /* * 空构造函数 */ public Model(){ super(); } /** *全参数构造函数 */ public Model(String id, int number, date, double dble,) { super(); this.id = id; this.number = number; this.date = date; this.dble = dble; } /** * * @return */ public String getId(){ return this.id; } public void setId(String id){ this.id = id; } /** * * @return */ public int getNumber(){ return this.number; } public void setNumber(int number){ this.number = number; } /** * * @return */ public getDate(){ return this.date; } public void setDate( date){ this.date = date; } /** * * @return */ public double getDble(){ return this.dble; } public void setDble(double dble){ this.dble = dble; } }
然后流程就通了,一通百通,别的就可以照旧了~~~