目录
sql标签
编辑
where标签
set标签
foreach标签
批量增加
批量删除
将基础SQL语句中重复性高的增加它的复用性,使得sql语句的灵活性更强
sql标签<sql>
<sql id="text">
select * from user
</sql>
<select id="selectAll" resultType="user">
<include refid="text"></include>
</select>
where标签
注意事项:where会自动忽略前后缀比如and与or
<select id="selectByIdAndPwd" resultType="user">
<include refid="text"></include>
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="pwd!=null">
and password=#{pwd}
</if>
</where>
</select>
set标签
注意事项:set中满足条件的if会自动忽略后缀,比如“,”
<update id="updateUser" parameterType="user">
update user
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="password!=null">
password=#{password}
</if>
</set>
where id=#{id}
</update>
foreach标签
批量增加
<!--collection传入的容器类型;open以什么为开始,close以什么为结束,separator分割
item传入的容器中的元素-->
<insert id="addForeach" parameterType="user">
insert into user(name,password,sex) values
<foreach collection="list" item="user" separator=",">
(#{user.name},#{user.password},#{user.sex})
</foreach>
</insert>
批量删除
<delete id="delForeach" parameterType="int">
delete from user where id in
<foreach collection="array" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</delete>