动态SQL基本标签
在MyBatis中提供了一些标签,能够便于程序员动态拼写SQL语句。
MyBatis的标签:
    if:判断
    choose (when, otherwise):分支选择;带了break的swtich-case 如果带了id就用id查,如果带了name就用name查;只会进入其中一个
    trim 字符串截取(where(封装查询条件), set(封装修改条件))
    foreach 遍历集合
if标签
if标签顾名思义就是用来判断的标签,能够动态的判断程序传过来的值符不符合条件来进行动态拼接SQL。
这里还引用了where标签,where标签就相当于SQL语句中的where,update更新语句下也可以使用set标签。
1 | <!-- public List<Student> getStudentDynamicIF(Student student); 接口方法--> |
choose标签
具有内部标签(when, otherwise):是一个分支选择;相当于带了break的swtich-case
1 | <!-- public List<Student> getStudentDynamicChoose(Student student); 接口方法--> |
trim标签
字符串截取,常用于SQL语句的一些连接符和结束标志。
1 | <!-- public List<Student> getStudentDynamicTrim(Student student); --> |
foreach标签
用于遍历集合的标签,可以做很多事情,例如批量查询,批量更新,批量删除。
批量查询
1 | <!-- public List<Student> getStudentDynamicforeach(List<Integer> list); 接口方法--> |
批量保存
1 | <!-- 批量保存 --> |