Mybatis动态SQL语句

动态SQL语句


if标签
例子:
<select id="listProduct" resultType="Product">
      select * from product
      <if test="name!=null">
             where name like concat('%',#{name},'%')
      </if>        
</select>

where标签
<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。
例子:
<select id="listProduct" resultType="Product">
    select * from product
    <where>
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>             
        <if test="price!=null and price!=0">
            and price > #{price}
        </if>    
    </where>         
</select>

set标签
与where标签类似,在update语句里也会碰到多个字段相关的问题
如果任何条件都不成立,sql语句就不会出现set关键字
如果有任何条件成立,set标签会自动去掉最后一个逗号
<update id="updateProduct" parameterType="Product" >
        update product
        <set>
            <if test="name != null">name=#{name},</if>
            <if test="price != null">price=#{price}</if>
        </set>
         where id=#{id}   
</update>

trim标签
trim 用来定制想要的功能
trim标签可以替换where和set标签:
prefixOverrides:前缀覆盖(去掉多余的前缀)
<trim prefix="where" prefixOverrides="and |or ">
      ... 
</trim>
suffixOverrides:后缀覆盖(去掉多余的后缀)
<trim prefix="SET" suffixOverrides=",">
  ...
</trim>
例子:
    <select id="listProduct" resultType="Product">
        select * from product
        <trim prefix="WHERE" prefixOverrides="AND |OR ">
            <if test="name!=null">
                and name like concat('%',#{name},'%')
            </if>        
            <if test="price!=null and price!=0">
                and price > #{price}
            </if>
        </trim>      
    </select>
     
    <update id="updateProduct" parameterType="Product" >
        update product
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name=#{name},</if>
            <if test="price != null">price=#{price}</if>
              
        </trim>
         
         where id=#{id}   
    </update>

choose标签:(if else的效果)
Mybatis里面没有else标签,但是可以使用when otherwise标签来达到这样的效果。
任何when条件成立,就进行条件查询,否则就使用otherwise条件查询
例子:
<select id="listProduct" resultType="Product">
      SELECT * FROM product 
      <where>
          <choose>
          <when test="name != null">
            and name like concat('%',#{name},'%')
          </when>              
          <when test="price !=null and price != 0">
            and price > #{price}
          </when>                      
            <otherwise>
                and id >1
            </otherwise>
          </choose>
      </where>
</select>

foreach标签
通常用于in 这样的语法里
例子:
    <select id="listProduct" resultType="Product">
          select * from product where id in
                <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
                    #{item}
                </foreach>
    </select>
调用的时候传入一个list集合的对象为参数

bind标签
就像是对传入的参数做一次字符串拼接,方便后续使用
例子:模糊查询,将传入的name前后拼接上%
        <select id="listProduct" resultType="Product">
            <bind name="likename" value="'%' + name + '%'" />
            select * from   product  where name like #{likename}
        </select>

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis是一个开源的持久层框架,它可以帮助开发者简化数据库操作的代码。MyBatis动态SQL语句是指在编写SQL语句时可以根据不同的条件动态生成不同的SQL语句MyBatis提供了以下几种方式来实现动态SQL语句: 1. if元素:通过if元素可以根据条件判断来生成不同的SQL语句片段。例如: ``` <select id="getUserList" parameterType="User" resultType="User"> SELECT * FROM user WHERE 1=1 <if test="name != null"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> </select> ``` 上述示例中,如果传入的User对象中name属性不为空,则会生成AND name = #{name}这段SQL语句。 2. choose、when、otherwise元素:通过choose、when、otherwise元素可以实现类似于switch语句的功能,根据不同的条件选择不同的SQL语句片段。例如: ``` <select id="getUserList" parameterType="User" resultType="User"> SELECT * FROM user WHERE 1=1 <choose> <when test="name != null"> AND name = #{name} </when> <when test="age != null"> AND age = #{age} </when> <otherwise> AND status = 'ACTIVE' </otherwise> </choose> </select> ``` 上述示例中,如果传入的User对象中name属性不为空,则会生成AND name = #{name}这段SQL语句;如果name为空而age不为空,则会生成AND age = #{age}这段SQL语句;如果name和age都为空,则会生成AND status = 'ACTIVE'这段SQL语句。 3. foreach元素:通过foreach元素可以实现对集合类型的参数进行遍历,并生成相应的SQL语句片段。例如: ``` <select id="getUserList" parameterType="List" resultType="User"> SELECT * FROM user WHERE id IN <foreach collection="list" item="id" open="(" separator="," close=")"> #{id} </foreach> </select> ``` 上述示例中,如果传入的List参数中包含[1, 2, 3]三个元素,则会生成SELECT * FROM user WHERE id IN (1, 2, 3)这段SQL语句。 这些是MyBatis动态SQL语句的几种常用方式,通过它们可以根据不同的条件生成不同的SQL语句,提高了SQL语句的灵活性和可复用性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值