NodeJs構造MybatisダイナミックSQL

8238 ワード

下の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 + '
'
; 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 + '
'
; where = where + ifTest; }); where += whereEnd; console.log(where);