if标签
1.if标签中test属性是必须的
2.if标签中的test属性的值是true或false
3.如果是true则将if标签中的sql语句就会拼接
4.test属性中可以使用的是
使用@Param注解 @Param(brand) brand
未使用注解 param1 param2 arg0 arg 1
使用POJO POJO类的属性名
5.在mybatis中不能使用&&只能用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标签
1.让where⼦句更加动态智能。
2.所有条件都为空时,where标签保证不会⽣成where⼦句。
3.⾃动去除某些条件
前⾯
多余的and或or。
4.继续使⽤if标签中的需求。
<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>
trim标签
prefix:加前缀
suffix:加后缀
prefixOverrides:删除前缀
suffixOverrides:删除后缀
suffix:加后缀
prefixOverrides:删除前缀
suffixOverrides:删除后缀
<select id="selectByMultiConditionWithTrim" resultType="Car">
select * from t_car
<trim prefix="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>
</trim>
</select>
set标签
主要使⽤在update语句当中,⽤来⽣成set关键字,同时去掉最后多余的“,”
⽐如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那这个字段不更新
<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>
choose when otherwise标签
1.三个标签一起使用
2.等同于if else 语句
<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 > #{guide_price}
</when>
<otherwise>
car_type = #{carType}
</otherwise>
</choose>
</where>
</select>
foreach标签
1.循环数组或集合
2.
collection:指定数组或集合
item:代表数组或集合中的元素
separator:循环之间的分隔符
item:代表数组或集合中的元素
separator:循环之间的分隔符
<delete id="deleteByIds">
delete from t_car where id in(
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</delete>
sql标签与include标签
sql标签⽤来声明sql⽚段
include标签⽤来将声明的sql⽚段包含到某个sql语句当中
<sql id="carCols">id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType</sql>
<select id="selectAllRetMap" resultType="map">
select <include refid="carCols"/> from t_car
</select>
<select id="selectAllRetListMap" resultType="map">
select <include refid="carCols"/> carType from t_car
</select>
<select id="selectByIdRetMap" resultType="map">
select <include refid="carCols"/> from t_car where id = #{id}
</select>