一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解MyBatis 常用寫法

詳解MyBatis 常用寫法

2021-06-15 10:24ZT1994 Java教程

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。這篇文章給大家介紹了MyBatis 常用寫法,感興趣的朋友跟隨小編一起看看吧

什么是 mybatis ?

mybatis 是一款優(yōu)秀的持久層框架,它支持定制化 sql、存儲過程以及高級映射。mybatis 避免了幾乎所有的 jdbc 代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。mybatis 可以使用簡單的 xml 或注解來配置和映射原生信息,將接口和 java 的 pojos(plain old java objects,普通的 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:表示在每次進行迭代是的分隔符。
6.close:where后面表示以什么結(jié)束,如以‘)'結(jié)束。

?
1
2
3
4
5
6
7
8
9
10
//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 模糊查詢

?
1
2
3
4
5
6
7
8
9
//模糊查詢使用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ù)是否有效來進行條件查詢。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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。防止 where and 錯誤。

4、if + set

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

<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 的效果。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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 常用寫法 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:https://www.cnblogs.com/zt19994/archive/2018/11/22/9999696.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费看国产精品久久久久 | 波多野结在线 | 亚洲高清一区二区三区四区 | 日韩欧一级毛片在线播无遮挡 | 四虎成人4hutv影院 | 禁漫H天堂免费A漫 | 好爽好粗 | 精品无人区麻豆乱码1区2 | 免费看成人毛片日本久久 | 国产成+人+综合+亚洲欧美丁香花 | 三极片在线观看 | 国产精品视频视频久久 | 亚洲国产午夜看片 | 91混血大战上海双胞胎 | 无人区在线观看免费国语完整版 | 亚洲国产综合精品 | 天天狠天天透 | 国产精品探花一区在线观看 | 亚洲夜色夜色综合网站 | ass日本乱妇ass | 加勒比一本大道香蕉在线视频 | 饭冈加奈子在线播放观看 | 成人影院免费看 | 毛片免费网站 | 成人区精品一区二区毛片不卡 | 国产成人精品一区二三区 | 欧美亚洲另类综合 | 精品一区二区国语对白 | 爸爸的宝贝小说全文在线阅读 | 大伊香蕉在线精品不卡视频 | 日韩一级精品视频在线观看 | 欧美疯狂做爰3xxx | 无人影院在线播放视频 | 国产一区二区在线观看视频 | 成人欧美一区二区三区 | 精品国产乱码久久久久久软件 | 欧美式禁忌 | 久久精品无码一区二区日韩av | 国内亚州视频在线观看 | 歪歪私人影院成人毛片 | 日本在线不卡免 |