个人资源站: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的那个。