|
@@ -0,0 +1,334 @@
|
|
|
+package org.geek.szbay.assistant.code;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.geek.szbay.assistant.code.generator.ControllerMaker;
|
|
|
+import org.geek.szbay.assistant.code.generator.DaoMysqlMaker;
|
|
|
+import org.geek.szbay.assistant.code.generator.EntityMysqlMaker;
|
|
|
+import org.geek.szbay.assistant.code.generator.MapperXmlMaker;
|
|
|
+import org.geek.szbay.assistant.code.generator.ServiceImplMysqlMaker;
|
|
|
+import org.geek.szbay.assistant.code.generator.ServiceMysqlMaker;
|
|
|
+import org.geek.szbay.assistant.code.generator.helpper.TableHelpper;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wutianbin
|
|
|
+ * @Time:2017年5月18日 下午7:29:10
|
|
|
+ * @version 1.0
|
|
|
+ */
|
|
|
+public class CodeHelpper {
|
|
|
+ //private static final String DB_URL ="jdbc:mysql://127.0.0.1/demo?useEncodingUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf-8&useOldAliasMetadataBehavior=true&allowMultiQueries=true";
|
|
|
+ private static final String DB_URL ="jdbc:mysql://127.0.0.1/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull";
|
|
|
+ private static final String DB_NAME = "root";
|
|
|
+ private static final String DB_PASS = "admin";
|
|
|
+ private static final String DB_DRIVER ="com.mysql.jdbc.Driver";
|
|
|
+ private static final String DB_SCHEMA = "demo";
|
|
|
+
|
|
|
+ /** 作者名字 */
|
|
|
+ private String authorName = "wutianbin";
|
|
|
+
|
|
|
+ /** 表的前导字符 */
|
|
|
+ //private String preTableName = "*";
|
|
|
+ private String preTableName = "fp\\_exam";
|
|
|
+
|
|
|
+ /** 指定实体生成所在包的路径 */
|
|
|
+ private String entityOutPath = "com.donghaiair.flyprepare.admin.domain";
|
|
|
+
|
|
|
+ /** 指定Dao生成所在包的路径 */
|
|
|
+ private String daoOutPath = "com.donghaiair.flyprepare.admin.dao";
|
|
|
+
|
|
|
+ /** 指定Service生成所在包的路径 */
|
|
|
+ private String serviceOutPath = "com.donghaiair.flyprepare.admin.service";
|
|
|
+
|
|
|
+ /** 指定controller生成所在包的路径 */
|
|
|
+ private String controllerOutPath = "com.donghaiair.flyprepare.admin.controller";
|
|
|
+
|
|
|
+ /** dto所在的目录根路径 */
|
|
|
+ private String dtoRootPath = "com.donghaiair.flyprepare.admin.dto";
|
|
|
+
|
|
|
+ /** 指定Mapper生成所在包的路径 */
|
|
|
+ private String mapperOutPath = "mappers";
|
|
|
+
|
|
|
+ public void generate() {
|
|
|
+ Connection con = initDB();
|
|
|
+ if (con == null) {
|
|
|
+ System.out.println("Connection is null.");
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> tableList = getTableList(con, preTableName);
|
|
|
+ if (tableList == null || tableList.size() <= 0) {
|
|
|
+ System.out.println("tableList is null.");
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = "";
|
|
|
+
|
|
|
+ List<String> colNames = new ArrayList<String>(); // 列名
|
|
|
+ List<String> attrNames = new ArrayList<String>(); // 属性名
|
|
|
+ List<String> colTypes = new ArrayList<String>(); // 列名类型
|
|
|
+ List<String> colComments = new ArrayList<String>(); // 列名注释 Attr
|
|
|
+
|
|
|
+ for(String tableName : tableList.keySet()) {
|
|
|
+ String tableComment = tableList.get(tableName);
|
|
|
+
|
|
|
+ key = getTableColumns(con, tableName, attrNames, colNames, colTypes, colComments);
|
|
|
+ if (key == null || key.trim().isEmpty()) {
|
|
|
+ System.out.println(tableName + " not key!!!!!!!!!!");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ generateEntity(tableName, tableComment, key, attrNames, colNames, colTypes, colComments);
|
|
|
+
|
|
|
+ // 不做读写分离的dao
|
|
|
+ generateDao(tableName, tableComment, key, attrNames, colNames, colTypes, colComments);
|
|
|
+
|
|
|
+ // 不做读写分离的Mapper
|
|
|
+ generateMapper(tableName, tableComment, key, attrNames, colNames, colTypes, colComments);
|
|
|
+
|
|
|
+ generateService(tableName, tableComment, key, attrNames, colNames, colTypes, colComments);
|
|
|
+ generateServiceImpl(tableName, tableComment, key, attrNames, colNames, colTypes, colComments);
|
|
|
+
|
|
|
+ generateController(tableName, tableComment, key, attrNames, colNames, colTypes, colComments);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Connection initDB() {
|
|
|
+ // 连接数据库
|
|
|
+
|
|
|
+ // 创建连接
|
|
|
+ Connection con = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ Class.forName(DB_DRIVER);
|
|
|
+ con = DriverManager.getConnection(DB_URL,DB_NAME,DB_PASS);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return con;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> getTableList(Connection con, String preTableName) {
|
|
|
+ String sql = "SELECT table_name, table_comment FROM information_schema.tables "
|
|
|
+ + " WHERE table_schema = '" + DB_SCHEMA + "'"
|
|
|
+ + " AND table_type = 'BASE TABLE'";
|
|
|
+ // 查要生成实体类的表
|
|
|
+ if (null == preTableName || preTableName.isEmpty() || preTableName.trim().equals("*")) {
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ sql = sql + " AND table_name LIKE '" + preTableName + "%'";
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> ret = new HashMap<String, String>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Statement stat = con.createStatement();
|
|
|
+ ResultSet rs = stat.executeQuery(sql);
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ String value = rs.getString(1);
|
|
|
+ String comment = rs.getString(2);
|
|
|
+ ret.put(value, comment);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTableColumns(Connection con, String tableName, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ attrNames.clear();
|
|
|
+ colNames.clear();
|
|
|
+ colTypes.clear();
|
|
|
+ colComments.clear();
|
|
|
+
|
|
|
+ String sql = "SELECT COLUMN_NAME, COLUMN_COMMENT, COLUMN_KEY, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS "
|
|
|
+ + " WHERE table_schema = '" + DB_SCHEMA + "'"
|
|
|
+ + " AND table_name = '" + tableName + "'";
|
|
|
+
|
|
|
+ String key = "";
|
|
|
+
|
|
|
+ Statement stat = null;
|
|
|
+ try {
|
|
|
+ stat = con.createStatement();
|
|
|
+ ResultSet rs = stat.executeQuery(sql);
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ String colName = rs.getString(1);
|
|
|
+ String colComment = rs.getString(2);
|
|
|
+ String colType = rs.getString(4);
|
|
|
+
|
|
|
+ String pk = rs.getString(3);
|
|
|
+ if ("PRI".equals(pk)) {
|
|
|
+ key = colName;
|
|
|
+ }
|
|
|
+
|
|
|
+ attrNames.add(TableHelpper.columnNameToAttrName(colName));
|
|
|
+ colNames.add(colName);
|
|
|
+ colTypes.add(TableHelpper.sqlTypeToJavaType(colType));
|
|
|
+
|
|
|
+ if (colComment == null || colComment.trim().equals("")) {
|
|
|
+ colComment = colName;
|
|
|
+ }
|
|
|
+
|
|
|
+ colComments.add(colComment);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveToFile(String tableName, String packageOutPath, String content, String fileName) {
|
|
|
+ try {
|
|
|
+// File directory = new File("");
|
|
|
+ String path = this.getClass().getResource("").getPath();
|
|
|
+
|
|
|
+ System.out.println(path);
|
|
|
+ System.out.println("src/?/" + path.substring(path.lastIndexOf("/org/", path.length())));
|
|
|
+ // 固定输出到 d:\temp\generate-code 下
|
|
|
+ String outputPath = "d:/temp/generate-code/" + packageOutPath.replace(".", "/") + "/" + fileName;
|
|
|
+// String outputPath = directory.getAbsolutePath() + "/src/" + packageOutPath.replace(".", "/") + "/" + fileName;
|
|
|
+
|
|
|
+ forceMakeDirs(outputPath);
|
|
|
+
|
|
|
+ FileWriter fw = new FileWriter(outputPath);
|
|
|
+ PrintWriter pw = new PrintWriter(fw);
|
|
|
+ try {
|
|
|
+ pw.println(content);
|
|
|
+ pw.flush();
|
|
|
+ } finally {
|
|
|
+ pw.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void forceMakeDirs(String fileName) {
|
|
|
+ File file = new File(fileName);
|
|
|
+ File parentFile = file.getParentFile();
|
|
|
+
|
|
|
+ if (!parentFile.exists()) {
|
|
|
+ boolean result = parentFile.mkdirs();
|
|
|
+ if (!result) {
|
|
|
+ System.out.println("路径创建失败:" + fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateEntity(String tableName, String tableComment,
|
|
|
+ String key, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ String className = TableHelpper.tableNameToClassName(tableName) + "DO";
|
|
|
+ String content = EntityMysqlMaker.parse(authorName, className,
|
|
|
+ tableName, tableComment, key, entityOutPath,
|
|
|
+ attrNames, colNames, colTypes, colComments);
|
|
|
+ saveToFile(tableName, entityOutPath, content, className + ".java");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateDao(String tableName, String tableComment,
|
|
|
+ String key, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ String tname = TableHelpper.tableNameToClassName(tableName);
|
|
|
+ String className = tname + "Dao";
|
|
|
+ String entityFullClassName = entityOutPath + "." + tname + "DO";
|
|
|
+ String content = DaoMysqlMaker.parse(authorName, className,
|
|
|
+ tableName, tableComment, key, daoOutPath,
|
|
|
+ entityFullClassName, tname + "DO",
|
|
|
+ attrNames, colNames, colTypes, colComments);
|
|
|
+ saveToFile(tableName, daoOutPath, content, className + ".java");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateService(String tableName, String tableComment,
|
|
|
+ String key, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ String tname = TableHelpper.tableNameToClassName(tableName);
|
|
|
+ String className = tname + "Service";
|
|
|
+ String entityFullClassName = entityOutPath + "." + tname + "DO";
|
|
|
+ String content = ServiceMysqlMaker.parse(authorName, className,
|
|
|
+ tableName, tableComment, key, serviceOutPath,
|
|
|
+ entityFullClassName, tname + "DO",
|
|
|
+ attrNames, colNames, colTypes, colComments);
|
|
|
+ saveToFile(tableName, serviceOutPath, content, className + ".java");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateServiceImpl(String tableName, String tableComment,
|
|
|
+ String key, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ String tname = TableHelpper.tableNameToClassName(tableName);
|
|
|
+ String className = tname + "ServiceImpl";
|
|
|
+
|
|
|
+ String entityClassName = tname + "DO";
|
|
|
+ String entityFullClassName = entityOutPath + "." + entityClassName;
|
|
|
+
|
|
|
+ String daoClassName = tname + "Dao";
|
|
|
+ String daoFullClassName = daoOutPath + "." + daoClassName;
|
|
|
+
|
|
|
+ String serviceClassName = tname + "Service";
|
|
|
+ String serviceFullClassName = serviceOutPath + "." + serviceClassName;
|
|
|
+
|
|
|
+ String content = ServiceImplMysqlMaker.parse(authorName, className, tableName,
|
|
|
+ tableComment, key, serviceOutPath + ".impl",
|
|
|
+ entityFullClassName, entityClassName,
|
|
|
+ daoFullClassName, daoClassName,
|
|
|
+ serviceFullClassName, serviceClassName,
|
|
|
+ attrNames, colNames, colTypes, colComments);
|
|
|
+ saveToFile(tableName, serviceOutPath + ".impl", content, className + ".java");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateMapper(String tableName, String tableComment,
|
|
|
+ String key, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ String tname = TableHelpper.tableNameToClassName(tableName);
|
|
|
+
|
|
|
+ String entityClassName = tname + "DO";
|
|
|
+ String entityFullClassName = entityOutPath + "." + entityClassName;
|
|
|
+
|
|
|
+ String daoClassName = tname + "Dao";
|
|
|
+ String daoFullClassName = daoOutPath + "." + daoClassName;
|
|
|
+
|
|
|
+ String fileName = tname + "Mapper";
|
|
|
+
|
|
|
+ String content = MapperXmlMaker.parse(authorName, tableName, tableComment, key, mapperOutPath,
|
|
|
+ entityFullClassName, entityClassName,
|
|
|
+ daoFullClassName, daoClassName,
|
|
|
+ attrNames, colNames, colTypes, colComments);
|
|
|
+
|
|
|
+ saveToFile(tableName, mapperOutPath, content, fileName + ".xml");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateController(String tableName, String tableComment,
|
|
|
+ String key, List<String> attrNames, List<String> colNames,
|
|
|
+ List<String> colTypes, List<String> colComments) {
|
|
|
+ String tname = TableHelpper.tableNameToClassName(tableName);
|
|
|
+ String className = tname + "Controller";
|
|
|
+
|
|
|
+ String entityClassName = tname + "DO";
|
|
|
+ String entityFullClassName = entityOutPath + "." + entityClassName;
|
|
|
+
|
|
|
+ String serviceClassName = tname + "Service";
|
|
|
+ String serviceFullClassName = serviceOutPath + "." + serviceClassName;
|
|
|
+
|
|
|
+ String content = ControllerMaker.parse(authorName, className,
|
|
|
+ tableName, tableComment, key, controllerOutPath,
|
|
|
+ entityFullClassName, entityClassName, serviceFullClassName, serviceClassName,
|
|
|
+ dtoRootPath, attrNames, colNames, colTypes, colComments);
|
|
|
+ saveToFile(tableName, controllerOutPath, content, className + ".java");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|