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的主要内容,如果未能解决你的问题,请参考以下文章

鍔ㄦ€佽鍒掕繘闃剁瘒1---鑳屽寘闂

DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode

鍔ㄦ€佹墽琛孲ql

绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級

鍔ㄦ极绠楁硶姊崇悊

Linux鍔ㄦ€佸簱鑷姩鏋勫缓Makefile