<!--trim的用法-->
<!-- trim 只要包含的内容没有任何条件成立,trim不会生效
如果包含的内容有条件成立,查看内部的语句前缀有没有与prefixOverrides内容相同的
相同就将其去掉,即此处的and,之后再在前面加上前缀prefix
-->
<select id="selectBySelective" parameterType="tendencyParam" resultType="tendency">
select * from tendency
<trim prefix="where" prefixOverrides="and">
<choose>
<when test="id!=null">
and id=#{id}
</when>
<when test="supCode != null">
and sup_code=#{supCode}
</when>
<when test="createDate != null">
and create_date=#{createDate}
</when>
<!--<otherwise>.....</otherwise>-->
</choose>
</trim>
</select>
<!--foreach的用法-->
// select sum(arv_rate) from tendency where id in ( ? , ? , ? , ? )
// mybatis内部会进行:将传递的集合对象
// 通过map.put("list", listInteger),将集合保存成键值对存储
// 之后再xml中在访问listInteger的时候,要通过list去访问
double sum = tendencyMapper.sumTendencyIds(listInteger);
<!--collection="list" 代表集合
item="val" 代表foreach每次循环遍历的值
separator="," 值之间的分隔符
open="(" 开始符号
close=")" 结束符号
(1001,1005,1007)
-->
<select id="sumTendencyIds" resultType="double">
select sum(arv_rate)
from tendency
where id in
<foreach collection="list" item="val" separator="," close=")" open="(">
#{val}
</foreach>
<!--if的用法-->
</select>
<update id="updateTendencyById" parameterType="tendency">
update tendency
<trim prefix="set" suffixOverrides=",">
<!--<set>-->
<if test="supCode!=null">
sup_code=#{supCode},
</if>
<if test="supName!=null">
sup_name=#{supName},
</if>
<if test="arvRate!=null">
arv_rate=#{arvRate},
</if>
<if test="passRate!=null">
pass_rate=#{passRate},
</if>
<if test="createDate!=null">
create_date=#{createDate},
</if>
</trim><!--</set>-->
where id=#{id}
</update>
动态SQL where trim
最新推荐文章于 2024-08-09 13:03:07 发布