激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

香港云服务器
服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 詳解MyBatis 常用寫(xiě)法

詳解MyBatis 常用寫(xiě)法

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

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

什么是 mybatis ?

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

1、foreach 循環(huán)

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

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

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

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

?
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 動(dòng)態(tài)語(yǔ)句中,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 的效果。

?
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 條件是否成立,如果有一個(gè)成立,則 choose 結(jié)束。當(dāng) choose 中所有 when 的條件都不滿(mǎn)足,則執(zhí)行 otherwise 中的 sql。類(lèi)似 java 中的 switch 語(yǔ)句,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 常用寫(xiě)法 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

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

延伸 · 閱讀

精彩推薦
476
主站蜘蛛池模板: 国产视频在线免费观看 | 久久久一区二区精品 | 1314成人网 | 午夜影视一区二区 | 亚洲综合视频网 | 色婷婷久久久亚洲一区二区三区 | 九九热免费视频在线观看 | 免费观看视频在线观看 | 欧美日韩大片在线观看 | 91看片在线播放 | 亚洲第一色婷婷 | 国产亚洲精品久久久久久久 | 午夜色片| 黄色免费电影网址 | 视频一区二区中文字幕 | 精品欧美一区二区精品久久 | 欧美成人精品一区二区三区 | 久久久久91视频 | 日本a大片 | 国产三级午夜理伦三级 | 成人 精品 | 蜜桃av鲁一鲁一鲁一鲁 | 美国人成人在线视频 | 久久久久日本精品一区二区三区 | 毛片免费在线视频 | 免费视频xxxx | 国产在线区| 国产在线精品一区二区三区 | 日韩精品中文字幕一区二区三区 | xx53xx| 免费黄色小网站 | 中文字幕亚洲一区二区三区 | 日本视频在线免费观看 | 免费观看一级黄色片 | 日韩视频高清 | 麻豆蜜桃在线观看 | 美女久久| 欧美精品电影一区 | 爱视频福利 | 久久精品免费国产 | 久久精品性视频 |