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