foreach 元素的属性主要有 item, idnex, collection, open, separator, close。
//mapper中需要传递一个容器 public list<user> querybyidlist(list<integer> useridlist); <select id="querybyidlist" resultmap="baseresultmap" parametertype="map"> select * from user where userid in <foreach collection="useridlist" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </select>
//模糊查询使用concat拼接sql <select id="querybyname" resultmap="baseresultmap" paramtertype"string"> select * from user <where> <if test="name != null"> name like concat('%', concat(#{name}, '%')) </if> </where> </select>
用 if 标签判断参数是否有效来进行条件查询。
<select id="getuserlist" resultmap="baseresultmap" paramtertype="com.demo.user"> select * from user <where> <if test="userid !=null and userid!= ''"> userid= #{userid} </if> <if test="name !=null and name!= ''"> and name= #{name} </if> <if phone="userid !=null and phone!= ''"> and phone= #{phone} </if> </where> </select>
where 动态语句中,where 标签会自动去掉 and 或 or。防止 where and 错误。
使用 set 标签可以动态的配置 set 关键字,使用 if + set 标签,如果某项为 null 则不进行更新。
<update id="updateuser" paramtertype="com.demo.user"> update user <set> <if test=" name != null and name != ''"> name = #{name}, </if> <if test=" phone != null and phone != ''"> phone = #{phone}, </if> </set> where userid = #{userid} </update>
trim 可以更灵活的去处多余关键字的标签,可以实现 where 和 set 的效果。
<select id="getuserlist" resultmap="baseresultmap" paramtertype="com.demo.user"> select * from user <trim prefix="where" prefixoverrides="and|or"> <if test="userid !=null and userid!= ''"> userid= #{userid} </if> <if test="name !=null and name!= ''"> and name= #{name} </if> <if phone="userid !=null and phone!= ''"> and phone= #{phone} </if> </trim> </select> <update id="updateuser" paramtertype="com.demo.user"> update user <trim prefix="set" suffixoverrides=","> <if test=" name != null and name != ''"> name = #{name}, </if> <if test=" phone != null and phone != ''"> phone = #{phone}, </if> </trim> where userid = #{userid} </update>
choose 标签是按顺序判断其内部 when 标签中的 test 条件是否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满足,则执行 otherwise 中的 sql。类似 java 中的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
<select id="selectcustomerbycustnameandtype" parametertype="map" resultmap="baseresultmap"> select * from user <choose> <when test="utype == 'name'"> where name = #{name} </when> <when test="utype == 'phone'"> where phone= #{phone} </when> <when test="utype == 'email'"> where email= #{email} </when> <otherwise> where name = #{name} </otherwise> </choose> </select>
如对本文有疑问, 点击进行留言回复!!
【性能】Android中的内存溢出(Out Of Memory,OOM)
android framework层input事件接收和派发
网友评论