一、if标签
符合if的条件则执行if代码块中的内容否则跳过 if标签中test属性必须写!
if 也可以在 udpate delete insert 中写入
<select id="selectBlogAndAuthor" resultMap="BlogAndAuthor">
select * from tb_blog g inner join tb_author r on g.sId = r.id
<where>
<if test="id !=null and id !='' "> <!-- test 中的id为自己定义的变量名必须一致 而不是数据库中的字段名 -->
r.id = #{id}
<!-- 错误写发 concat("'%","#{username}","%'")-->
</if>
<if test="blogId != null and blogId !=''">
g.blogId = #{blogId}
</if>
<if test="blogTitle !=null and blogTitle !='' ">
and g.blogTitle like concat('%',#{blogTitle},'%')
</if >
<if test="createTime != null and createTime !='' ">
and g.createTime between #{createTime} and #{endTime}
</if>
</where>
order by g.blogId
</select>
<update id="updateUserByIdSelective"
update tb_user set
<if test="userName !=null and userName !=''">
user_name=#{userName}
</if>
<if test="userPassword !=null and userPassword !=''">
user_password=#{user_password}
</if>
id=#{id} <!-- 必须写 -->
where id=#{id}
</update>
<insert id="insertUserByIdSelective"
insert into tb_user(user_name,user_password,
<if test="userEmail !=null and userEmail !=''">
user_email,
</if>
user_info) values(#{userName},#{userPassword},
<if test="userEmail !=null and userEmail !=''>
#{userEmail},
</if>
#{userInfo})
)
二、sql标签
1.choose标签
哪个when标签test属性满足条件 就执行那个when的代码块内容,都不满足则 执行otherwise标签中的内容
<select id="selectBlogAndAuthor" resultMap="BlogAndAuthor">
select * from tb_user where 1=1
<choose> <!-- choose 那个when标签test属性满足条件 就执行那个when的代码块内容,都不满足则 执行otherwise标签中的内容-->
<when test="id!=null">
and id=#{id}
</when>
<when test="userName!=null and userName!=''">
and User_name=#{userName}
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</select>
2.where标签
当when标签中的条件满足时才会执行其中的代码块内容 否则不执行when
<select id="selectBlogAndAuthor" resultMap="BlogAndAuthor">
select * from tb_blog g inner join tb_author r on g.sId = r.id
<where> <!-- 当when标签中的条件满足时才会执行其中的代码块内容 否则不执行<when> -->
<if test="id !=null and id !='' "> <!-- test 中的id为自己定义的变量名必须一致 而不是数据库中的字段名 -->
r.id = #{id}
<!-- 错误写发 concat("'%","#{username}","%'")-->
</if>
<if test="blogId != null and blogId !=''">
g.blogId = #{blogId}
</if>
</where>
order by g.blogId
</select>
3.set标签
满足set标签中的条件才会执行 其中的内容 否则不执行set标签
<update id="updateUserByIdSelective"
update tb_user
<set>
<if test="userName !=null and userName !=''">
user_name=#{userName}
</if>
<if test="userPassword !=null and userPassword !=''">
user_password=#{user_password}
</if>
id=#{id} <!-- 必须写 -->
</set>
where id=#{id}
</update>
4.trim标签
可以综合使用 set标签和where标签的功能 相当于拼接他们两个
<!-- 设置前缀where -->
<select id="selectBlogAndAuthor" resultMap="BlogAndAuthor">
select * from tb_user
<trim prefix="where" prefixOverrides="and | or">
<if test="userName !=null and userName !=''">
and user_name=#{userName}
</if>
<if test="userPassword !=null and userPassword !=''">
and user_password=#{user_password}
</if>
</trim>
<!-- 清除<set>后缀',' -->
<update id="updateUserByIdSelective"
update tb_user
<trim prefix="set" suffixOverrides=",">
<if test="userName !=null and userName !=''">
user_name=#{userName}
</if>
<if test="userPassword !=null and userPassword !=''">
user_password=#{user_password}
</if>
id=#{id}
</trim>
where id=#{id}
</update>
5.foreach标签
动态删除 配合sql 关键字 in 使用
<!-- 批量删除-->
<delete id="deleteById">
delete from tb_blog where blogId in
<!-- delete from emp where empno in(7789,7790) -->
<!-- forEach : 用来循环
collection : 用来指定循环的数据的类型 可以填的值有:array,list,map
item : 循环中为每个循环的数据指定一个别名
index : 循环中循环的下标
open : 开始 close : 结束
separator : 数组中元素之间的分隔符 -->
<foreach collection="array" item="blogId" open="(" close=")" separator=",">
#{blogId}
</foreach>
</delete>