国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

詳解MyBatis 常用寫法

2024-07-14 08:42:55
字體:
供稿:網(wǎng)友

什么是 MyBatis ?

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲(chǔ)過程以及高級(jí)映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動(dòng)設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡(jiǎn)單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對(duì)象)映射成數(shù)據(jù)庫中的記錄。

1、forEach 循環(huán)

  forEach 元素的屬性主要有 item, idnex, collection, open, separator, close。

1.collection:傳入的 List 或 Array 或自己封裝的 Map。
2.item:集合中元素迭代時(shí)的別名。
3.idnex:集合中元素迭代是的索引。
4.open:where 后面表示以什么開始,如以‘('開始。
5.separator:表示在每次進(jìn)行迭代是的分隔符。
6.close:where后面表示以什么結(jié)束,如以‘)'結(jié)束。

//mapper中需要傳遞一個(gè)容器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>

 

2、concat 模糊查詢

//模糊查詢使用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>

3、if + where 標(biāo)簽

用 if 標(biāo)簽判斷參數(shù)是否有效來進(jìn)行條件查詢。

<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 動(dòng)態(tài)語句中,where 標(biāo)簽會(huì)自動(dòng)去掉 AND 或 OR。防止 WHERE AND 錯(cuò)誤。

4、if + set

使用 set 標(biāo)簽可以動(dòng)態(tài)的配置 SET 關(guān)鍵字,使用 if + set 標(biāo)簽,如果某項(xiàng)為 null 則不進(jìn)行更新。

<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>

5、if + trim 代替 where/set 標(biāo)簽

  trim 可以更靈活的去處多余關(guān)鍵字的標(biāo)簽,可以實(shí)現(xiàn) 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>

5、choose(when, otherwise)標(biāo)簽

  choose 標(biāo)簽是按順序判斷其內(nèi)部 when 標(biāo)簽中的 test 條件是否成立,如果有一個(gè)成立,則 choose 結(jié)束。當(dāng) choose 中所有 when 的條件都不滿足,則執(zhí)行 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>

總結(jié)

以上所述是小編給大家介紹的MyBatis 常用寫法 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遵化市| 桐城市| 榕江县| 京山县| 天长市| 昭通市| 太保市| 舒兰市| 金寨县| 龙山县| 宿迁市| 屏东县| 樟树市| 玉树县| 五常市| 鄂托克旗| 湾仔区| 大宁县| 黄骅市| 合肥市| 清水县| 松溪县| 静宁县| 贵南县| 永川市| 鄂伦春自治旗| 隆德县| 丽水市| 白水县| 瓮安县| 应城市| 兴宁市| 蒲城县| 徐闻县| 万年县| 宝山区| 美姑县| 蚌埠市| 尉犁县| 佛坪县| 鸡东县|