属于图像编码的编码方式 编码方式( 三 )


1.查询表格信息
首先,我们要从数据库中拿到我们生成代码所需要的表和列相关信息 。
| 1.1.查询表信息
查询表信息语句:
select t.table_name as '表名称', t.table_comment as '表备注'from information_schema.tables twhere t.table_schema = ?and t.table_type = 'BASE TABLE'and t.table_name = ?;其中,第1个问号赋值数据库名称,第2个问号赋值表名称 。
查询表信息结果:

属于图像编码的编码方式  编码方式

文章插图
| 1.2.查询列信息
查询列信息语句:
select c.column_name as '列名称', c.column_comment as '列备注', c.data_type as '数据类型', c.character_maximum_length as '字符长度', c.numeric_precision as '数字精度', c.numeric_scale as '数字范围', c.column_default as '', c.is_nullable as '是否可空', c.column_key as '列键名'from information_schema.columns cwhere c.table_schema = ?and c.table_name = ?order by c.ordinal_position;其中,第1个问号赋值数据库名称,第2个问号赋值表名称 。
查询列信息结果:
属于图像编码的编码方式  编码方式

文章插图
2.编写生成代码
| 2.1.编写生成模型类代码
/** 生成模型类文件函数 */private void generateModelClassFile(File dir, Table table, List<Column> columnList) throws Exception { try (PrintWriter writer = new PrintWriter(new File(dir, className + "DO.java"))) { String className = getClassName(table.getTableName()); String classComments = getClassComment(table.getTableComment()); writer.println("package " + groupName + "." + systemName + ".database;"); ...... writer.println("/** " + classComments + "DO类 */"); writer.println("@Getter"); writer.println("@Setter"); writer.println("@ToString"); writer.println("public class " + className + "DO {"); for (Column column : columnList) { String fieldType = getFieldType(column); String fieldName = getFieldName(column.getColumnName()); String fieldComment = getFieldComment(column); writer.println("\t/** " + fieldComment + " */"); writer.println("\tprivate " + fieldType + " " + fieldName + ";"); } writer.println("}"); }}| 2.2.编写生成 DAO 接口代码
/** 生成DAO接口文件函数 */private void generateDaoInterfaceFile(File dir, Table table, List<Column> columnList, List<Column> pkColumnList) throws Exception { try (PrintWriter writer = new PrintWriter(new File(dir, className + "DAO.java"))) { String className = getClassName(table.getTableName()); String classComments = getClassComment(table.getTableComment()); writer.println("package " + groupName + "." + systemName + ".database;"); ...... writer.println("/** " + classComments + "DAO接口 */"); writer.println("public interface " + className + "DAO {"); writer.println("\t/** 获取" + classComments + "函数 */"); writer.print("\tpublic " + className + "DO get("); boolean isFirst = true; for (Column pkColumn : pkColumnList) { if (!isFirst) { writer.print(", "); } else { isFirst = false; } String fieldType = getFieldType(pkColumn); String fieldName = getFieldName(pkColumn.getColumnName()); writer.print("@Param(\"" + fieldName + "\") " + fieldType + " " + fieldName); } writer.println(");"); ...... writer.println("}"); }}| 2.3.编写生成 DAO 映射代码
/** 生成DAO映射文件函数 */private void generateDaoMapperFile(File dir, Table table, List<Column> columnList, List<Column> pkColumnList) throws Exception { try (PrintWriter writer = new PrintWriter(new File(dir, className + "DAO.xml"))) { String className = getClassName(table.getTableName()); String classComments = getClassComment(table.getTableComment()); writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); ...... writer.println("<!-- " + classComments + "映射 -->"); writer.println("<mapper namespace=\"" + groupName + "." + systemName + ".database." + className + "DAO\">"); writer.println("\t<!-- 所有字段语句 -->"); writer.println("\t<sql id=\"fields\">"); if (CollectionUtils.isNotEmpty(columnList)) { boolean isFirst = true; String columnName = getColumnName(pkColumn.getColumnName()); for (Column column : columnList) { if (isFirst) { isFirst = false; writer.println("\t\t" + columnName); } else { writer.println("\t\t, " + columnName); } } } writer.println("\t</sql>"); writer.println("\t<!-- 获取" + classComments + "函数语句 -->"); writer.println("\t<select id=\"get\" resultType=\"" + groupName + "." + systemName + ".database." + className + "DO\">"); writer.println("\t\tselect"); writer.println("\t\t<include refid=\"fields\"/>"); writer.println("\t\tfrom " + table.getTableName()); boolean isFirst = true; for (Column pkColumn : pkColumnList) { String columnName = getColumnName(pkColumn.getColumnName()); String fieldName = getFieldName(pkColumn.getColumnName()); writer.print("\t\t"); if (isFirst) { writer.print("where"); isFirst = false; } else { writer.print("and"); } writer.println(" " + columnName + " = #{" + fieldName + "}"); } writer.println("\t</select>"); writer.println("</mapper>"); }}3.生成相关代码

推荐阅读