NodeJs构造Mybatis动态SQL

通过下面的sql查询所有的列

select TABLE_NAME,GROUP_CONCAT(COLUMN_NAME) from information_schema.columns where TABLE_SCHEMA = '数据库名' and table_name = '表名' group by TABLE_NAME;

点击这里选择NodeJs运行脚本

const str = 'thing_model_template_id,template_number,template_name,device_type_id,device_subtype_id,data_type_id,template_status,create_time,update_time,remark,bind_type_ids,language,schema,product_key,profile';
const prefix = 'tmtp.';
const suffix = ','
const join = " as ";
let strArray = str.split(',');
strArray.forEach((item, index) => {
    let column = prefix + item;
    let alias = item.replace(/\_(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
    console.log(column + join + alias + suffix);
});

const whereStart = '';
const whereEnd = '';
const objPrefix = '';
const ifTest0 = ';
const ifTest1 = '!=null">';
const ifTest2 = '';
const and = ' and ';
const ope = '=';
let where = whereStart + '\n';
strArray.forEach((item, index) => {
    let column = prefix + item;
    let alias = item.replace(/\_(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
    let obj = objPrefix + alias;
    let ifTest = ifTest0 + obj + ifTest1 + and + column + ope + '#{' + obj + '}' + ifTest2 + '\n';
    where = where + ifTest;
});
where += whereEnd;
console.log(where);

你可能感兴趣的:(nodejs)