<selectid="selectByMultiCondition"resultType="car">
select * from t_car where 1=1
<!--
1.if标签中test属性是必须的。
2.if标签中test属性的值是false或者true。
3.如果test是true,则if标签中的sql语句就会拼接,反之,则不会拼接。
4.test属性中可以使用的是:
当使用了@Param注解,那么test中要出现的是@Param注解指定的参数名。@Param("brand"),那么这里只能使用brand
当没有使用@Param注解,那么test中要出现的是:param1 param2 param3 arg0 arg1 arg2...
当使用了POJO,那么test中出现的是POJO类的属性名。
5.在mybatis的动态sql中,不能使用&&,只能使用and。
--><iftest="brand != null and brand != ''">
and brand like "%"#{brand}"%"
</if><iftest="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
</if><iftest="carType != null and carType != ''">
and car_type = #{carType}
</if></select>
where标签
<selectid="selectByMultiConditionWithWhere"resultType="car">
select * from t_car
<!--where标签是专门负责where子句动态生成的
会自动去掉前面多余的and或or
不会去掉后面多余的and或or
--><where><iftest="brand != null and brand != ''">
brand like "%"#{brand}"%"
</if><iftest="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
</if><iftest="carType != null and carType != ''">
and car_type = #{carType}
</if></where></select>
trim标签
<selectid="selectByMultiConditionWithTrim"resultType="car">
select * from t_car
<!--
prefix:加前缀
suffix:加后缀
prifixOverrides:删除前缀
suffixOverrides:删除后缀
--><!--prefix="where" 是在trim标签所有内容的前面添加 where--><!--suffixOverrides="and|or" 把trim标签中内容的后缀and或or去掉--><trimprefix="where"suffixOverrides="and|or"prefixOverrides="and|or"><iftest="brand != null and brand != ''">
brand like "%"#{brand}"%"
</if><iftest="guidePrice != null and guidePrice != ''">
and guide_price > #{guidePrice}
</if><iftest="carType != null and carType != ''">
and car_type = #{carType}
</if></trim></select>
set标签
<updateid="updateBySet">
update t_car
<set><iftest="carNum != null and carNum != ''">car_num = #{carNum},</if><iftest="brand != null and brand != ''">brand = #{brand},</if><iftest="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if><iftest="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if><iftest="carType != null and carType != ''">car_type = #{carType}</if></set>
where id = #{id}
</update>
choose标签
<selectid="selectByChoose"resultType="car">
select * from t_car
<where><choose><whentest="brand != null and brand != ''">brand like "%"#{brand}"%"</when><whentest="guidePrice != null and guidePrice != ''">guide_price > #{guidePrice}</when><otherwise>
car_type = #{carType}
</otherwise></choose></where></select>
foreach批量删除和批量插入
<!--
foreach标签的属性:
collection:指定数组或者集合
item:代表数组或集合中的元素
separator:循环之间的分隔符
open:foreach循环拼接的所有sql语句的最前面以什么开始
close:foreach循环拼接的所有sql语句的最后面以什么结束
collection="ids" 第一次写这个的时候报错了,错误信息是:[array,arg0]
什么意思?
map.put("array",数组);
map.put("arg0",数组);
--><deleteid="deleteByIds"><!--delete from t_car where id in (
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)-->
delete from t_car where id in
<foreachcollection="ids"item="id"separator=","open="("close=")">
#{id}
</foreach></delete><!--批量删除,使用or关键字--><deleteid="deleteByIds2">
delete from t_car where
<foreachcollection="ids"item="id"separator="or">
id = #{id}
</foreach></delete><!--批量插入--><insertid="insertBatch">
insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values
<foreachcollection="cars"item="car"separator=",">
(null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
</foreach></insert>
sql标签和include标签
<!--声明一个SQL片段--><sqlid="carColumnNameSql">
id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
</sql><selectid="selectById2"resultType="car">
select
<!--将声明的SQL片段包含进来--><includerefid="carColumnNameSql"></include>
from t_car
where id = #{id}
</select>