<select id="findByColor" resultType="person">
select * from person
<where><if test="id!=null">
and id = #{id}</if><if test="name!=null">
and name = #{name}</if><if test="loveColor!=null">
and love_color like "%"#{loveColor}"%"</if></where></select><!--如果有条件,where会删除第一个and。如果没有条件查询,where标签就当不存在-->
注意:1.like这块 的字符拼接如下两种方式都可以,
// "%"#{lovecolor}"%" 推荐使用这种方式// "%${lovecolor}%" 但是$的形式,是字符串拼接的,会出现sql注入问题。2.注释使用xml的注释,不能使用sql语句注释
动态修改
<update id="updateById">
update person
<set><if test="age!=null">
age = #{age},</if><if test="name!=null">
name = #{name},</if></set>
where id=#{id}</update>//注:update的set关键字,会帮我们去掉最后一个逗号
动态删除
<delete id="deleteByIds">
<!-- collection:指定输入集合或者数组的参数名称 集合就是list。数组就是array -->
<!-- item:声明遍历出的参数变量 -->
<!-- open:遍历开始时需要拼接的内容 -->
<!-- close:遍历结束时需要拼接的内容 -->
<!-- separator:遍历出的参数变量之间需要拼接的参数 ,delete from person where id in ( 1,2,3)-->
delete from person where id in (
<foreach collection="array" item="deleteid" separator=",">
${deleteid}
</foreach>
)
</delete>