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

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

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

服務器之家 - 編程語言 - Java教程 - Mybatis模糊查詢和動態sql語句的用法

Mybatis模糊查詢和動態sql語句的用法

2021-07-26 10:52LLY19960418 Java教程

今天小編就為大家分享一篇關于Mybatis模糊查詢和動態sql語句的用法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

mybatis 模糊查詢和動態sql語句

模糊查詢

對數據庫最常用的操作就是查詢了,但是如何使用mybatis進行模糊查詢呢?下面先看一個簡單的模糊查詢

?
1
2
3
4
5
6
7
<select id="select01" resultmap="basicresultmap">
 select
 *
from
 oa_employee
where emp_name like #{asd}
</select>

這是一條偽模糊查詢, 因為沒有實現真正的模糊 “%”。參數為字符串,所以#{}中內容不被限制。但是應該如何插入 % 字符呢。 我們首先想到的是傳遞字符串參數時將%插入到字符串中 “張%”,但是這種方法操作略微繁瑣了一些。 下面提供了使用sql方法的策略

?
1
2
3
4
5
6
7
<select id="select01" resultmap="basicresultmap">
  select
  *
 from
  oa_employee
 where emp_name like concat( #{asd} ,'%')
 </select>

另外一種不推薦的寫法給大家

?
1
2
3
4
5
6
7
<select id="select01" resultmap="basicresultmap">
   select
   *
  from
   oa_employee
  where emp_name like '${emp_name}%'
  </select>

#{} 是采用預編譯的寫法,也就是jdbc中的perparestatement,這種寫法可以防止sql注入,但${}這種寫法是不采用預編譯,其中的參數寫成類中的屬性或者map的key值或者為接口中注解的參數名。

mybatis 提供了bind 標簽。下面舉個例子

?
1
2
3
4
5
6
7
8
<select id="select01" resultmap="basicresultmap">
 <bind name="emp_name" value="'%'+ _parameter.getemp_name() +'%'"/>
  select
  *
 from
  oa_employee
 where emp_name like #{emp_name}
 </select>

他是在#{}表達式自動填入value值,值得注意的是“_parameter.getemp_name()” 調用的方法是對象中作為查詢參數的屬性的get方法

多條件查詢

多種條件查詢的要點是判斷查詢條件是否為空,拼接sql語句。在mybatis中提供了if標簽和where 標簽。 下面來介紹兩種標簽的用法。

if標簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<select id="select01" resultmap="basicresultmap">
select
*
from
oa_employee
where 1=1
<if test="emp_name != null and emp_name != ''">
and emp_name = #{emp_name }
</if>
<if test="emp_sex != null and emp_sex != ''">
and sex = #{emp_sex}
</if>
</select>

mybatis 中的if標簽有些類似于el表達式的使用,test中可以直接寫入類中的屬性或者key值。

where標簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="select01" resultmap="basicresultmap">
   select
   *
   from
   oa_employee
   <where>
   <if test="emp_name != null and emp_name != ''">
    and emp_name = #{emp_name }
   </if>
   <if test="emp_sex != null and emp_sex != ''">
    and sex = #{emp_sex}
   </if>
   </where>
  </select>

這里的where標簽 替換了前一段代碼的 where 1=1 。 mybatis中的where 標簽會判斷標簽內是否有內容, 如果有內容就自動生成where 并把 where 后面的第一個and +一個空格,or+一個空格 去掉。

choose , when 和 otherwise 標簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<select id="select01" resultmap="basicresultmap">
   select
   *
   from
   oa_employee
   <where>
   <choose>
    <when test="emp_name != null and emp_name != ''">
       and emp_name = #{emp_name }
    </when>
     <when test="emp_sex != null and emp_sex != ''">
       and sex = #{emp_sex}
    </when>
    <otherwise>
      emp_id = 50
    </otherwise>
   </choose>
   </where>
  </select>

當所有條件不滿足時,執行otherwise標簽的內容。

trim標簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<select id="select01" resultmap="basicresultmap">
   select
   *
   from
   oa_employee
    <trim prefix="where" prefixoverrides="and |or ">
    <if test="emp_name != null and emp_name != ''">
      and emp_name = #{emp_name }
    </if>
    <if test="emp_sex != null and emp_sex != ''">
       and sex = #{emp_sex}
    </if>
  </trim>

trim標簽的屬性及其含義

  • - prefix : 標簽之間有內容在最前面加入
  • - prefixoverrides: 檢查內容的最前面是否匹配,匹配就刪除
  • - suffix: 標簽之間有內容在最后面加入
  • - suffixoverrides:檢查內容的最后面是否匹配,匹配就刪除

set標簽

set標簽常用于update操作,并且會自動抹掉無關的,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<update id="update01" >
 update
  oa_employee
 <set>
   <if test="emp_name != null and emp_name != ''">
      emp_name = #{emp_name}
   </if>
   <if test="emp_sex != null and emp_sex != ''">
     ,sex = #{emp_sex}
   </if>
 </set>
 where emp_id = 50
 </update>

foreach標簽

foreach 用于處理數組或者list集合,下面是一個批量添加的例子

?
1
2
3
4
5
6
7
8
9
<insert id="insert01">
 insert into
 oa_employee
 ( emp_name, sex, fk_dept_id)
 values
 <foreach collection="list" item="employee" separator=",">
  (#{employee.emp_name},#{employee.emp_sex},#{employee.fk_dept_id})
 </foreach>
 </insert>

其中 如果參數為數組 則collection只能為“array” 參數為list集合則collection只能為 “list” item類似jstl 中的var的作用, 指代容器中的每一個對象。separator=”,”的含義是每條數據以 , 分割。 未注明的屬性有 open 和 close 他們的含義是在遍歷開始和結束時分別添加其內容。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/LLY19960418/article/details/71124247

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 秋葵丝瓜茄子草莓榴莲樱桃 | 红楼梦黄色小说 | 国产乱子伦真实china | 撕开老师的丝袜白丝扒开粉嫩的小 | 色久天 | avav一区 | 亚洲精品成人A8198A片漫画 | 久久毛片网站 | 波多野结衣中文丝袜字幕 | 男人捅女人的鸡鸡 | 污网站免费观看在线高清 | 亚洲成年人在线观看 | 成 人免费va视频 | www.最色| 精品视频久久久久 | 为什么丈夫插我我却喜欢被打着插 | 国产精品吹潮香蕉在线观看 | 农村妇女野战bbxxx农村妇女 | 国产色网址 | 图片亚洲va欧美va国产综合 | 二次元美女互摸隐私互扒 | 午夜精品网站 | 日韩国产成人资源精品视频 | 国产精品露脸国语对白河北 | 美女扒开胸罩露出胸大乳 | 国产一线天 | 娇妻被老外疯狂调教 | 欧美激情亚洲 | 国产一区二区免费福利片 | 欧美一区二区日韩一区二区 | 亚州春色 | 精品在线免费观看视频 | 成人男女啪啪免费观看网站 | 4444亚洲国产成人精品 | 校园春色偷拍自拍 | 五月色婷婷久久综合 | 久久中文字幕无线观看 | 亚洲高清在线天堂精品 | 欧美成狂野欧美在线观看 | 精品视频在线免费看 | 久久久久久久久女黄 |