java 注解 装饰器_Node 注解(装饰器)(MyBatis注解版)

import TransactionMysql = require('transaction-mysql');

let pool = new TransactionMysql({

host: 'localhost',

port: 3306,

user: 'tujiawei',

password: '123456',

database: 'test'

});

// 注解

function Select(sql) {

return function (target, key: string, descriptor) {

descriptor.value = async function (...params) {

return await pool.exec(sql, params);

}

}

}

// 事务注解

const Transaction = function (target, key: string, descriptor) {

let originMethod = descriptor.value;

descriptor.value = async function () {

pool.begin();

await originMethod.apply(this, [pool]);

await pool.commit();

}

};

class Service {

conn;

@Select('select * from t_table')

async findAll() {}

@Select('select * from t_table where id = ?')

async findById(id) {}

@Select('update t_table set name = ? where id = ?')

async update(...args) {}

@Transaction

async updateTow(conn: any = {}) {

let item1: any = await this.findById(4);

let item2: any = await this.findById(3);

item1 = item1[0];

item2 = item2[0];

conn.add('update t_table set name = ? where id = ?', [item1.name, item2.id]).add('update t_table set name = ? where id = ?', [item2.name, item1.id]);

}

}

async function test() {

let service = new Service();

// await service.updateTow();

let list = await service.findAll();

console.log(list)

}

test();

// pool.begin().add('insert into t_table(name) values(?)', ['abc']).commit();

// new Service().findAll().then(list => {

// console.log(list);

// });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值