包含以下模块: - antdv-next-admin: Vue 3 + TypeScript + Ant Design Vue 管理后台 - 设备/许可证/配件/耗材 CRUD 管理页面 - 基础数据管理 (分类/位置/制造商/型号/供应商) - 业务管理 (故障报修/盘点/资产分配/资产申请/交易记录) - 下拉选项改造 (ID输入框 → 搜索下拉选择) - 资产状态字典化 (接入sys_dict系统) - 界面文案优化 (设备→资产, 在库/在用/维修中/已报废) - 修复 console 警告 (popupClassName, 重复组件注册) - our-itam: Java Spring Boot + magic-api 后端服务 - fantastic-admin: 前端底层框架 (pnpm monorepo) - ciyo-itasset: CIYO 资产模块 - magic-script-skill: Claude Code skill 定义 - .claude: 对话历史记录 Co-Authored-By: Claude Code <noreply@anthropic.com>
126 lines
2.3 KiB
Markdown
126 lines
2.3 KiB
Markdown
# 增删改查
|
||
|
||
## SQL参数
|
||
|
||
### #{} 注入参数
|
||
作用和`mybatis`一致,都是将`#{}`区域替换为占位符`?`
|
||
```javascript
|
||
var id = 123;
|
||
return db.select("""
|
||
select * from sys_user where id = #{id}
|
||
""");
|
||
// 运行时生成的SQL为:select * from sys_user where id = ?
|
||
```
|
||
|
||
### ${} 拼接参数
|
||
作用和`mybatis`一致,都是将`${}`区域替换为对应的字符串
|
||
```javascript
|
||
var id = 123;
|
||
return db.select("""
|
||
select * from sys_user where id = ${id}
|
||
""");
|
||
// 运行时生成的SQL为:select * from sys_user where id = 123
|
||
```
|
||
|
||
## 动态SQL参数
|
||
通过`?{condition,expression}`来实现动态拼接`SQL`
|
||
```javascript
|
||
return db.select("select * from sys_user ?{id,where id = #{id}}");
|
||
// 当id有值时,生成SQL:select * from sys_user where id = ?
|
||
// 当id无值时,生成SQL:select * from sys_user
|
||
```
|
||
|
||
## 切换数据源
|
||
```javascript
|
||
// 从数据源key定义为slave的库中查询
|
||
return db.slave.select("""
|
||
select * from sys_user
|
||
""")
|
||
```
|
||
|
||
## SQL缓存
|
||
```javascript
|
||
// 将查询结果缓存到名为user_cache的缓存中,有效期1小时
|
||
return db.cache("user_cache", 3600 * 1000).select("""
|
||
select * from sys_user
|
||
""")
|
||
```
|
||
|
||
## 使用事务
|
||
|
||
### 自动事务
|
||
```javascript
|
||
var val = db.transaction(()=>{
|
||
var v1 = db.update('...');
|
||
var v2 = db.update('....');
|
||
return v2;
|
||
});
|
||
return val;
|
||
```
|
||
|
||
### 手动事务
|
||
```javascript
|
||
var tx = db.transaction(); //开启事务
|
||
try{
|
||
var value = db.update('...');
|
||
tx.commit(); // 提交事务
|
||
return value;
|
||
}catch(e){
|
||
tx.rollback(); // 回滚事务
|
||
}
|
||
```
|
||
|
||
## Mybatis语法支持
|
||
|
||
### if
|
||
```javascript
|
||
var sql = """
|
||
select * from test_data
|
||
where 1 = 1
|
||
<if test="id != null">
|
||
and id = #{id}
|
||
</if>
|
||
"""
|
||
return db.select(sql)
|
||
```
|
||
|
||
### where
|
||
```javascript
|
||
var sql = """
|
||
select * from test_data
|
||
<where>
|
||
<if test="id != null">
|
||
and id = #{id}
|
||
</if>
|
||
</where>
|
||
"""
|
||
return db.select(sql)
|
||
```
|
||
|
||
### set、trim
|
||
```javascript
|
||
var sql = """
|
||
update test_data
|
||
<set>
|
||
<if test="name != null">
|
||
name = #{name}
|
||
</if>
|
||
</set>
|
||
where `id` = #{id}
|
||
"""
|
||
return db.update(sql)
|
||
```
|
||
|
||
### foreach
|
||
```javascript
|
||
var sql = """
|
||
select * from test_data
|
||
where id in
|
||
<foreach item='item' index='index' collection='body.ids'
|
||
open="(" separator="," close=")">
|
||
#{item}
|
||
</foreach>
|
||
"""
|
||
return db.select(sql)
|
||
```
|