# 增删改查

# 查询List

var sql = "select * from sys_user";    
return db.select(sql);  //返回List
1
2

# 动态参数

当执行SQL需要动态参数时,可以使用#{} ${} ?{} 来表示

# #{expression}

#{expression}的作用是把参数替换成占位符?,与mybatis中的#{}作用一致

return db.select('select * from sys_user where id = #{id}');
// 最终生成的sql为:select * from sys_user where id = ?
1
2

# ${expression}

#{expression}的作用是把参数拼接到SQL中,与mybatis中的${}作用一致

return db.select("select * from sys_user where id = '${id}'");
// 最终生成的sql为:select * from sys_user where id = '123'
1
2

# ?{condition,expression}

?{condition,expression}的作用是先判断,如果条件成立则拼接后部分内容SQL中,与mybatis中的if标签基本一致

return db.select("select * from sys_user ?{id,where id = #{id}}");
// 当id有值时切,生成SQL:select * from sys_user where id = ?`,相当于mybatis中的<if test="id != nulla nd id != ''">
// 当id无值时,生成SQL:select * from sys_user
return db.select("select * from sys_user ?{id!=null&&id.length() > 3,where id = #{id}}");
// 当id!=null&&id.length() > 3判断为true时,生成SQL:`select * from sys_user where id = ?
// 当判断为false时,生成SQL:select * from sys_user

## 查询数量
```javascript
var sql = "select count(*) from sys_user";
return db.selectInt(sql);   //返回int
1
2
3
4
5
6
7
8
9
10
11

# 查询Map

var sql = "select * from sys_user limit 1";
return db.selectOne(sql);   //返回Map
1
2

# 查询值

//#{id}表示从请求参数或PathVariable中获取参数
var sql = "select username from sys_user where id = #{id}";    
return db.selectValue(sql); //返回String 
1
2
3

# 增删改

//#{id}表示从请求参数或PathVariable中获取参数
var sql = "delete from sys_user where id = #{id}";    
return db.update(sql); //返回int 
1
2
3
上次更新: 7/5/2020, 11:04:54 AM