mybatis鍔ㄦ€丼QL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis鍔ㄦ€丼QL相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/sele' title='sele'>sele tor 閬嶅巻 sel select word open map 鍚庣紑
1.sql鐗囨锛歴ql璇彞涓噸澶嶄娇鐢ㄧ殑鍙互鎻愬彇鎴恠ql鐗囨渚涜皟鐢紙濡傛暟鎹〃涓殑瀛楁锛?/p>
sql鏍囩鏄痵ql鐗囨锛宨d鏄璋冪敤鏃剁敤鐨?/p>
<sql id="all"> u_id,u_username,u_password,u_sex,u_createtime,u_createtime,u_delete </sql> |
sql鐗囨鐢╥nclude鏍囩璋冪敤
<include refid="all"></include> |
2.where+if
where鏍囩浼氬湪杩斿洖鐨剆ql璇彞鍓嶅姞where鍐嶅幓闄nd鎴杘r
<select id="selectByWhere" resultMap="BaseMap"> select <include refid="all"></include> from tb_user <where> <if test="uid!=0"> and u_id=#uid </if> <if test="username!=null and username!=鈥樷€?> <!-- 妯$硦鏌ヨ --> and u_username like concat(鈥?鈥?#username,鈥?鈥? </if> </where> </select> |
浠ヤ笂杩樼敤浜嗘ā绯婃煡璇細鐢╟oncat鍑芥暟鎷兼帴%
3.set+if
set鏍囩浼氬湪杩斿洖鐨剆ql鍓嶅姞set鍐嶅幓闄ゆ渶鍚庣殑锛岋紙閫楀彿锛?/p>
<update id="update"> update tb_user <set> <if test="username!=null and username!=鈥樷€?> u_username=#username, </if> <if test="password!=null and password!=鈥樷€?> u_password=#password, </if> <if test="usex!=null and usex!=鈥樷€?> u_sex=#usex, </if> <if test="udelete!=null and udelete!=鈥樷€?> u_delete=#udelete, </if> </set> where u_id=#uid </update> |
4.trim+if锛氶噷闈㈡湁灞炴€э細
a) prefix锛氬墠缂€锛屽湪sql璇彞鍓嶅姞xxx
b) suffix锛氬悗缂€锛屽湪sql璇彞鍚庡姞xxx
c) prefixOverride锛氭浛鎹ql璇彞鍓嶇殑xxx
d) suffixOverride锛氭浛鎹ql璇彞鍚庣殑xxx
5.choose+when+otherwise
浠庝笂寰€涓嬮€夋嫨锛屾湁涓€涓鍚堝氨璺冲嚭
<!-- 鏍规嵁涓€涓笉闄愬畾鐨勬潯浠舵煡璇?--> <select id="select" resultMap="BaseMap"> select <include refid="all"></include> from tb_user <where> <choose> <when test="uid!=0"> and u_id=#uid </when> <when test="username!=null and username!=鈥樷€?> and u_username like concat(鈥?鈥?#username,鈥?鈥? </when> <otherwise> </otherwise> </choose> </where> </select> |
6.foreach
閬嶅巻鏁扮粍鎴栭泦鍚堬紙鏍规嵁澶氫釜id鏌ヨ鐢╥n鎴杘r锛?/p>
<!-- 鏍规嵁澶氫釜id鏌ヨ --> <select id="selectByIds" resultMap="BaseMap"> select <include refid="all"></include> from tb_user <where> <foreach collection="ids" open="u_id in(" close=")" item="id" separator=","> #id </foreach> </where> </select> |
以上是关于mybatis鍔ㄦ€丼QL的主要内容,如果未能解决你的问题,请参考以下文章