Mybatis基于xml的动态sql实现

动态sql可以很方便的拼接sql语句,主要用于复合条件查询;

主要通过这几个标签实现:

if 标签:

where 标签

choose标签:

foreach标签:


if 标签:

1
2
3
4
5
6
7
8
9
<select id="selectStudentByIf" resultType="Student">
        select id,name,age,score from student where 1=1
        <if test="name != null and name != ''">
            and name like '%' #{name} '%'
        </if>
        <if test="age > 0">
            and age < #{age}
        </if>
 </select>

使用if标签判断参数,可以使用where 1=1,后面跟上if标签,这样可以避免因为‘and’导致sql语句冲突;

where 标签

1
2
3
4
5
6
7
8
9
10
11
<select id="selectStudentByWhere" resultType="Student">
        select id,name,age,score from student
        <where>
            <if test="name != null and name != ''">
                and name like '%' #{name} '%'
            </if>
            <if test="age > 0">
                and age < #{age}
            </if>
        </where>
 </select>

  

由于使用where 1=1 对查询性能也有一定的影响;可以使用where嵌套 if 标签,可以不需要使用上面“where 1=1”;

where标签会自动过滤掉if标签里sql语句的第一个and连接词;

作用:用来简化SQL语句中的where条件,可以嵌套其他标签;

choose标签:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<select id="selectStudentByChoose" resultType="Student">
        select id,name,age,score from student
        <where>
            <choose>
                <when test="name != null and name != ''">
                    name like '%' #{name} '%'
                </when>
                <when test="age > 0">
                    age < #{age}
                </when>
                <otherwise>
                    1 != 1
                </otherwise>
            </choose>
        </where>
  </select>

  

 表示:

当查询条件有name时,不管有没有age,都只按name查询;

当查询条件没有name时,才按age进行查询;

当name和age都没有时,查询不到任何数据。

作用:类似于switch语句,选择多个条件中的一个;

foreach标签:

foreach用来遍历,遍历的对象可以是数组,也可以是集合。

属性:

collection可以指定遍历对象的类型:array,list

item是指定的集合中每一个元素迭代时候的别名;

separator表示在每次进行迭代之间以什么符号作为分隔符;

open表示该语句以什么开始,close表示以什么结束;

 

1
2
3
4
5
6
7
8
9
<select id="selectStudentByForeachArray" resultType="Student">
        select id,name,age,score from student
         <if test="array != null and array.length > 0">
             where id in
             <foreach collection="array" item="id" open="(" close=")" separator=",">
                 #{id}
             </foreach>
         </if>
    </select>

  

表示遍历一个数组,迭代别名为id;

1
2
3
4
5
6
7
8
9
<select id="selectStudentByForeachList" resultType="Student">
    select id,name,age,score from student
     <if test="list != null and list.size > 0">
         where id in
         <foreach collection="list" item="id" open="(" close=")" separator=",">
             #{id}
         </foreach>
     </if>
</select>

  

遍历一个list集合;

1
2
3
4
5
6
7
8
9
<select id="selectStudentByForeachList2" resultType="Student">
    select id,name,age,score from student
     <if test="list != null and list.size > 0">
         where id in
         <foreach collection="list" item="stu" open="(" close=")" separator=",">
             #{stu.id}
         </foreach>
     </if>
</select>

遍历一个list,list中每个元素为对象;遍历对象的id属性;

posted on   passionConstant  阅读(874)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示