码迷,mamicode.com
首页 > 数据库 > 详细

动态SQL语句

时间:2018-12-10 22:04:43      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:reac   date   pre   自动   rom   例子   前缀   动态sql   list   

动态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>

动态SQL语句

标签:reac   date   pre   自动   rom   例子   前缀   动态sql   list   

原文地址:https://www.cnblogs.com/snzd9958/p/10099028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!