动态SQL

动态拼写SQL是MyBatis中很重要的一个内容。

动态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
2
3
4
5
6
7
8
9
<!-- public List<Student> getStudentDynamicIF(Student student); 接口方法-->
<select id="getStudentDynamicIF" resultType="entity.Student">
select * from user <where>
<if test="id!=null">id=#{id}</if>
<!-- 双引号这用单引号 -->
<if test="name!=null and name!=''">and name like #{name}</if>
<if test="userclass!=null">and userclass=#{userclass}</if>
</where>
</select>

choose标签

具有内部标签(when, otherwise):是一个分支选择;相当于带了break的swtich-case

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- public List<Student> getStudentDynamicChoose(Student student); 接口方法-->
<select id="getStudentDynamicChoose" resultType="entity.Student">
select * from user
<where>
<!-- 如果带了id就用id查,如果带了name就用name查;只会进入其中一个 -->
<choose>
<when test="id!=null">id=#{id}</when>
<when test="name!=null and name!=''">name like #{name}</when>
<!-- 其它条件均不符合的时候 -->
<otherwise>userclass=2</otherwise>
</choose>
</where>
</select>

trim标签

字符串截取,常用于SQL语句的一些连接符和结束标志。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- public List<Student> getStudentDynamicTrim(Student student); -->
<select id="getStudentDynamicTrim" resultType="entity.Student">
select * from user
<!-- 后面多出的and或者or where标签不能解决
prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。
prefix给拼串后的整个字符串加一个前缀
prefixOverrides="":
前缀覆盖: 去掉整个字符串前面多余的字符
suffix="":后缀
suffix给拼串后的整个字符串加一个后缀
suffixOverrides=""
后缀覆盖:去掉整个字符串后面多余的字符
-->
<!-- 自定义字符串的截取规则 -->
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">id=#{id} and</if>
<!-- 双引号这用单引号 -->
<if test="name!=null and name!=''"> name like #{name} and</if>
<if test="userclass!=null"> userclass=#{userclass}</if>
</trim>
</select>

foreach标签

用于遍历集合的标签,可以做很多事情,例如批量查询,批量更新,批量删除。
批量查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- public List<Student> getStudentDynamicforeach(List<Integer> list); 接口方法-->
<select id="getStudentDynamicforeach" resultType="entity.Student">
select * from user
<!--
collection:指定要遍历的集合:
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值

#{变量名}就能取出变量的值也就是当前遍历出的元素
-->
<foreach collection="list" item="list_id" separator=","
open="where id in(" close=")">
#{list_id}
</foreach>
</select>

批量保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!-- 批量保存 -->
<!--public void addStudent(@Param("stu")List<Student> stu); -->
<!--MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法-->
<insert id="addStudent">
insert into user(name,sex,userclass)
values
<foreach collection="stu" item="stus" separator=",">
(#{stus.name},#{stus.sex},#{stus.userclass})
</foreach>
</insert>

<!-- 这种方式需要数据库连接属性allowMultiQueries=true;
这种分号分隔多个sql可以用于其他的批量操作(删除,修改) -->
<insert id="addStudent">
<foreach collection="stu" item="stus" separator=";">
insert into user(name,sex,userclass)
values(#{stus.name},#{stus.sex},#{stus.userclass})
</foreach>
</insert>

# sql标签
抽取可重用的sql片段。方便后面引用
```Xml
<!--
抽取可重用的sql片段。方便后面引用
1、sql抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用
2、include来引用已经抽取的sql:
3、include还可以自定义一些property,sql标签内部就能使用自定义的属性
include-property:取值的正确方式${prop},
#{不能使用这种方式}
-->
<sql id="insertColumn">
<if test="_databaseId=='oracle'">
name,sex,user_class
</if>
<if test="_databaseId=='mysql'">
name,sex,userclass
</if>
</sql>

<!-- 引用例子 -->
<insert id="addStudent">
insert into user(
<include refid="insertColum">
<property name="testColomn" value="abc"/> <!-- 可以定义值 -->
</include>
)
values
<foreach collection="stu" item="stus" separator=",">
(#{stus.name},#{stus.sex},#{stus.userclass})
</foreach>
</insert>
原创技术分享,您的支持将鼓励我继续创作
0%