Commit 78aabd2b9a2186138e72745369179f6c0c38ad73
1 parent
67310d3f
Exists in
master
1.add some adapter function
Showing
3 changed files
with
217 additions
and
23 deletions
Show diff stats
build.gradle
| @@ -51,7 +51,7 @@ uploadArchives { | @@ -51,7 +51,7 @@ uploadArchives { | ||
| 51 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 51 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
| 52 | } | 52 | } |
| 53 | pom.project { | 53 | pom.project { |
| 54 | - version '1.2.4' | 54 | + version '1.2.6' |
| 55 | artifactId ARTIFACT_Id | 55 | artifactId ARTIFACT_Id |
| 56 | groupId GROUP_ID | 56 | groupId GROUP_ID |
| 57 | packaging TYPE | 57 | packaging TYPE |
build/classes/java/main/com/taover/codegenerate/bazhuayun/GenerateCode.class
No preview for this file type
src/main/java/com/taover/codegenerate/bazhuayun/GenerateCode.java
| @@ -18,7 +18,7 @@ import com.taover.codegenerate.db.Tools; | @@ -18,7 +18,7 @@ import com.taover.codegenerate.db.Tools; | ||
| 18 | import freemarker.template.Configuration; | 18 | import freemarker.template.Configuration; |
| 19 | import freemarker.template.Template; | 19 | import freemarker.template.Template; |
| 20 | 20 | ||
| 21 | -public class GenerateCode { | 21 | +public class GenerateCode { |
| 22 | public static void main(String args[]){ | 22 | public static void main(String args[]){ |
| 23 | try { | 23 | try { |
| 24 | generate("com.taover.wxorder", "ALL", "D:"+File.separator+"dblist", "127.0.0.1", "3306", "tylife", "lexi365", "8zyun_wxorder"); | 24 | generate("com.taover.wxorder", "ALL", "D:"+File.separator+"dblist", "127.0.0.1", "3306", "tylife", "lexi365", "8zyun_wxorder"); |
| @@ -46,26 +46,40 @@ public class GenerateCode { | @@ -46,26 +46,40 @@ public class GenerateCode { | ||
| 46 | //取数据库连接 | 46 | //取数据库连接 |
| 47 | Connection conn = getConnection(host, port, user, password, dbName); | 47 | Connection conn = getConnection(host, port, user, password, dbName); |
| 48 | //初始化要生成的表列表 | 48 | //初始化要生成的表列表 |
| 49 | - List<String> tableNameList = new ArrayList<String>(); | ||
| 50 | - | ||
| 51 | - if ("ALL".equalsIgnoreCase(tableNameStr) ) { | ||
| 52 | - String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+dbName+"'"; | ||
| 53 | - ResultSet rs = Tools.executeQuery(Tools.createStmt(conn), sql); | ||
| 54 | - if (null == rs) return ; | ||
| 55 | - try { | ||
| 56 | - while (rs.next()) { | ||
| 57 | - tableNameList.add(rs.getString("Table_name")); | ||
| 58 | - } | ||
| 59 | - } catch (SQLException e) { | ||
| 60 | - e.printStackTrace(); | ||
| 61 | - } | ||
| 62 | - Tools.close(rs); | ||
| 63 | - } else { | ||
| 64 | - String[] TableNameArr = tableNameStr.split(","); | ||
| 65 | - for (int i = 0; i < TableNameArr.length; i++) | ||
| 66 | - tableNameList.add(TableNameArr[i]); | 49 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); |
| 50 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 51 | + | ||
| 52 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 53 | + | ||
| 54 | + //取得表中所有列 | ||
| 55 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 56 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 57 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 58 | + continue; | ||
| 59 | + } | ||
| 60 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 61 | + if (pkcolum == null) { | ||
| 62 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 63 | + continue; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 67 | + generateDto(srcPath, apiModel); | ||
| 68 | + generateEntity(srcPath, apiModel); | ||
| 69 | + generateManageController(srcPath, apiModel); | ||
| 70 | + generateV1Controller(srcPath, apiModel); | ||
| 71 | + generateRepository(srcPath, apiModel); | ||
| 72 | + generateService(srcPath, apiModel); | ||
| 73 | + generateVo(srcPath, apiModel); | ||
| 67 | } | 74 | } |
| 68 | - | 75 | + System.out.println("处理完成"); |
| 76 | + } | ||
| 77 | + | ||
| 78 | + public static void generateDto(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 79 | + //取数据库连接 | ||
| 80 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 81 | + //初始化要生成的表列表 | ||
| 82 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 69 | for (int i = 0; i < tableNameList.size(); i++) { | 83 | for (int i = 0; i < tableNameList.size(); i++) { |
| 70 | 84 | ||
| 71 | String tableNameCurr = (String) tableNameList.get(i); | 85 | String tableNameCurr = (String) tableNameList.get(i); |
| @@ -82,18 +96,198 @@ public class GenerateCode { | @@ -82,18 +96,198 @@ public class GenerateCode { | ||
| 82 | continue; | 96 | continue; |
| 83 | } | 97 | } |
| 84 | 98 | ||
| 85 | - ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | 99 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); |
| 86 | generateDto(srcPath, apiModel); | 100 | generateDto(srcPath, apiModel); |
| 101 | + } | ||
| 102 | + System.out.println("处理完成"); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + public static void generateEntity(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 106 | + //取数据库连接 | ||
| 107 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 108 | + //初始化要生成的表列表 | ||
| 109 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 110 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 111 | + | ||
| 112 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 113 | + | ||
| 114 | + //取得表中所有列 | ||
| 115 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 116 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 117 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 118 | + continue; | ||
| 119 | + } | ||
| 120 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 121 | + if (pkcolum == null) { | ||
| 122 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 123 | + continue; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 87 | generateEntity(srcPath, apiModel); | 127 | generateEntity(srcPath, apiModel); |
| 128 | + } | ||
| 129 | + System.out.println("处理完成"); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + public static void generateControllerManage(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 133 | + //取数据库连接 | ||
| 134 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 135 | + //初始化要生成的表列表 | ||
| 136 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 137 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 138 | + | ||
| 139 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 140 | + | ||
| 141 | + //取得表中所有列 | ||
| 142 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 143 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 144 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 145 | + continue; | ||
| 146 | + } | ||
| 147 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 148 | + if (pkcolum == null) { | ||
| 149 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 150 | + continue; | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 88 | generateManageController(srcPath, apiModel); | 154 | generateManageController(srcPath, apiModel); |
| 155 | + } | ||
| 156 | + System.out.println("处理完成"); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + public static void generateControllerV1(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 160 | + //取数据库连接 | ||
| 161 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 162 | + //初始化要生成的表列表 | ||
| 163 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 164 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 165 | + | ||
| 166 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 167 | + | ||
| 168 | + //取得表中所有列 | ||
| 169 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 170 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 171 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 172 | + continue; | ||
| 173 | + } | ||
| 174 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 175 | + if (pkcolum == null) { | ||
| 176 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 177 | + continue; | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 89 | generateV1Controller(srcPath, apiModel); | 181 | generateV1Controller(srcPath, apiModel); |
| 182 | + } | ||
| 183 | + System.out.println("处理完成"); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + public static void generateRepository(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 187 | + //取数据库连接 | ||
| 188 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 189 | + //初始化要生成的表列表 | ||
| 190 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 191 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 192 | + | ||
| 193 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 194 | + | ||
| 195 | + //取得表中所有列 | ||
| 196 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 197 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 198 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 199 | + continue; | ||
| 200 | + } | ||
| 201 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 202 | + if (pkcolum == null) { | ||
| 203 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 204 | + continue; | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 90 | generateRepository(srcPath, apiModel); | 208 | generateRepository(srcPath, apiModel); |
| 209 | + } | ||
| 210 | + System.out.println("处理完成"); | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + public static void generateService(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 214 | + //取数据库连接 | ||
| 215 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 216 | + //初始化要生成的表列表 | ||
| 217 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 218 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 219 | + | ||
| 220 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 221 | + | ||
| 222 | + //取得表中所有列 | ||
| 223 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 224 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 225 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 226 | + continue; | ||
| 227 | + } | ||
| 228 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 229 | + if (pkcolum == null) { | ||
| 230 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 231 | + continue; | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 91 | generateService(srcPath, apiModel); | 235 | generateService(srcPath, apiModel); |
| 236 | + } | ||
| 237 | + System.out.println("处理完成"); | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + public static void generateVo(String basePackage, String tableNameStr, String srcPath, String host, String port, String user, String password, String dbName) throws Exception{ | ||
| 241 | + //取数据库连接 | ||
| 242 | + Connection conn = getConnection(host, port, user, password, dbName); | ||
| 243 | + //初始化要生成的表列表 | ||
| 244 | + List<String> tableNameList = getListTableName(tableNameStr, dbName, conn); | ||
| 245 | + for (int i = 0; i < tableNameList.size(); i++) { | ||
| 246 | + | ||
| 247 | + String tableNameCurr = (String) tableNameList.get(i); | ||
| 248 | + | ||
| 249 | + //取得表中所有列 | ||
| 250 | + Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, dbName, tableNameCurr, "%", "mysql"); | ||
| 251 | + if (columnsInTable == null || columnsInTable.size() == 0) { | ||
| 252 | + System.out.println(tableNameCurr + " : 生成失败,得不到表中列明"); | ||
| 253 | + continue; | ||
| 254 | + } | ||
| 255 | + TableColumn pkcolum = TableColumn.getPKColum(columnsInTable); | ||
| 256 | + if (pkcolum == null) { | ||
| 257 | + System.out.println(tableNameCurr + " : 生成失败,未设置主键"); | ||
| 258 | + continue; | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + ApiModel apiModel = new ApiModel(dbName, tableNameCurr, columnsInTable, "1.0.0", basePackage); | ||
| 92 | generateVo(srcPath, apiModel); | 262 | generateVo(srcPath, apiModel); |
| 93 | } | 263 | } |
| 94 | System.out.println("处理完成"); | 264 | System.out.println("处理完成"); |
| 95 | } | 265 | } |
| 96 | - | 266 | + |
| 267 | + private static List<String> getListTableName(String tableNameStr, String dbName, Connection conn) throws Exception{ | ||
| 268 | + List<String> tableNameList = new ArrayList<String>(); | ||
| 269 | + if ("ALL".equalsIgnoreCase(tableNameStr) ) { | ||
| 270 | + String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+dbName+"'"; | ||
| 271 | + ResultSet rs = Tools.executeQuery(Tools.createStmt(conn), sql); | ||
| 272 | + if(rs == null){ | ||
| 273 | + throw new Exception("查询数据表名称失败"); | ||
| 274 | + } | ||
| 275 | + try { | ||
| 276 | + while (rs.next()) { | ||
| 277 | + tableNameList.add(rs.getString("Table_name")); | ||
| 278 | + } | ||
| 279 | + } catch (SQLException e) { | ||
| 280 | + e.printStackTrace(); | ||
| 281 | + } | ||
| 282 | + Tools.close(rs); | ||
| 283 | + } else { | ||
| 284 | + String[] TableNameArr = tableNameStr.split(","); | ||
| 285 | + for (int i = 0; i < TableNameArr.length; i++) | ||
| 286 | + tableNameList.add(TableNameArr[i]); | ||
| 287 | + } | ||
| 288 | + return tableNameList; | ||
| 289 | + } | ||
| 290 | + | ||
| 97 | private static String renderByTemplate(String templateName, ApiModel apiModel) throws Exception{ | 291 | private static String renderByTemplate(String templateName, ApiModel apiModel) throws Exception{ |
| 98 | Configuration cfg = new Configuration(); | 292 | Configuration cfg = new Configuration(); |
| 99 | //String comPath = GenerateCode.class.getResource("/").getPath(); | 293 | //String comPath = GenerateCode.class.getResource("/").getPath(); |