六、Mybatis-动态SQL

动态SQl

1.if 标签

  <select id="xx" resultType="xxx">
        select * from user where 1=1
        <if test="name != null and name !=''">
            and name = #{name}
        </if>
        <if test="age != null and age != ''">
            and age = #{age}
        </if>


    </select>

2. where标签

 <select id="xxx">
        select * from user
    <where>
        <if test="name != null and name !=''">
            and name = #{name}
        </if>
        <if test="age != null and age != ''">
            and age = #{age}
        </if>
    </where>
    </select>

where会把 多余的 and,or,自动去掉。当where标签内没有内容时,就不会显示where

3.trim标签

  • prefix/suffix 将trim标签内的内容,前面/后面添加指定内容

  • prefixOverrides/suffixOverrides 将trim标签中内容前面或后面 去掉指定内容

  • 若标签中没有内容时,trim标签也没有任何效果

 <select id="xxxx">

    <!---
      prefix/suffix 将trim标签内的内容,前面/后面添加指定内容
      prefixOverrides/suffixOverrides 将trim标签中内容前面或后面 去掉指定内容
      若标签中没有内容时,trim标签也没有任何效果
    -->
        <trim prefix="where"   suffixOverrides="and | or">
            <if test="name != null and name !=''">
                and name = #{name}
            </if>
            <if test="age != null and age != ''">
                and age = #{age}
            </if>

        </trim>
    </select>

4.choose when otherwise 标签

choose when otherwise 相当于 if…else…if…else

所以 when 和 otherwise 只有一个条件执行

    <select id="xxxx">
        select * from user 
        <where>
            <choose>
                <when test="name != null and name != ''">
                    name = #{name}
                </when>
                
                <when test="age != null and age != null">
                    age = #{age}
                </when>

                <otherwise>
                    eid =1
                </otherwise>
            </choose>
        </where>
    </select>

5.foreach 标签

批量删除

  <delete id="xxxx">
        delete from user where id in(
            <foreach collection="ids" item="id" separator=",">
                #{id}
            </foreach>
            )
    </delete>

或者

 <delete id="xxxx">
        delete from user where id in
            <foreach collection="ids" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
            
    </delete>

或者

 <delete id="xxxx">
        delete from user where 
            <foreach collection="ids" item="id" separator="or" >
               id= #{id}
            </foreach>

</delete>

批量添加

<insert id="">
        insert into user values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.name},#{emp.sex},#{emp.age})
        </foreach>
    </insert>

相关推荐

  1. Mybatis-动态SQL

    2024-04-07 13:54:05       34 阅读
  2. MyBatis动态SQL

    2024-04-07 13:54:05       54 阅读
  3. Mybatis 动态 SQL - foreach

    2024-04-07 13:54:05       55 阅读
  4. MyBatis动态Sql

    2024-04-07 13:54:05       59 阅读
  5. MyBatis动态SQL语句

    2024-04-07 13:54:05       70 阅读
  6. MyBatis动态SQL

    2024-04-07 13:54:05       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-07 13:54:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-07 13:54:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-07 13:54:05       87 阅读
  4. Python语言-面向对象

    2024-04-07 13:54:05       96 阅读

热门阅读

  1. RabbitMQ

    RabbitMQ

    2024-04-07 13:54:05      48 阅读
  2. 【微信小程序】开发简介

    2024-04-07 13:54:05       43 阅读
  3. QT 创建线程的几种方法

    2024-04-07 13:54:05       53 阅读
  4. MySQL中的sql优化

    2024-04-07 13:54:05       45 阅读
  5. nginx配置多个服务

    2024-04-07 13:54:05       190 阅读
  6. MySQL 行锁和表锁是什么?区别,作用等学习总结

    2024-04-07 13:54:05       46 阅读