欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Java培訓(xùn)之詳解MyBatis常用寫法

2018-12-04 13:39:14 1231瀏覽

今天扣丁學(xué)堂Java培訓(xùn)老師給大家介紹一下關(guān)于MyBatis常用寫法的詳細(xì)介紹,首先MyBatis是一款優(yōu)秀的持久層框架,它支持定制化SQL、存儲過程以及高級映射。MyBatis避免了幾乎所有的JDBC代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis可以使用簡單的XML或注解來配置和映射原生信息,將接口和Java的POJOs(PlainOldJavaObjects,普通的Java對象)映射成數(shù)據(jù)庫中的記錄。



1、forEach循環(huán)

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

1.collection:傳入的List或Array或自己封裝的Map。

2.item:集合中元素迭代時的別名。

3.idnex:集合中元素迭代是的索引。

4.open:where后面表示以什么開始,如以‘('開始。

5.separator:表示在每次進(jìn)行迭代是的分隔符。

6.close:where后面表示以什么結(jié)束,如以‘)'結(jié)束。

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

4、if+set

使用set標(biāo)簽可以動態(tài)的配置SET關(guān)鍵字,使用if+set標(biāo)簽,如果某項為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)簽,可以實現(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條件是否成立,如果有一個成立,則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>

以上扣丁學(xué)堂Java培訓(xùn)之詳解MyBatis常用寫法的詳細(xì)介紹,希望對大家有所幫助,扣丁學(xué)堂有專業(yè)老師制定的Java學(xué)習(xí)路線圖輔助學(xué)員學(xué)習(xí),此外還有與時俱進(jìn)的Java課程體系和Java視頻直播課供大家學(xué)習(xí),想要學(xué)好Java開發(fā)技術(shù)的小伙伴快快行動吧??鄱W(xué)堂Java技術(shù)交流群:670348138。



 


關(guān)注微信公眾號獲取更多學(xué)習(xí)資料 


查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>


標(biāo)簽: Java培訓(xùn) Java視頻教程 Java多線程 Java面試題 Java學(xué)習(xí)視頻 Java開發(fā)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部