Search in sources :

Example 1 with Table

use of com.hxkj.common.util.code_generator.Table in project my_curd by qinyou.

the class SysGeneratorController method generate.

/**
 * 生成代码 文件
 */
public void generate() {
    Ret ret = Ret.create();
    String jsonStr = getPara("jsonStr");
    if (StrKit.isBlank(jsonStr)) {
        ret.setFail();
        ret.set("msg", "jsonStr参数不能为空");
        renderJson(ret);
        return;
    }
    try {
        JSONObject jsonObject = JSON.parseObject(jsonStr, JSONObject.class);
        String moduleName = jsonObject.getString("moduleName");
        if (moduleName.contains(":)")) {
            ret.setFail();
            ret.set("msg", "jsonStr 中 moduleName 不合法");
            renderJson(ret);
            return;
        }
        JSONArray jsonArray = jsonObject.getJSONArray("tables");
        Iterator<Object> it = jsonArray.iterator();
        List<Table> tables = new ArrayList<Table>();
        while (it.hasNext()) {
            JSONObject jsonObjectTemp = (JSONObject) it.next();
            Table table = jsonObjectTemp.toJavaObject(Table.class);
            tables.add(table);
        }
        CodeGeneratorService codeGeneratorService = new CodeGeneratorService();
        List<String> outputPaths = codeGeneratorService.generate(moduleName, tables);
        ret.setOk();
        ret.put("outputPaths", outputPaths);
        renderJson(ret);
    } catch (Exception e) {
        LogKit.error(e.getMessage());
        ret.setFail();
        ret.set("msg", "系统发生异常。");
        renderJson(ret);
    }
}
Also used : Ret(com.jfinal.kit.Ret) Table(com.hxkj.common.util.code_generator.Table) JSONObject(com.alibaba.fastjson.JSONObject) CodeGeneratorService(com.hxkj.system.service.CodeGeneratorService) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) JSONObject(com.alibaba.fastjson.JSONObject)

Example 2 with Table

use of com.hxkj.common.util.code_generator.Table in project my_curd by qinyou.

the class CodeGeneratorService method generate.

/**
 * 生成代码 文件
 *
 * @param moduleName
 * @param tables
 * @return
 */
public List<String> generate(String moduleName, List<Table> tables) {
    List<String> outPathList = new ArrayList<String>();
    for (Table table : tables) {
        Map<String, Object> content = new HashMap<String, Object>();
        content.put("moduleName", moduleName);
        content.put("basePackageName", this.basePackageName);
        content.put("table", table);
        for (int i = 0; i < this.templates.length; i++) {
            String fileName;
            // .java 文件 使用 首字母大写的驼峰, 非java 文件使用驼峰命名
            if (fileNameWrapers[i].endsWith(".java")) {
                fileName = toOutPath(moduleName, this.paths[i]) + fileNameWrapers[i].replaceAll("@tableName@", table.getTableNameCamelFirstUp());
            } else {
                fileName = toOutPath(moduleName, this.paths[i] + File.separator + moduleName) + fileNameWrapers[i].replaceAll("@tableName@", table.getTableNameCamel());
            }
            fileName = fileName.replaceAll("\\\\", "/");
            LOG.debug("fileName:  " + fileName);
            ToolFreeMarker.makeHtml(tplDir, templates[i], content, fileName);
            outPathList.add(fileName);
        }
    }
    return outPathList;
}
Also used : Table(com.hxkj.common.util.code_generator.Table) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 3 with Table

use of com.hxkj.common.util.code_generator.Table in project my_curd by qinyou.

the class CodeGeneratorService method main.

public static void main(String[] args) {
    CodeGeneratorService service = new CodeGeneratorService();
    List<Table> tables = new ArrayList<Table>();
    String jsonStr = "{\n" + "\t\t\t\"tableName\": \"bus_novel\",\n" + "\t\t\t\"tableNameCamel\": \"busNovel\",\n" + "\t\t\t\"tableNameCamelFirstUp\": \"BusNovel\",\n" + "\t\t\t\"tableComment\": \"代码生成测试表--小说表\",\n" + "\t\t\t\"tablePrimaryKeys\": [\n" + "\t\t\t\t\"id\"\n" + "\t\t\t],\n" + "\t\t\t\"columnList\": [\n" + "\t\t\t\t{\n" + "\t\t\t\t\t\"columnName\": \"id\",\n" + "\t\t\t\t\t\"columnNameCamel\": \"id\",\n" + "\t\t\t\t\t\"columnNameCamelFirstUp\": \"Id\",\n" + "\t\t\t\t\t\"columnComment\": \"主键\",\n" + "\t\t\t\t\t\"columnDBType\": \"VARCHAR\",\n" + "\t\t\t\t\t\"columnJavaType\": \"java.lang.String\"\n" + "\t\t\t\t},\n" + "\t\t\t\t{\n" + "\t\t\t\t\t\"columnName\": \"title\",\n" + "\t\t\t\t\t\"columnNameCamel\": \"title\",\n" + "\t\t\t\t\t\"columnNameCamelFirstUp\": \"Title\",\n" + "\t\t\t\t\t\"columnComment\": \"书名\",\n" + "\t\t\t\t\t\"columnDBType\": \"VARCHAR\",\n" + "\t\t\t\t\t\"columnJavaType\": \"java.lang.String\"\n" + "\t\t\t\t},\n" + "\t\t\t\t{\n" + "\t\t\t\t\t\"columnName\": \"author\",\n" + "\t\t\t\t\t\"columnNameCamel\": \"author\",\n" + "\t\t\t\t\t\"columnNameCamelFirstUp\": \"Author\",\n" + "\t\t\t\t\t\"columnComment\": \"作者\",\n" + "\t\t\t\t\t\"columnDBType\": \"VARCHAR\",\n" + "\t\t\t\t\t\"columnJavaType\": \"java.lang.String\"\n" + "\t\t\t\t},\n" + "\t\t\t\t{\n" + "\t\t\t\t\t\"columnName\": \"create_time\",\n" + "\t\t\t\t\t\"columnNameCamel\": \"createTime\",\n" + "\t\t\t\t\t\"columnNameCamelFirstUp\": \"CreateTime\",\n" + "\t\t\t\t\t\"columnComment\": \"创建时间\",\n" + "\t\t\t\t\t\"columnDBType\": \"DATETIME\",\n" + "\t\t\t\t\t\"columnJavaType\": \"java.util.Date\"\n" + "\t\t\t\t}\n" + "\t\t\t]\n" + "\t\t}";
    Table table = JSON.parseObject(jsonStr, Table.class);
    tables.add(table);
    service.generate("bus", tables);
}
Also used : Table(com.hxkj.common.util.code_generator.Table) ArrayList(java.util.ArrayList)

Aggregations

Table (com.hxkj.common.util.code_generator.Table)3 ArrayList (java.util.ArrayList)3 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 CodeGeneratorService (com.hxkj.system.service.CodeGeneratorService)1 Ret (com.jfinal.kit.Ret)1 HashMap (java.util.HashMap)1