Mybatis_03_动态语句
1.if标签
test里面的userName是实体类的属性。
<!--根据条件查询--> <select id="findUserByConditon" resultMap="userMap" parameterType="user"> select * from user where 1=1 <if test="userName!=null"> and username=#{userName} </if> <if test="sex!=null"> and sex=#{sex} </if> </select>
2.where标签 不用谢where 1=1
<!--根据条件查询--> <select id="findUserByConditon" resultMap="userMap" parameterType="user"> select * from user <where> <if test="userName!=null"> and username=#{userName} </if> <if test="sex!=null"> and sex=#{sex} </if> </where> </select>
3.foreach标签
open一个( close 一个) item是每一项 separator使用 ',' 分隔。
<!--根据queryVo中的id集合查询用户列表--> <select id="findUserInIds" resultMap="userMap" parameterType="queryVo"> select * from user <where> <if test="ids!=null and ids.size()>0"> <foreach collection="ids" open="and id in (" close=")" item="uid" separator=","> #{uid} </foreach> </if> </where> </select>
学习记录,网上找的,自己写的都记录下来。如有雷同并且侵犯到您的权益,请联系本人修改。
浙公网安备 33010602011771号