标签:concat com ola nbsp upd 传递 去掉 select des
??forEach 元素的属性主要有 item, idnex, collection, open, separator, close。
<select id="queryByName" resultMap="BaseResultMap" paramterType"string"> SELECT * FROM user <where> <if test="name != null"> name like concat(‘%‘, concat(#{name}, ‘%‘)) </if> </where> </select>
SELECT * FROM user <where> <if test="userId !=null and userId!= ‘‘"> userId= </if> <if test="name !=null and name!= ‘‘"> AND name= </if> <if phone="userId !=null and phone!= ‘‘"> AND 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> <iftest=" name != null and name != ‘‘"> name = #{name}, </if> <iftest=" 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= </if> <if test="name !=null and name!= ‘‘"> AND name= </if> <if phone="userId !=null and phone!= ‘‘"> AND phone= </if> </trim> </select> <update id="updateUser" paramterType="com.demo.User"> UPDATE user <trim prefix="SET" suffixOverrides=","> <if test=" name != null and name != ‘‘"> name = </if> <if test=" phone != null and phone != ‘‘"> phone = </if> </trim> WHERE 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 = </when> <when test="Utype == ‘phone‘"> WHERE phone= </when> <when test="Utype == ‘email‘"> WHERE email= </when> <otherwise> WHERE name = </otherwise> </choose> </select>
//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=","> </foreach> </select>
标签:concat com ola nbsp upd 传递 去掉 select des
原文地址:https://www.cnblogs.com/xiaoqianTS/p/10000393.html