mybatis的一些特殊SQL用法

 个人资源站:https://file.woytu.com/

trim标签:

<trim prefix="(" suffix=")" prefixOverrides="AND |OR " suffixOverrides="," >

其中

prefix:代表trim标签包括起来的开头要加入的字符

suffix:代表trim标签包括起来的结尾要加入的字符

prefixOverrides(前缀):代表要删除的第一个字符串,可用“|”(或者的意思)

suffixOverrides (后缀):代表要删除的最后一个字符串,可用“|”(或者的意思)

 

 

动态插入数据到表中(判断参数是否为空):

<insert id="updateOrderAccessories">
insert into zjst_01_store_price
<!-- 这里是用“(”开始,用“)”结束 -->
<trim prefix="(" suffix=")" suffixOverrides=",">
	<!-- if判断mapper接口传来的参数是否为空 -->
	<if test="detailId !=null and detailId''">
		detail_id,
	</if>
	<if test="accessoriesId !=null and accessoriesId !=''">
		parts_id,
	</if>
	<if test="number !=null and number !=''">
		number,
	</if>
</trim>
<!-- 这里是用“values (”开始,用“)”结束 -->
<trim prefix="values (" suffix=")" suffixOverrides=",">
<!-- if判断mapper接口传来的参数是否为空 -->
	<if test="detailId !=null and detailId''">
		#{detailId},
	</if>
	<if test="accessoriesId !=null and accessoriesId !=''">
		#{accessoriesId},
	</if>
	<if test="number !=null and number !=''">
		#{number},
	</if>
</trim>
</insert>

 

动态更改表中数据(判断参数是否为空):

 

<update id="updateOrderStutas">
update zjst_01_order 
<!--使用trim就是为了删掉最后字段的“,”。主要不用单独写SET了,因为set被包含在trim中  -->
<trim prefix="set" suffixOverrides=",">
	stutas=6,
	<!-- 判断参数是否为空 -->
	<if test="RejectProblem !=null RejectProblem !=''">
	question_entry=#{RejectProblem}
	</if>
</trim>
where id=#{orderId}
</update>

 

 

choose:  我选择了你,你选择了我!

Java中有switch,  mybatis有choose。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose></select>

当title和author都不为null的时候, 那么选择二选一(前者优先), 如果都为null, 那么就选择 otherwise中的, 如果tilte和author只有一个不为null, 那么就选择不为null的那个。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值