Mybatis常用动态SQL标签

<where> 标签

作用

智能处理 WHERE 子句,自动去除多余的 AND 或 OR

基本语法

java 复制代码
<where>
    SQL条件...
</where>

<if> 标签

作用

根据条件判断是否包含某段SQL

基本语法

java 复制代码
<if test="condition">
    SQL片段
</if>

<choose>, <when>, <otherwise> 标签

作用

类似 Java 的 switch-case 语句,从多个条件中选择一个

基本语法

java 复制代码
<choose>
    <when test="condition1">SQL1</when>
    <when test="condition2">SQL2</when>
    <otherwise>默认SQL</otherwise>
</choose>
java 复制代码
  <select id="selectConditionList" resultType="com.java.vo.GroupVO"
            parameterType="com.java.vo.filter.GroupFilterVO">
        SELECT count(b.sfp_user_id) people,a.id,a.group_name groupName, a.group_accord_name groupAccordName,a.app_id
        appId,a.create_by createBy,
        a.create_time createTime
        FROM user_group a LEFT JOIN group_user b on b.user_group_id = a.id
        <where>
            a.app_id = #{appId}
            <choose>
                <when test="onlyMine">
                    and a.create_by=#{userNm}
                    <choose>
                        <when test="keyword != null and keyword !=''">
                            and a.group_accord_name like CONCAT('%',#{keyword},'%')
                        </when>
                    </choose>
                </when>
                <otherwise>
                    <choose>
                        <when test="keyword != null and keyword !=''">
                            and (a.create_by like CONCAT('%',#{keyword},'%') or a.group_accord_name like
                            CONCAT('%',#{keyword},'%'))
                        </when>
                    </choose>
                </otherwise>
            </choose>
        </where>
        GROUP BY a.group_name , a.group_accord_name ,a.app_id , a.create_by ,a.create_time ,a.id
        ORDER BY
        <choose>
            <when test='peopleAscDesc == "0"'>
                people DESC,createTime DESC
            </when>
            <when test='peopleAscDesc == "1"'>
                people ASC,createTime DESC
            </when>
            <otherwise>
                createTime DESC
            </otherwise>
        </choose>
    </select>

<case> 标签

作用

mapper.xml文件中,需要直接给查询的数据直接赋值或者别名显示的时候需要用到CASE WHEN THEN

java 复制代码
select case 
  when total > max then 2 
  when total < min then 1 
else 0
end
from ta 

<set> 标签

作用

智能处理 UPDATE 语句中的 SET 子句,自动去除末尾逗号

java 复制代码
<!-- 动态更新用户信息 -->
<update id="updateUserSelective" parameterType="User">
    UPDATE users
    <set>
        <if test="name != null">
            name = #{name},
        </if>
        <if test="email != null">
            email = #{email},
        </if>
        <if test="phone != null">
            phone = #{phone}
        </if>
    </set>
    WHERE id = #{id}
</update>

<foreach> 标签

作用

遍历集合,通常用于 IN 条件或批量操作

属性说明

  • collection: 要遍历的集合
  • item: 每个元素的变量名
  • index: 索引变量名
  • open: 开始符号,只会拼接一次
  • close: 结束符号,只会拼接一次
  • separator: 分隔符。foreach 标签不会错误地添加多余的分隔符,也就是最后一次迭代不会加分隔符
java 复制代码
<!-- IN 查询 -->
<select id="selectUsersInIds" parameterType="list" resultType="User">
    SELECT * FROM users 
    WHERE id IN
    <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
    </foreach>
</select>

<!-- 批量插入 -->
<insert id="batchInsertUsers" parameterType="list">
    INSERT INTO users (name, email, age) VALUES
    <foreach collection="list" item="user" separator=",">
            (#{user.name}, #{user.email}, #{user.age})
    </foreach>
</insert>

<trim> 标签

作用

比 where 和 set 更灵活的标签,可自定义前后缀

属性说明

  • prefix: 添加前缀
  • suffix: 添加后缀
  • prefixOverrides: 去除的前缀
  • suffixOverrides: 去除的后缀
java 复制代码
<!-- trim替代where -->
<select id="selectByTrim" parameterType="map" resultType="User">
    SELECT * FROM users
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
    <if test="name != null">
        AND name = #{name}
    </if>
    <if test="email != null">
        AND email = #{email}
    </if>
</select>

<!-- trim替代set -->
<update id="updateByTrim" parameterType="User">
    UPDATE users
    <trim prefix="SET" suffixOverrides=",">
        <if test="name != null">
            name = #{name},
        </if>
    </trim>
</update>
相关推荐
CoderYanger9 分钟前
递归、搜索与回溯-综合练习:27.黄金矿工
java·算法·leetcode·深度优先·1024程序员节
vx_vxbs6623 分钟前
【SSM高校普法系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
android·java·python·mysql·小程序·php·idea
张较瘦_25 分钟前
Springboot3 | ResponseEntity 完全使用教程
java·springboot·开发
毕设源码-郭学长25 分钟前
【开题答辩全过程】以 高校兼职系统为例,包含答辩的问题和答案
java·spring boot
黄嚯嚯27 分钟前
Jackson 多态反序列化详解:基于字段自动选择子类的优雅方案
java
h***381833 分钟前
SpringBoot - Cookie & Session 用户登录及登录状态保持功能实现
java·spring boot·后端
一只乔哇噻37 分钟前
java后端工程师+AI大模型进修ing(研一版‖day57)
java·开发语言·人工智能·算法·语言模型
十五喵38 分钟前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·智慧物业管理系统
Williams1044 分钟前
Java POI/Excel工具:终结OOM、精度丢失和i18n三大难题
java·开发语言·excel
k***81721 小时前
IDEA搭建SpringBoot,MyBatis,Mysql工程项目
spring boot·intellij-idea·mybatis