<!--
//根据id和性别进行查询
-->
<select id="findByIdAndSex" resultType="user">
select * from user where 1=1
<if test="id!=null and id!=0">
and id> #{id}
</if>
<if test="sex!=null and sex!=null">
and sex = #{sex}
</if>
</select>
<!--
where+if
-->
<select id="findByIdAndSex1" resultType="user">
select * from user
<where>
<if test="id!=null and id!=0">
and id> #{id}
</if>
<if test="sex!=null and sex!=null">
and sex = #{sex}
</if>
</where>
</select>
<!--
set 修改
-->
<update id="updateUser" parameterType="user">
update user
<set>
<if test="username!=null and username!=''">
username=#{username},
</if>
<if test="birthday!= null">
birthday=#{birthday},
</if>
<if test="sex!=null and sex!='' ">
sex=#{sex},
</if>
<if test="address!=null and address!=''">
address=#{address}
</if>
</set>
where id = #{id}
</update>
<!--
根据数组进行查询sql-foreach-数组
-->
<select id="findByIdsByArray" parameterType="int[]" resultType="user">
select * from user where id in
<foreach collection="array" open="(" close=")" item="tempId" separator=",">
#{tempId}
</foreach>
</select>
<!--sql - foreach 集合-->
<select id="findByIdsByList" resultType="user" parameterType="list">
select * from user where id in
<foreach collection="list" open="(" close=")" item="tempId" separator=",">
#{tempId}
</foreach>
</select>
<!--对象查询-->
<select id="findByIdsByPojo" parameterType="queryCondition" resultType="user">
<include refid="baseSql"></include>
where id in
<foreach collection="idsList" open="(" close=")" item="tempId" separator=",">
#{tempId}
</foreach>
</select>
<sql id="baseSql">
select username,id ,birthday,sex,address from user
</sql>
mybatis动态sql
最新推荐文章于 2022-08-21 20:13:59 发布
本文详细介绍了MyBatis中动态SQL的使用,包括`<if>`标签实现条件查询和更新,以及`<foreach>`标签在处理数组和集合查询中的应用。示例展示了如何根据ID和性别查询用户,以及如何按条件更新用户信息。同时,还演示了如何通过数组和列表查询用户ID。这些内容对于理解和掌握MyBatis动态SQL及参数处理至关重要。
摘要由CSDN通过智能技术生成