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 = '<where>';
const whereEnd = '</where>';
const objPrefix = '';
const ifTest0 = '<if test="';
const ifTest1 = '!=null">';
const ifTest2 = '</if>';
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);

猜你喜欢

转载自blog.csdn.net/qq_30038111/article/details/107820894
今日推荐