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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

詳解MyBatis 常用寫法

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

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

什么是 mybatis ?

mybatis 是一款優秀的持久層框架,它支持定制化 sql、存儲過程以及高級映射。mybatis 避免了幾乎所有的 jdbc 代碼和手動設置參數以及獲取結果集。mybatis 可以使用簡單的 xml 或注解來配置和映射原生信息,將接口和 java 的 pojos(plain old java objects,普通的 java對象)映射成數據庫中的記錄。

1、foreach 循環

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

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

?
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 標簽

用 if 標簽判斷參數是否有效來進行條件查詢。

?
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 動態語句中,where 標簽會自動去掉 and 或 or。防止 where and 錯誤。

4、if + set

使用 set 標簽可以動態的配置 set 關鍵字,使用 if + set 標簽,如果某項為 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 標簽

  trim 可以更靈活的去處多余關鍵字的標簽,可以實現 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)標簽

  choose 標簽是按順序判斷其內部 when 標簽中的 test 條件是否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿足,則執行 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>

總結

以上所述是小編給大家介紹的mybatis 常用寫法 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 香蕉人人超人人超碰超国产 | 国产a一级毛片爽爽影院 | 色中色官网 | 亚洲精品久久久WWW游戏好玩 | 黑人疯狂巨大xxoo | 美女脱小内内给男生摸j | 日本一级不卡一二三区免费 | 风间由美一区二区av101 | 国产成人精品一区二三区2022 | 成人性生交大片免费看软件 | 色老头oldmoneyvideos | 91麻豆在线观看 | 国产精品久久久久久搜索 | 涩情主播在线翻车 | 奇米影视欧美 | 深夜在线观看网站 | 亚洲AV 日韩 国产 有码 | 日韩精品一区二区三区中文版 | 欧美生活一级片 | 韩国日本在线观看 | naruto tube18动漫 mm131亚洲精品久久 | 精品一区二区三区免费视频 | 亚洲狠狠综合久久 | 欧美日韩一级视频 | 四虎影在线永久免费观看 | 青青青国产精品国产精品久久久久 | 男人日女人的逼视频 | 92精品国产成人观看免费 | 日本tube24xxxxx| 性bbwbbwbbwbbw撒尿| 男女男精品视频免费观看 | 国产成人精品免费视频大全五级 | 黑帮少爷爱上我第8集最新 荷兰精品女人性hd 和日本免费不卡在线v | h肉动漫在线视频无修无遮挡 | 黄瓜视频导航 | 99久久精品免费观看区一 | 午夜爱爱爱爱爽爽爽视频网站 | 国产精品天天影视久久综合网 | 免费我看视频在线观看 | 女王调奴丨vk | 国产福利一区二区三区四区 |