Mybatis学习笔记(二) 动态SQL

mybatis可以在xml中结合一些标签,对sql语句进行动态处理。

IF标签

昨天咱写查询的时候,有查询所有listProduct和模糊查询listProductByName 两个方法,在调用的时候就需要分别调用他们。

如果使用if标签,就可以通过判断你有没有name参数,有就模糊查询,没有就查询所有。

就实现了一条sql语句应付多种情况。

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

where标签

如果存在多条件需要判断的情况,比如:

<select id="listProduct" resultType="Product">
	select * from product_
	<if test="name!=null">
		where name like concat('%',#{name},'%')
	</if>		 	
	<if test="price!=0">
		and price > #{price}
	</if>		 	
</select>

就会出现一些情况,如果name是空的,那么我们的sql语句就会变成

select * from product_ and price > #{price}

这样肯定是执行不了的。

那么这中多条件的问题就可以使用where标签来处理。

如下面的代码所示,

如果where标签里面的if全都不成立,那么咱的sql语句就不会出现where

如果有任何一个成立了,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标签

咱在update功能的时候sql语句会有set,他的功能和where一样的,也是依据内部的if是否有成立的,判断set出不出现。

要注意的是在set内部if,连接不是用的and或者or,那么咱需要自己打上“,”来连接。

比如下面的第一个if里面有个 逗号。

<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的功能性就更加强大,类似之前的resultMap和resultType的区别,可以 用来定制想要的功能,比如where标签就可以用

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>

set可以用:

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>
 

CHOOSE、WHEN、OTHERWISE标签

mybatis里面没有else标签,但是可以用when、otherwise来实现ifelse的功能

如果任何一个whenok,那么久使用对应的内容,否则就用otherwise中的内容。

要注意的是这里和switch case里面加了break一样,任何一个满足就会跳出来了。

<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标签

循环collection里面的内容。

可以设置格式。比如下面就用open separator close 设置好了循环开始之前、间隔、末尾的格式,填充中间部分就行了。

<select id="listProduct" resultType="Product">
          SELECT * FROM product_
            WHERE ID in
                <foreach item="item" index="index" collection="list"
                    open="(" separator="," close=")">
                    #{item}
                </foreach>
    </select>

bind标签

bind标签就像是再做一次字符串拼接,方便后续使用

比如上面的模糊查询 我们可以把concat改成bind方式:

<select id="listProduct" resultType="Product">
            <bind name="likename" value="'%' + name + '%'" />
            select * from   product_  where name like #{likename}
        </select>

学习网站:https://how2j.cn/p/4989

发布了82 篇原创文章 · 获赞 21 · 访问量 7934

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/104921765