if标签中test属性时必须的
if标签中test属性的值是true或者false,true则进行拼接,false则反之
test属性中可以使用的是:
注意test里面用的是and表示并且
例如:
<select id="selectByMultiCondition" resultType="car">
select * from t_car
where 1 = 1
<if test="brand != null and brand != ''">
and brand like "%"#{brand}"%"
if>
<if test="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
if>
<if test="carType != null and carType != ''">
and car_type = #{carType}
if>
select>
让where字句更加动态智能
<select id="selectByMultiConditionWithWhere" resultType="car">
select * from t_car
<where>
<if test="brand != null and brand != ''">
and brand like "%"#{brand}"%"
if>
<if test="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
if>
<if test="carType != null and carType != ''">
and car_type = #{carType}
if>
where>
select>
第一个if标签中的and可以自动去掉

<select id="selectByMultiConditionWithTrim" resultType="car">
select * from t_act
<trim prefix="where" suffixOverrides="and|or" >
<if test="brand != null and brand != ''">
brand like "%"#{brand}"%" and
if>
<if test="guidePrice != null and guidePrice != ''">
guide_price > #{guidePrice} and
if>
<if test="carType != null and carType != ''">
car_type = #{carType}
if>
trim>
select>
加前缀时,改标签可以动态判断,当if里面全部不成立时,where不会加上去。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-twDNDO9Z-1669533417960)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1669518854852.png)]](https://1000bd.com/contentImg/2024/04/17/2274ec887a921a19.png)
<update id="updateBySet">
update t_car
<set>
<if test="carNum != null and carNum != ''">
car_num = #{carNum},
if>
<if test="brand != null and brand != ''">
brand = #{brand},
if>
<if test="guidePrice != null and guidePrice != ''">
guide_Price = #{guidePrice},
if>
<if test="produceTime != null and produceTime != ''">
produce_time = #{produceTime},
if>
<if test="carType != null and carType != ''">
car_type = #{carType},
if>
set>
where
id = #{id}
update>
可以自动去掉逗号 “,”,并且添加set
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vl9vFDPg-1669533417961)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1669530264925.png)]](https://1000bd.com/contentImg/2024/04/17/d30b24a328bacb38.png)
<select id="selectByChoose" resultType="car">
select * from t_car
<where>
<choose>
<when test="brand != null and brand != ''">
brand like "%"#{brand}"%"
when>
<when test="guidePrice != null and guidePrice != ''">
guide_price > #{guidePrice}
when>
<otherwise>
car_type = #{carType}
otherwise>
choose>
where>
select>
foreach标签的属性:
<delete id="deleteByIds">
delete from t_car where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
foreach>
delete>
<insert id="insertBatch">
insert into t_car values
<foreach collection="cars" item="car" separator=",">
(null ,#{car.carType}, #{car.brand}, #{car.carPrice}, #{car.produceTime}, #{car.carType})
foreach>
insert>
声明sql片段提高复用性,用的较少
<sql id="selectColumn">
<if test="brand != null and brand != ''">
brand like "%"#{brand}"%" and
if>
<if test="guidePrice != null and guidePrice != ''">
guide_price > #{guidePrice} and
if>
<if test="carType != null and carType != ''">
car_type = #{carType}
if>
sql>
<select id="selectByMultiConditionWithTrim" resultType="car">
select * from t_car
<trim prefix="where" suffixOverrides="and|or" >
<include refid="selectColumn" />
trim>
select>