基于@fastcar/core框架下对mysql的封装
快速安装
npm install @fastcar/mysql
结构说明
使用说明
-
配置mysql配置文件如下
- dataSoucreConfig: SqlConfig[];
- slowSQLInterval: number; //单位毫秒默认500毫秒会输出
- maximumConnectionReleaseTime?: number; //连接可用最大时长,防止因为忘记释放而被占用 单位毫秒
- printSQL: boolean; //是否打印sql
- sessionTimeOut: number; //会话超时
-
应用入口开启
import { EnableMysql } from "@fastcar/mysql/annotation";
@Application
@EnableMysql
class APP {
app!: FastCarApplication;
}
export default = new APP();
- 声明数据库映射文件(可以用@fastcar/mysql-tool进行逆向生成)
import { Size, NotNull, Table, Field, DBType, PrimaryKey } from "@fastcar/core/annotation";
import "reflect-metadata";
@Table("test")
class Test {
@Field("id")
@DBType("int")
@PrimaryKey
id!: number;
@Field("name")
@DBType("varchar")
@NotNull
@Size({ maxSize: 10 })
name!: string;
@Field("case_name")
@DBType("varchar")
@Size({ maxSize: 20 })
caseName!: string;
@Field("case_time")
@DBType("datetime")
caseTime!: Date;
@Field("flag")
@DBType("tinyint")
flag: boolean = true;
@Field("money")
@DBType("decimal")
money: number = 1.0;
constructor(...args: any) {
Object.assign(this, ...args);
}
}
export default Test;
import { Entity, Repository } from "@fastcar/core/annotation";
import { MysqlMapper } from "@fastcar/mysql";
import Test from "../model/Test";
@Entity(Test)
@Repository
class TestMapper extends MysqlMapper<Test> {}
export default TestMapper;
import { Autowired, Service } from "@fastcar/core/annotation";
import TestMapper from "../mapper/TestMapper";
import Test from "../model/Test";
@Service
class SimpleService {
@Autowired
myMapper!: TestMapper;
constructor() {}
async query() {
let res = await this.myMapper.selectOne({
where: {
name: {
value: "hello",
},
},
});
return res;
}
async saveUpdate() {
let test = new Test({ name: "ABC", caseTime: new Date() });
let res = await this.myMapper.saveORUpdate(test);
return res;
}
async saveOne() {
let test = new Test({ name: "aaa", caseTime: new Date(), money: 100000000 });
let res = await this.myMapper.saveOne(test);
return res;
}
async saveList() {
let test = new Test({ name: "bbb" });
let test2 = new Test({ name: "ccc" });
let res = await this.myMapper.saveList([test, test2]);
return res;
}
async update() {
let res = await this.myMapper.update({ where: { id: 1 }, row: { name: "ABCD" } });
return res;
}
async updateOne() {
let row = { name: "ABCDE" };
let res = await this.myMapper.updateOne({ where: { id: 1 }, row });
return res;
}
async updateByPrimaryKey() {
let test = new Test({ id: 1, name: "1234" });
let res = await this.myMapper.updateByPrimaryKey(test);
return res;
}
async selectOne() {
let res = await this.myMapper.selectOne({
where: {
name: "aaa",
caseTime: { ">=": "2022-01-11", "<=": "2022-02-12" },
},
});
return res;
}
async exist() {
let res = await this.myMapper.exist({
name: "124",
});
return res;
}
async count() {
let countNum = await this.myMapper.count({ id: 1 });
return countNum;
}
async delete() {
let res = await this.myMapper.delete({
where: {
name: "bbb",
},
});
return res;
}
async opeatorError() {
return await this.myMapper.execute("select * from noExistTable");
}
}
export default SimpleService;
import { DS, Entity, Repository } from "@fastcar/core/annotation";
import { MysqlMapper } from "@fastcar/mysql";
import Test from "../model/Test";
@Entity(Test)
@Repository
@DS("test2")
class TestMapper2 extends MysqlMapper<Test> {}
export default TestMapper2;
import { Autowired, Service } from "@fastcar/core/annotation";
import TestMapper from "../mapper/TestMapper";
import TestMapper2 from "../mapper/TestMapper2";
import Test from "../model/Test";
@Service
export default class TestDS {
@Autowired
myMapper!: TestMapper;
@Autowired
myMapper2!: TestMapper2;
async switchDS() {
await this.myMapper.saveOne(new Test({ name: "test", caseName: "数据源TEST1" }));
await this.myMapper2.saveOne(new Test({ name: "test", caseName: "数据源TEST2" }));
let result = await Promise.all([
this.myMapper.selectOne({
where: {
name: "test",
},
fields: ["caseName"],
}),
this.myMapper2.selectOne({
where: {
name: "test",
},
fields: ["caseName"],
}),
]);
return result;
}
}
import { Autowired, Service, SqlSession, Transactional } from "@fastcar/core/annotation";
import { MysqlDataSourceManager } from "@fastcar/mysql";
import TestMapper from "../mapper/TestMapper";
@Service
class TestTransactional {
@Autowired
myMapper!: TestMapper;
@Autowired
private dsm!: MysqlDataSourceManager;
async exec() {
let sql = "update test set flag = 0 where id = 1 ";
let sql2 = "update test set flag = 0 where id = 2";
try {
let res = await this.dsm.batchExecute([{ sql }, { sql: sql2 }]);
return res;
} catch (e) {
return null;
}
}
@Transactional()
async work(@SqlSession sessionId?: string) {
let res = await this.myMapper.updateOne(
{
where: { id: 1 },
row: { case_time: new Date() },
},
"",
sessionId
);
let sql2 = "select * from noExistTable";
await this.myMapper.execute(sql2, [], sessionId);
return res;
}
@Transactional()
async bacthExec(@SqlSession sessionId?: string) {
let res = await Promise.all([
this.myMapper.updateOne(
{
where: { id: 2 },
row: { case_time: new Date() },
},
"",
sessionId
),
this.myMapper.updateOne(
{
where: { id: 3 },
row: { case_time: new Date() },
},
"",
sessionId
),
this.myMapper.updateOne(
{
where: { id: 1 },
row: { case_time: new Date() },
},
"",
sessionId
),
]);
return res;
}
@Transactional()
async firstWork(@SqlSession sessionId?: string) {
await this.myMapper.updateOne(
{
where: { id: 2 },
row: { case_time: new Date() },
},
"",
sessionId
);
return await this.secondWork(sessionId);
}
@Transactional()
async secondWork(@SqlSession sessionId?: string) {
let res = await this.myMapper.updateOne(
{
where: { id: 3 },
row: { case_time: new Date() },
},
"",
sessionId
);
return res;
}
}
export default TestTransactional;
更多用法
参考项目git地址 @fastcar/mysql/test 下的example内
项目开源地址