├── .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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.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 | #list>
27 |
28 | @bstable>
29 | @bslist>
--------------------------------------------------------------------------------
/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 | 
48 |
49 | (微信捐助)
50 |
51 | 
--------------------------------------------------------------------------------
/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