8.特殊SQL的执行
特殊SQL的执行
模糊查询
1 | |
1 | |
- 其中
select * from t_user where username like "%"#{mohu}"%"是最常用的
批量删除
- 只能使用${},如果使用#{},则解析后的sql语句为
delete from t_user where id in ('1,2,3'),这样是将1,2,3看做是一个整体,只有id为1,2,3的数据会被删除。正确的语句应该是delete from t_user where id in (1,2,3),或者delete from t_user where id in ('1','2','3')
1 | |
1 | |
1 | |
动态设置表名
- 只能使用${},因为表名不能加单引号
1 | |
1 | |
添加功能获取自增的主键
使用场景
t_clazz(clazz_id,clazz_name)
- t_student(student_id,student_name,clazz_id)
- 添加班级信息
- 获取新添加的班级的id
- 为班级分配学生,即将某学的班级id修改为新添加的班级的id
在mapper.xml中设置两个属性
useGeneratedKeys:设置使用自增的主键
- keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
1 | |
1 | |
1 | |
1 | |
8.特殊SQL的执行
http://example.com/2021/04/11/8.特殊SQL的执行/