├── .gitignore ├── lib ├── velocity-1.7-dep.jar └── mysql-connector-java-5.1.23-bin.jar ├── src └── com │ └── uikoo9 │ └── gcode │ ├── db.properties │ ├── tmp │ ├── ftl-search.vm │ ├── ftl-input.vm │ ├── ftl-index.vm │ ├── Model.vm │ └── Controller.vm │ ├── util │ ├── QUtil.java │ ├── QDbUtil.java │ ├── QGenCodeUtil.java │ └── QStringUtil.java │ └── gui │ ├── QGenerateCodeFrame.form │ └── QGenerateCodeFrame.java ├── .project ├── .classpath ├── .settings └── org.eclipse.jdt.core.prefs ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /lib/velocity-1.7-dep.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uikoo9/jfinalQ-gencode/HEAD/lib/velocity-1.7-dep.jar -------------------------------------------------------------------------------- /lib/mysql-connector-java-5.1.23-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uikoo9/jfinalQ-gencode/HEAD/lib/mysql-connector-java-5.1.23-bin.jar -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/db.properties: -------------------------------------------------------------------------------- 1 | db.driver=com.mysql.jdbc.Driver 2 | db.url=jdbc:mysql://localhost:3306/db_com 3 | db.username=root 4 | db.password=uikoo7 -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/tmp/ftl-search.vm: -------------------------------------------------------------------------------- 1 | <\#include "/WEB-INF/view/base/inc.html"/> 2 | 3 |
4 | #foreach($col in $cols)#if($col.get("colname") != "cdate" && $col.get("colname") != "cuser_name" && $col.get("colname") != "cuser_id")<@bsinput title='$col.get("remarks")' name='row.$col.get("colname")'/>#end 5 | 6 | #end 7 | 8 |
-------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | jfinalq_02_gcode 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/tmp/ftl-input.vm: -------------------------------------------------------------------------------- 1 | <\#include "/WEB-INF/view/base/inc.html"/> 2 | 3 |
4 | 5 | #foreach($col in $cols)#if($col.get("colname") != "cdate" && $col.get("colname") != "cuser_name" && $col.get("colname") != "cuser_id")<@bsinput title='$col.get("remarks")' name='row.$col.get("colname")' value='${(row.$col.get("colname"))!}'/>#end 6 | 7 | #end 8 | 9 |
-------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.6 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.6 12 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/tmp/ftl-index.vm: -------------------------------------------------------------------------------- 1 | <\#include "/WEB-INF/view/base/inc.html"/> 2 | 3 | <@bslist qpage=qpage> 4 | <@bstable> 5 | 6 | 7 | 8 | #foreach($col in $cols)#if($col.get("colname") != "cuser_name" && $col.get("colname") != "cuser_id")$col.get("remarks")#end 9 | 10 | #end 11 | 12 | 操作 13 | 14 | 15 | 16 | <#list qpage.list?if_exists as row> 17 | 18 | 19 | #foreach($col in $cols)#if($col.get("colname") != "cuser_name" && $col.get("colname") != "cuser_id")${(row.$col.get("colname"))!}#end 20 | 21 | #end 22 | <@bsbutton size='xs' icon='pencil' class='editbtn'/> 23 | <@bsbutton size='xs' icon='remove' class='delbtn'/> 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 uikoo9 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/tmp/Model.vm: -------------------------------------------------------------------------------- 1 | package com.uikoo9.manage.${pname}.model; 2 | 3 | import java.util.List; 4 | 5 | import com.jfinal.plugin.activerecord.Model; 6 | import com.uikoo9.util.core.annotation.QTable; 7 | import com.uikoo9.util.core.data.QStringUtil; 8 | 9 | /** 10 | * ${cname}Model
11 | * id id
12 | #foreach($col in $cols) 13 | * $col.get("colname") $col.get("remarks")
14 | #end 15 | * @author qiaowenbin 16 | */ 17 | @QTable("${tablename}") 18 | @SuppressWarnings("serial") 19 | public class ${cname}Model extends Model<${cname}Model>{ 20 | 21 | public static final ${cname}Model dao = new ${cname}Model(); 22 | 23 | /** 24 | * find all 25 | * @return 26 | */ 27 | public List<${cname}Model> findAll(){ 28 | return findAll(null); 29 | } 30 | 31 | /** 32 | * find all by order 33 | * @param order 34 | * @return 35 | */ 36 | public List<${cname}Model> findAll(String order){ 37 | StringBuilder sb = new StringBuilder("select * from ${tablename} "); 38 | if(QStringUtil.isEmpty(order)){ 39 | return dao.find(sb.append("order by id desc").toString()); 40 | }else{ 41 | return dao.find(sb.append(order).toString()); 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/tmp/Controller.vm: -------------------------------------------------------------------------------- 1 | package com.uikoo9.manage.${pname}.controller; 2 | 3 | import com.uikoo9.manage.${pname}.model.${cname}Model; 4 | import com.uikoo9.util.core.annotation.QControllerUrl; 5 | import com.uikoo9.util.plugin.json.QJsonUtil; 6 | import com.uikoo9.z.jfinal.QController; 7 | 8 | /** 9 | * ${cname}Controller 10 | * @author qiaowenbin 11 | */ 12 | @QControllerUrl("${url}") 13 | public class ${cname}Controller extends QController{ 14 | 15 | /** 16 | * 跳转到首页 17 | */ 18 | public void index(){ 19 | setAttr("qpage", list(${cname}Model.class)); 20 | render("/WEB-INF/view/manage/${pname}/${ftl}-index.html"); 21 | } 22 | 23 | /** 24 | * 跳转到搜索页 25 | */ 26 | public void search(){ 27 | render("/WEB-INF/view/manage/${pname}/${ftl}-search.html"); 28 | } 29 | 30 | /** 31 | * 跳转到保存修改页 32 | */ 33 | public void savep(){ 34 | setAttr("row", getRow(${cname}Model.class)); 35 | render("/WEB-INF/view/manage/${pname}/${ftl}-input.html"); 36 | } 37 | 38 | /** 39 | * 保存或修改 40 | */ 41 | public void save(){ 42 | String validate = validate(); 43 | if(validate == null){ 44 | renderJson(save(${cname}Model.class)); 45 | }else{ 46 | renderJson(QJsonUtil.error(validate)); 47 | } 48 | } 49 | 50 | /** 51 | * 删除一条或多条 52 | */ 53 | public void del(){ 54 | renderJson(del(${cname}Model.class)); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jfinalQ-gencode 2 | ========================================== 3 | 1. jfinalQ代码生成工具 4 | 2. 采用velocity模版技术 5 | 3. 可以多表同时生成 6 | 7 | [jfinalQ](http://uikoo9.com/jfinalQ) 8 | --- 9 | 1. 基于[jfinal2.0](http://www.jfinal.com/),易学,开发快速,功能强大 10 | 2. 基于[bootstrap3.x](http://v3.bootcss.com/),简洁美观,完美适配移动端 11 | 3. [jfinalQ](https://github.com/uikoo9/jfinalQ):jfinalQ最简化示例 12 | 4. [jfinalQ-gencode](https://github.com/uikoo9/jfinalQ-gencode):jfinalQ自带代码生成工具 13 | 5. [jfinalQ-encrypt](https://github.com/uikoo9/jfinalQ-encrypt):jfinalQ自带tomcat加密部署工具 14 | 6. [bootstrapQ](http://uikoo9.com/bootstrapQ):jfinalQ自带bootstrap增强组件 15 | 16 | 表命名规范 17 | --- 18 | 1. 数据库规范:db\_开头,例如:db\_blog 19 | 2. 表名规范:t\_开头,中间为模块名,结尾为功能名,例如:t\_ucenter\_user 20 | 3. 字段规范:模块名\_表名\_开头,字段结尾,例如:ucenter\_user\_name 21 | 4. 必备字段: 22 | 1. id:id,int,10,not null,pk,ac 23 | 2. 创建日期:cdate,datetime,not null 24 | 3. 创建人id:cuser_id,int,10,not null 25 | 4. 创建人姓名:cuser_name,varchar,200,not null 26 | 27 | 开始使用 28 | --- 29 | 1. 将src和lib复制到一个java project中 30 | 2. 修改db.properties中的数据库连接信息 31 | 3. 运行com.uikoo9.gcode.gui.QGenerateCodeFrame 32 | 4. 点击浏览按钮,选择要保存代码的文件夹 33 | 5. 选择要生成代码的表,点击生成 34 | 6. 将生成的代码直接拷贝到项目下 35 | 36 | 注意 37 | --- 38 | 1. 可以通过修改com.uikoo9.tmp下的模版文件修改生成模版 39 | 2. 为了适应32位,64位,linux等操作系统,所以没有生成jar 40 | 41 | 捐助 42 | --- 43 | 希望得到您的捐助: 44 | 45 | (支付宝捐助) 46 | 47 | ![zhifubao](http://uikoo9.qiniudn.com/@/img/donate/zhifu2.png) 48 | 49 | (微信捐助) 50 | 51 | ![weixin](http://uikoo9.qiniudn.com/@/img/donate/zhifu1.png) -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/util/QUtil.java: -------------------------------------------------------------------------------- 1 | package com.uikoo9.gcode.util; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | import java.io.OutputStreamWriter; 8 | import java.net.URLDecoder; 9 | import java.util.Map; 10 | import java.util.Properties; 11 | 12 | import org.apache.velocity.VelocityContext; 13 | import org.apache.velocity.app.Velocity; 14 | import org.apache.velocity.texen.util.FileUtil; 15 | 16 | /** 17 | * 通用工具类 18 | * @author qiaowenbin 19 | */ 20 | public class QUtil { 21 | 22 | /** 23 | * jar包得到自身的路径 24 | * @return 25 | */ 26 | public static String getJarPath() { 27 | String res = null; 28 | 29 | try { 30 | res = URLDecoder.decode(QUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); 31 | } catch (Exception e) { 32 | e.printStackTrace(); 33 | } 34 | 35 | return res; 36 | } 37 | 38 | /** 39 | * 读取配置文件 40 | * @param in 41 | * @return 42 | */ 43 | public static Properties readProperties(String path){ 44 | Properties p = new Properties(); 45 | try { 46 | p.load(QUtil.class.getResourceAsStream(path)); 47 | } catch (IOException e) { 48 | e.printStackTrace(); 49 | } 50 | 51 | return p; 52 | } 53 | 54 | /** 55 | * 生成代码 by velocity 56 | * @param map 变量 57 | * @param destPath 目的地址 58 | * @param destFile 目的文件名 59 | * @param tmpPath 模版地址 60 | * @param tmpFile 模版文件名 61 | * @return 62 | */ 63 | public static boolean generateCodeByVelocity(Map map, String destPath, String destFile, String tmpPath, String tmpFile){ 64 | try { 65 | // 1.初始化 66 | Properties properties = new Properties(); 67 | properties.put("file.resource.loader.path", tmpPath); 68 | properties.put("input.encoding", "UTF-8"); 69 | properties.put("output.encoding", "UTF-8"); 70 | Velocity.init(properties); 71 | VelocityContext context = new VelocityContext(map); 72 | 73 | // 2.生成代码 74 | FileUtil.mkdir(destPath); 75 | BufferedWriter sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(destPath, destFile)), "UTF-8")); 76 | Velocity.getTemplate(tmpFile).merge(context, sw); 77 | sw.flush(); 78 | sw.close(); 79 | 80 | return true; 81 | } catch (Exception e) { 82 | e.printStackTrace(); 83 | return false; 84 | } 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/util/QDbUtil.java: -------------------------------------------------------------------------------- 1 | package com.uikoo9.gcode.util; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.ResultSet; 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Properties; 11 | 12 | /** 13 | * 数据库工具类 14 | * @author qiaowenbin 15 | */ 16 | public class QDbUtil { 17 | 18 | /** 19 | * get con by path 20 | * @param path 数据库配置文件地址 21 | * @return 22 | * @throws Exception 23 | */ 24 | public static Connection getCon(String path) throws Exception{ 25 | Properties properties = QUtil.readProperties(path); 26 | String dbDriver = properties.getProperty("db.driver"); 27 | String dbUrl = properties.getProperty("db.url"); 28 | String dbUsername = properties.getProperty("db.username"); 29 | String dbPassword = properties.getProperty("db.password"); 30 | 31 | Class.forName(dbDriver); 32 | return DriverManager.getConnection(dbUrl, dbUsername, dbPassword); 33 | } 34 | 35 | /** 36 | * close con 37 | * @param con 连接 38 | * @throws Exception 异常 39 | */ 40 | public static void closeCon(Connection con) throws Exception{ 41 | if(con != null) con.close(); 42 | } 43 | 44 | /** 45 | * get table info to map 46 | * @param tableName 47 | * @return 48 | */ 49 | public static Map getTableInfoMap(String dbPath, String tableName){ 50 | Map info = new HashMap(); 51 | 52 | Connection con = null; 53 | try { 54 | try{ 55 | if(QStringUtil.notEmpty(dbPath) && QStringUtil.notEmpty(tableName)){ 56 | con = QDbUtil.getCon(dbPath); 57 | 58 | String cname = QStringUtil.getClassNameFromTableName(tableName); 59 | info.put("pname", tableName.split("_")[1]); 60 | info.put("cname", cname); 61 | info.put("vname", QStringUtil.firstLower(cname)); 62 | info.put("url", tableName.replace("_", "/").substring(1)); 63 | info.put("ftl", tableName.replace("_", "-").substring(2)); 64 | info.put("tablename", tableName); 65 | info.put("pkname", QDbUtil.getPkName(con, tableName)); 66 | info.put("cols", QDbUtil.getCols(con, tableName)); 67 | } 68 | }finally{ 69 | QDbUtil.closeCon(con); 70 | } 71 | } catch (Exception e) { 72 | e.printStackTrace(); 73 | } 74 | 75 | return info; 76 | } 77 | 78 | /** 79 | * get pk name 80 | * @param con 81 | * @param tableName 82 | * @return 83 | */ 84 | public static String getPkName(Connection con, String tableName) throws Exception{ 85 | ResultSet pkrs = con.getMetaData().getPrimaryKeys(null, null, tableName); 86 | return pkrs.next() ? pkrs.getString("COLUMN_NAME") : null; 87 | } 88 | 89 | 90 | /** 91 | * get cols info to map list 92 | * @param con 93 | * @param tableName 94 | * @return 95 | * @throws Exception 96 | */ 97 | public static List> getCols(Connection con, String tableName) throws Exception{ 98 | List> cols = new ArrayList>(); 99 | 100 | String pkName = getPkName(con, tableName); 101 | ResultSet colrs = con.getMetaData().getColumns(null, "%", tableName, "%"); 102 | while(colrs.next()){ 103 | if(QStringUtil.notEmpty(pkName) && !pkName.equals(colrs.getString("COLUMN_NAME"))){ 104 | Map col = new HashMap(); 105 | col.put("colname", colrs.getString("COLUMN_NAME")); 106 | col.put("coltype", colrs.getString("TYPE_NAME")); 107 | col.put("remarks", colrs.getString("REMARKS")); 108 | 109 | cols.add(col); 110 | } 111 | } 112 | 113 | return cols; 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/util/QGenCodeUtil.java: -------------------------------------------------------------------------------- 1 | package com.uikoo9.gcode.util; 2 | 3 | import java.io.File; 4 | import java.sql.Connection; 5 | import java.sql.ResultSet; 6 | import java.util.Map; 7 | import java.util.Vector; 8 | 9 | import javax.swing.table.DefaultTableModel; 10 | 11 | /** 12 | * 代码生成-业务代码 13 | * @author qiaowenbin 14 | */ 15 | public class QGenCodeUtil { 16 | 17 | public static final String dbPath = "/com/uikoo9/gcode/db.properties"; 18 | 19 | /** 20 | * 填充表格-获取数据库表信息 21 | */ 22 | public static void fillTable(DefaultTableModel model){ 23 | Connection con = null; 24 | 25 | try { 26 | try { 27 | con = QDbUtil.getCon(dbPath); 28 | ResultSet rs = con.getMetaData().getTables(null,null,"",null); 29 | while(rs.next()){ 30 | Vector v = new Vector(); 31 | v.add(0, ""); 32 | v.add(1, rs.getString("TABLE_NAME")); 33 | 34 | model.addRow(v); 35 | } 36 | } finally { 37 | QDbUtil.closeCon(con); 38 | } 39 | } catch (Exception e) { 40 | e.printStackTrace(); 41 | } 42 | } 43 | 44 | /** 45 | * 生成一个表对应的代码 46 | * @param tableName 47 | * @param destBasePath 48 | * @return 49 | */ 50 | public static String genCodeFromTable(String tableName, String destBasePath){ 51 | StringBuilder res = new StringBuilder(); 52 | 53 | String tmpBasePath = QUtil.getJarPath() + File.separator + "com" + File.separator + "uikoo9"; 54 | try { 55 | Connection con = null; 56 | try { 57 | con = QDbUtil.getCon(dbPath); 58 | res.append(generateJfinalCode(tableName, destBasePath, tmpBasePath)); 59 | } finally { 60 | QDbUtil.closeCon(con); 61 | } 62 | } catch (Exception e) { 63 | e.printStackTrace(); 64 | } 65 | 66 | return res.toString(); 67 | } 68 | 69 | /** 70 | * 生成一个表具体的代码for jfinal 71 | * @param tableName 72 | * @param destBasePath 73 | * @param tmpBasePath 74 | * @return 75 | */ 76 | public static String generateJfinalCode(String tableName, String destBasePath, String tmpBasePath){ 77 | StringBuilder sb = new StringBuilder(); 78 | 79 | String folderName = tableName.split("_")[1]; 80 | String ftlName = tableName.replace("_", "-").substring(2); 81 | String className = QStringUtil.getClassNameFromTableName(tableName); 82 | 83 | Map map = QDbUtil.getTableInfoMap(dbPath, tableName); 84 | String tmpPath = tmpBasePath + File.separator + "gcode" + File.separator + "tmp"; 85 | String destFTLPath = 86 | destBasePath + File.separator + 87 | "WebRoot" + File.separator + 88 | "WEB-INF" + File.separator + 89 | "view" + File.separator + 90 | "manage" + File.separator + 91 | folderName; 92 | String destSRCPath = 93 | destBasePath + File.separator + 94 | "src" + File.separator + 95 | "com" + File.separator + 96 | "uikoo9" + File.separator + 97 | "manage" + File.separator + 98 | folderName + File.separator; 99 | 100 | boolean indexFtl = QUtil.generateCodeByVelocity(map, destFTLPath, ftlName + "-index.html", tmpPath, "ftl-index.vm"); 101 | boolean inputFtl = QUtil.generateCodeByVelocity(map, destFTLPath, ftlName + "-input.html", tmpPath, "ftl-input.vm"); 102 | boolean searchFtl = QUtil.generateCodeByVelocity(map, destFTLPath, ftlName + "-search.html", tmpPath, "ftl-search.vm"); 103 | boolean model = QUtil.generateCodeByVelocity(map, destSRCPath + "model", className + "Model.java", tmpPath, "Model.vm"); 104 | boolean controller = QUtil.generateCodeByVelocity(map, destSRCPath + "controller", className + "Controller.java", tmpPath, "Controller.vm"); 105 | 106 | sb.append("generate jfinal code begin...\r\n"); 107 | sb.append("jfinal index.html生成" + (indexFtl ? "成功" : "失败") + "!\r\n"); 108 | sb.append("jfinal input.html生成" + (inputFtl ? "成功" : "失败") + "!\r\n"); 109 | sb.append("jfinal search.html生成" + (searchFtl ? "成功" : "失败") + "!\r\n"); 110 | sb.append("jfinal model生成" + (model ? "成功" : "失败") + "!\r\n"); 111 | sb.append("jfinal controller生成"+ (controller ? "成功" : "失败") + "!\r\n"); 112 | sb.append("generate jfinal code end...\r\n"); 113 | 114 | return sb.toString(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/gui/QGenerateCodeFrame.form: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 |
110 |
111 |
112 |
113 |
114 |
115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
128 |
129 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/util/QStringUtil.java: -------------------------------------------------------------------------------- 1 | package com.uikoo9.gcode.util; 2 | 3 | import java.io.PrintWriter; 4 | import java.io.StringWriter; 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | import java.util.regex.Pattern; 9 | 10 | /** 11 | * 字符串工具类
12 | * 1.判空
13 | * 2.判非空
14 | * 3.判任一空
15 | * 4.判都非空
16 | * 5.判在数组内
17 | * 6.判不在数组内
18 | * 7.to str
19 | * 8.to int
20 | * 9.to boolean
21 | * 10.to html
22 | * 11.from html
23 | * 12.from exception
24 | * 13.判email
25 | * 14.判n位数字
26 | * 15.首字母大写
27 | * 16.首字母小写
28 | * 17.分隔字符串
29 | * 18.分隔字符串返回最后一个字符串
30 | * 19.将驼峰命名的字符串拆分
31 | * 20.将表名转换为类名
32 | * 21.将类名转换为表名
33 | * @author qiaowenbin 34 | * @version 0.1.0.20141209 35 | * @history 36 | * 0.1.0.20141209
37 | * 0.0.12.20141121
38 | * 0.0.11.20141117
39 | * 0.0.10.20141109
40 | * 0.0.9.20141027
41 | * 0.0.8.20141015
42 | * 0.0.7.20141009
43 | * 0.0.6.20140926
44 | */ 45 | public class QStringUtil { 46 | 47 | /** 48 | * 判空 49 | * @param s 字符串 50 | * @return 判断结果 51 | */ 52 | public static boolean isEmpty(String s){ 53 | return s == null || s.trim().equals("") ? true : false; 54 | } 55 | 56 | /** 57 | * 判非空 58 | * @param s 字符串 59 | * @return 判断结果 60 | */ 61 | public static boolean notEmpty(String s){ 62 | return s != null && !s.trim().equals("") ? true : false; 63 | } 64 | 65 | /** 66 | * 判任一空 67 | * @param ss 68 | * @return 69 | */ 70 | public static boolean anyoneEmpty(String... ss){ 71 | if(ss.length == 0) return true; 72 | 73 | for(String s : ss){ 74 | if(isEmpty(s)) return true; 75 | } 76 | 77 | return false; 78 | } 79 | 80 | /** 81 | * 判都非空 82 | * @param ss 83 | * @return 84 | */ 85 | public static boolean allNotEmpty(String... ss){ 86 | if(ss.length == 0) return false; 87 | 88 | for(String s : ss){ 89 | if(isEmpty(s)) return false; 90 | } 91 | 92 | return true; 93 | } 94 | 95 | /** 96 | * 判在数组内 97 | * @param s 98 | * @param ss 99 | * @return 100 | */ 101 | public static boolean isIn(String s, String... ss){ 102 | if(ss != null){ 103 | for(String str : ss){ 104 | if(str.equals(s)){ 105 | return true; 106 | } 107 | } 108 | } 109 | 110 | return false; 111 | } 112 | 113 | /** 114 | * 判不在数组内 115 | * @param s 116 | * @param ss 117 | * @return 118 | */ 119 | public static boolean notIn(String s, String... ss){ 120 | return !isIn(s, ss); 121 | } 122 | 123 | /** 124 | * to str 125 | * @param s 126 | * @return 127 | */ 128 | public static String toStr(String s){ 129 | return s == null ? "" : s; 130 | } 131 | 132 | /** 133 | * to int 134 | * @param s 135 | * @return 136 | */ 137 | public static int toInt(String s){ 138 | return isEmpty(s) ? 0 : Integer.parseInt(s); 139 | } 140 | 141 | /** 142 | * to boolean 143 | * @param s 144 | * @return 145 | */ 146 | public static Boolean toBoolean(String s){ 147 | if(s != null){ 148 | s = s.toLowerCase(); 149 | 150 | if(isIn(s, "yes", "t", "1")){ 151 | return true; 152 | } 153 | if(isIn(s, "no", "f", "0")){ 154 | return false; 155 | } 156 | } 157 | 158 | return null; 159 | } 160 | 161 | /** 162 | * to html 163 | * @param str 164 | * @return 165 | */ 166 | public static String toHtml(String str) { 167 | if (str == null || str.length() == 0) { 168 | return str; 169 | } 170 | return str.replaceAll("<", "<").replaceAll(">", ">") 171 | .replaceAll("&", "&").replaceAll(""", "\"") 172 | .replaceAll(" ", " "); 173 | } 174 | 175 | /** 176 | * from html 177 | * @param str 178 | * @return 179 | */ 180 | public static String fromHtml(String str) { 181 | if (str == null || str.length() == 0) { 182 | return str; 183 | } 184 | StringBuffer buf = new StringBuffer(); 185 | char ch = ' '; 186 | for (int i = 0; i < str.length(); i++) { 187 | ch = str.charAt(i); 188 | if (ch == '<') { 189 | buf.append("<"); 190 | } else if (ch == '>') { 191 | buf.append(">"); 192 | } else if (ch == '&') { 193 | buf.append("&"); 194 | } else if (ch == '"') { 195 | buf.append("""); 196 | } else if (ch == ' ') { 197 | buf.append(" "); 198 | } else { 199 | buf.append(ch); 200 | } 201 | } 202 | return buf.toString(); 203 | } 204 | 205 | /** 206 | * from exception 207 | * @param exception 208 | * @return 209 | */ 210 | public static String fromException(Exception exception){ 211 | try { 212 | StringWriter sw = new StringWriter(); 213 | PrintWriter pw = new PrintWriter(sw); 214 | 215 | exception.printStackTrace(pw); 216 | 217 | pw.close(); 218 | sw.close(); 219 | 220 | return sw.toString(); 221 | } catch (Exception e) { 222 | e.printStackTrace(); 223 | } 224 | 225 | return null; 226 | } 227 | 228 | /** 229 | * 判email 230 | * @param s 231 | * @return 232 | */ 233 | public static boolean isEmail(String s){ 234 | String pattern = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"; 235 | return Pattern.compile(pattern).matcher(s).find(); 236 | } 237 | 238 | /** 239 | * 判n位数字 240 | * @param s 241 | * @param n 242 | * @return 243 | */ 244 | public static boolean isNLengthNumber(String s, int n){ 245 | String pattern = "^\\d{" + n + "}$"; 246 | return Pattern.compile(pattern).matcher(s).find(); 247 | } 248 | 249 | /** 250 | * 首字母大写 251 | * @param s 252 | * @return 253 | */ 254 | public static String firstUpper(String s){ 255 | StringBuilder sb = new StringBuilder(s); 256 | 257 | sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); 258 | 259 | return sb.toString(); 260 | } 261 | 262 | /** 263 | * 首字母小写 264 | * @param s 265 | * @return 266 | */ 267 | public static String firstLower(String s){ 268 | StringBuilder sb = new StringBuilder(s); 269 | 270 | sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); 271 | 272 | return sb.toString(); 273 | } 274 | 275 | /** 276 | * 分隔字符串 277 | * @param s 待分隔的字符串 278 | * @param split 分隔符 279 | * @return 分隔后的字符串List 280 | */ 281 | public static List splitToList(String s, String split){ 282 | if(s == null) return null; 283 | 284 | return new ArrayList(Arrays.asList(s.split(split))); 285 | } 286 | 287 | /** 288 | * 分隔字符串返回最后一个字符串 289 | * @param str 290 | * @param split 291 | * @return 292 | */ 293 | public static String splitAndReturnLastString(String str, String split){ 294 | if(isEmpty(str)){ 295 | return ""; 296 | }else{ 297 | String[] ss = str.split(split); 298 | return ss[ss.length - 1]; 299 | } 300 | } 301 | 302 | /** 303 | * 将驼峰命名的字符串拆分 304 | * @param s 305 | * @return 306 | */ 307 | public static List tuoFeng(String s){ 308 | List res = new ArrayList(); 309 | 310 | if(QStringUtil.notEmpty(s)){ 311 | StringBuilder sb = new StringBuilder(); 312 | for(int i=0; i names = QStringUtil.tuoFeng(clazzName); 355 | for(String s: names){ 356 | sb.append("_" + s.toLowerCase()); 357 | } 358 | 359 | return sb.toString(); 360 | } 361 | 362 | } 363 | -------------------------------------------------------------------------------- /src/com/uikoo9/gcode/gui/QGenerateCodeFrame.java: -------------------------------------------------------------------------------- 1 | /* 2 | * QGenerateCodeFrame.java 3 | * 4 | * Created on __DATE__, __TIME__ 5 | */ 6 | 7 | package com.uikoo9.gcode.gui; 8 | 9 | import java.awt.Component; 10 | 11 | import javax.swing.JCheckBox; 12 | import javax.swing.JOptionPane; 13 | import javax.swing.JTable; 14 | import javax.swing.table.DefaultTableModel; 15 | import javax.swing.table.TableCellRenderer; 16 | 17 | import com.uikoo9.gcode.util.QGenCodeUtil; 18 | import com.uikoo9.gcode.util.QStringUtil; 19 | 20 | /** 21 | * 22 | * @author __USER__ 23 | */ 24 | @SuppressWarnings("serial") 25 | public class QGenerateCodeFrame extends javax.swing.JFrame { 26 | 27 | /** Creates new form QGenerateCodeFrame */ 28 | public QGenerateCodeFrame() { 29 | initComponents(); 30 | this.setLocationRelativeTo(null); 31 | QGenCodeUtil.fillTable((DefaultTableModel) jTable1.getModel()); 32 | } 33 | 34 | /** This method is called from within the constructor to 35 | * initialize the form. 36 | * WARNING: Do NOT modify this code. The content of this method is 37 | * always regenerated by the Form Editor. 38 | */ 39 | //GEN-BEGIN:initComponents 40 | // 41 | private void initComponents() { 42 | 43 | jFileChooser1 = new javax.swing.JFileChooser(); 44 | jLabel1 = new javax.swing.JLabel(); 45 | jTextField1 = new javax.swing.JTextField(); 46 | jButton1 = new javax.swing.JButton(); 47 | jButton2 = new javax.swing.JButton(); 48 | jScrollPane1 = new javax.swing.JScrollPane(); 49 | jTable1 = new javax.swing.JTable(); 50 | jScrollPane2 = new javax.swing.JScrollPane(); 51 | jTextArea1 = new javax.swing.JTextArea(); 52 | 53 | jFileChooser1 54 | .setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY); 55 | 56 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 57 | setTitle("gcode-v2.0.0"); 58 | 59 | jLabel1.setText("\u4fdd\u5b58\u5230"); 60 | 61 | jTextField1.setEditable(false); 62 | 63 | jButton1.setText("\u6d4f\u89c8"); 64 | jButton1.addActionListener(new java.awt.event.ActionListener() { 65 | public void actionPerformed(java.awt.event.ActionEvent evt) { 66 | jButton1ActionPerformed(evt); 67 | } 68 | }); 69 | 70 | jButton2.setText("\u751f\u6210"); 71 | jButton2.addActionListener(new java.awt.event.ActionListener() { 72 | public void actionPerformed(java.awt.event.ActionEvent evt) { 73 | jButton2ActionPerformed(evt); 74 | } 75 | }); 76 | 77 | jTable1.setModel(new javax.swing.table.DefaultTableModel( 78 | new Object[][] { 79 | 80 | }, new String[] { "选择(按住ctrl多选)", "表名" }) { 81 | boolean[] canEdit = new boolean[] { false, false }; 82 | 83 | public boolean isCellEditable(int rowIndex, int columnIndex) { 84 | return canEdit[columnIndex]; 85 | } 86 | }); 87 | jTable1.getColumnModel().getColumn(0) 88 | .setCellRenderer(new TableCellRenderer() { 89 | @Override 90 | public Component getTableCellRendererComponent( 91 | JTable table, Object value, boolean isSelected, 92 | boolean hasFocus, int row, int column) { 93 | JCheckBox ck = new JCheckBox(); 94 | ck.setSelected(isSelected); 95 | ck.setHorizontalAlignment((int) 0.5f); 96 | return ck; 97 | } 98 | }); 99 | jScrollPane1.setViewportView(jTable1); 100 | 101 | jTextArea1.setColumns(20); 102 | jTextArea1.setRows(5); 103 | jScrollPane2.setViewportView(jTextArea1); 104 | 105 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout( 106 | getContentPane()); 107 | getContentPane().setLayout(layout); 108 | layout.setHorizontalGroup(layout 109 | .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 110 | .addGroup( 111 | javax.swing.GroupLayout.Alignment.TRAILING, 112 | layout.createSequentialGroup() 113 | .addContainerGap() 114 | .addGroup( 115 | layout.createParallelGroup( 116 | javax.swing.GroupLayout.Alignment.TRAILING) 117 | .addComponent( 118 | jScrollPane2, 119 | javax.swing.GroupLayout.Alignment.LEADING, 120 | javax.swing.GroupLayout.DEFAULT_SIZE, 121 | 394, Short.MAX_VALUE) 122 | .addGroup( 123 | javax.swing.GroupLayout.Alignment.LEADING, 124 | layout.createSequentialGroup() 125 | .addComponent( 126 | jLabel1) 127 | .addPreferredGap( 128 | javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 129 | .addComponent( 130 | jTextField1, 131 | javax.swing.GroupLayout.DEFAULT_SIZE, 132 | 208, 133 | Short.MAX_VALUE) 134 | .addPreferredGap( 135 | javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 136 | .addComponent( 137 | jButton1) 138 | .addPreferredGap( 139 | javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 140 | .addComponent( 141 | jButton2)) 142 | .addComponent( 143 | jScrollPane1, 144 | javax.swing.GroupLayout.Alignment.LEADING, 145 | javax.swing.GroupLayout.DEFAULT_SIZE, 146 | 394, Short.MAX_VALUE)) 147 | .addContainerGap())); 148 | layout.setVerticalGroup(layout 149 | .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 150 | .addGroup( 151 | layout.createSequentialGroup() 152 | .addContainerGap() 153 | .addGroup( 154 | layout.createParallelGroup( 155 | javax.swing.GroupLayout.Alignment.BASELINE) 156 | .addComponent(jLabel1) 157 | .addComponent(jButton2) 158 | .addComponent(jButton1) 159 | .addComponent( 160 | jTextField1, 161 | javax.swing.GroupLayout.PREFERRED_SIZE, 162 | javax.swing.GroupLayout.DEFAULT_SIZE, 163 | javax.swing.GroupLayout.PREFERRED_SIZE)) 164 | .addPreferredGap( 165 | javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 166 | .addComponent(jScrollPane1, 167 | javax.swing.GroupLayout.PREFERRED_SIZE, 168 | 170, 169 | javax.swing.GroupLayout.PREFERRED_SIZE) 170 | .addPreferredGap( 171 | javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 172 | .addComponent(jScrollPane2, 173 | javax.swing.GroupLayout.DEFAULT_SIZE, 174 | 148, Short.MAX_VALUE).addContainerGap())); 175 | 176 | pack(); 177 | }// 178 | //GEN-END:initComponents 179 | 180 | private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 181 | String destBasePath = this.jTextField1.getText(); 182 | 183 | if (QStringUtil.isEmpty(destBasePath)) { 184 | JOptionPane.showMessageDialog(this, 185 | "\u8BF7\u9009\u62E9\u76EE\u6807\u6587\u4EF6\u5939\uFF01"); 186 | return; 187 | } 188 | if (jTable1.getSelectedRowCount() == 0) { 189 | JOptionPane 190 | .showMessageDialog(this, 191 | "\u8BF7\u9009\u62E9\u8981\u751F\u6210\u4EE3\u7801\u7684\u8868\uFF01"); 192 | return; 193 | } 194 | 195 | for (int i : jTable1.getSelectedRows()) { 196 | String tableName = (String) jTable1.getModel().getValueAt(i, 1); 197 | String generateCodeRes = QGenCodeUtil.genCodeFromTable( 198 | tableName, destBasePath); 199 | jTextArea1.append(generateCodeRes); 200 | } 201 | } 202 | 203 | private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 204 | this.jFileChooser1.showDialog(this, "\u9009\u62E9\u6587\u4EF6\u5939"); 205 | this.jTextField1.setText(jFileChooser1.getSelectedFile().toString()); 206 | } 207 | 208 | /** 209 | * @param args the command line arguments 210 | */ 211 | public static void main(String args[]) { 212 | java.awt.EventQueue.invokeLater(new Runnable() { 213 | public void run() { 214 | new QGenerateCodeFrame().setVisible(true); 215 | } 216 | }); 217 | } 218 | 219 | //GEN-BEGIN:variables 220 | // Variables declaration - do not modify 221 | private javax.swing.JButton jButton1; 222 | private javax.swing.JButton jButton2; 223 | private javax.swing.JFileChooser jFileChooser1; 224 | private javax.swing.JLabel jLabel1; 225 | private javax.swing.JScrollPane jScrollPane1; 226 | private javax.swing.JScrollPane jScrollPane2; 227 | private javax.swing.JTable jTable1; 228 | private javax.swing.JTextArea jTextArea1; 229 | private javax.swing.JTextField jTextField1; 230 | // End of variables declaration//GEN-END:variables 231 | 232 | } --------------------------------------------------------------------------------