首页 > 代码库 > entity模板

entity模板

importPackage(java.util);
importPackage(java.io);
importPackage(com.ravali.dbschema.data);

/**
 * 初始化入口
 */
function init(dataModel) {
    var _dataModel = dataModel;
    var cnt = 0;
    if (_dataModel) {
        cnt = _dataModel.getTables().size();
    }
    gen("c:\\entity.java", _dataModel);
    return "" + _dataModel.toString();
}

/**
 * 生成代码的主函数
 */
function gen(outFileName, dataModel) {
    var outFileStr = "";
    outFileStr += genClassName(dataModel);
    outFileStr += "\r\n";

    var cnt = dataModel.getTables().size();
    for (var i = 0; i < cnt; i++) {
        var table = dataModel.getTables().get(i);
        //outFileStr += table.getName() + "\r\n";
        var columns = table.getColumns();
        var gettersetter = "";
        for (var j = 0; j < columns.size(); j++) {
            var c = columns.get(j);
            var oriType = "" + c.getType();
            var comment = "";
            if (oriType == "NUMBER") {
                comment = oriType + "(" + c.getPrecision() + ", " + c.getScale() + ")";
            } else {
                comment = oriType + "(" + c.getLength() + ")";
            }
            var type = convertDbTypeToJavaType(c.getType(), c.getLength(), c.getPrecision(), c.getScale()); 
            
            var camelVarName = toCamel("" + c.getName());
            /*生成私有变量  begin*/
            outFileStr += "    /**" + "\r\n";
            outFileStr += "    * " + c.getComments() + "\r\n";
            outFileStr += "    */" + "\r\n";
            outFileStr += "    private " + type + "    " + camelVarName + ";    //" + comment + "\r\n\r\n";
            /*生成私有变量  end*/
            
            /*生成getter, setter方法 begin*/
            gettersetter += "    public " + type +" get" + toFirstUpCamel("" + c.getName()) + "() {"+ "\r\n";
            gettersetter += "        return " + camelVarName + ";"+ "\r\n";
            gettersetter += "    }"+ "\r\n"+ "\r\n";
            
            gettersetter += "    public void set" + toFirstUpCamel("" + c.getName()) + "(" + type +" " + camelVarName + ") {"+ "\r\n";
            gettersetter += "        this." + camelVarName + " = "+ genAssign(camelVarName, c.getType(), c.getLength(), c.getPrecision(), c.getScale()) + ";\r\n";
            gettersetter += "    }"+ "\r\n"+ "\r\n";
            /*生成getter, setter方法 end*/
        }
        /*生成完整的输出*/
        outFileStr += "\r\n";
        outFileStr += gettersetter;
    }

    outFileStr += genClassEnd();

    writeToFile(outFileName, outFileStr);
}

function genClassName(dataModel) {
    var str = "";
    var pre = "";
    var cnt = dataModel.getTables().size();
    for (var i = 0; i < cnt; i++) {
        var table = dataModel.getTables().get(i);
        str += pre + table.getName();
        pre = "_";
    }
    str = str.toLowerCase();
    str = str.charAt(0).toUpperCase() + str.slice(1);


    //return "public class " + str +"Entity{";
    return "public class " + str +"{";
}

function genAssign(varName, dbDataType, length, precision, scale){
    var retVarName = varName;
    
    var _dbDataType = "" + dbDataType.toString().toUpperCase();

    switch (_dbDataType) {
    case "VARCHAR":
    case "VARCHAR2":
        retVarName = varName + "==null ? null : " + varName + ".trim()" ;
        break;
    }
    
    return retVarName;
}

function genClassEnd() {
    return "}";
}

/*****************************************************************************/
function toFirstUpCamel(str){
    var camel = toCamel(str);
    return camel.charAt(0).toUpperCase() + camel.slice(1);
}

function toCamel(str){
return str.toLowerCase()
        // Replaces any - or _ characters with a space
        .replace(/[-_]+/g, ‘ ‘)
        // Removes any non alphanumeric characters
        .replace(/[^\w\s]/g, ‘‘)
        // Uppercases the first character in each group immediately following a space
        // (delimited by spaces)
        .replace(/ (.)/g, function($1) {
            return $1.toUpperCase();
        })
        // Removes spaces
.replace(/ /g, ‘‘);
}

function toCamel_1(str) {
    str = str.toLowerCase();
    var re = /_(\w)/g;
    return str.replace(re, function () {
        var args = arguments;
        return args[1].toUpperCase();
    })
}

function toUnderline(s) {
    return s.replace(/([A-Z])/g, "_$1").toLowerCase();
}

function convertDbTypeToJavaType(dbDataType, length, precision, scale) {

    var retType = "String";
    var _dbDataType = "" + dbDataType.toString().toUpperCase();

    switch (_dbDataType) {
    case "VARCHAR":
    case "VARCHAR2":
        retType = "String";
        break;
    case "NUMBER":
        if (scale > 0 || precision > 24) {
            retType = "BigDecimal";
        } else if (precision > 10) {
            retType = "Long";
        } else if (precision > 4) {
            retType = "Integer";
        } else if (precision == 1) {
            retType = "Boolean";
        } else {
            retType = "Integer";
        }
        break;
    case "DATE":
        retType = "Date";
        break;
    }

    //    if(_dbType == "VARCHAR"){
    //        retType = "String";
    //    } else if(_dbType == "VARCHAR2"){
    //        retType = "String";
    //    }  else if(_dbType == "NUMBER"){
    //        retType = "int";
    //    }

    return retType;
}

function writeToFile(fileName, content) {
    var file = new File(fileName);
    file.createNewFile();
    var writerStream = new FileOutputStream(file);

    var writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
    writer.write(content);
    writer.close();
}

 

entity模板