Mybatis模糊查詢和動態(tài)sql語句的用法
Mybatis 模糊查詢和動態(tài)sql語句
模糊查詢
對數(shù)據(jù)庫最常用的操作就是查詢了,但是如何使用Mybatis進(jìn)行模糊查詢呢?下面先看一個簡單的模糊查詢
<select id="select01" resultMap="BasicResultMap"> SELECT * FROM oa_employee WHERE emp_name LIKE #{asd} </select>
這是一條偽模糊查詢, 因為沒有實現(xiàn)真正的模糊 “%”。參數(shù)為字符串,所以#{}中內(nèi)容不被限制。但是應(yīng)該如何插入 % 字符呢。 我們首先想到的是傳遞字符串參數(shù)時將%插入到字符串中 “張%”,但是這種方法操作略微繁瑣了一些。 下面提供了使用sql方法的策略
<select id="select01" resultMap="BasicResultMap"> SELECT * FROM oa_employee WHERE emp_name LIKE CONCAT( #{asd} ,'%') </select>
另外一種不推薦的寫法給大家
<select id="select01" resultMap="BasicResultMap"> SELECT * FROM oa_employee WHERE emp_name LIKE '${emp_name}%' </select>
#{} 是采用預(yù)編譯的寫法,也就是JDBC中的PerpareStatement,這種寫法可以防止sql注入,但${}這種寫法是不采用預(yù)編譯,其中的參數(shù)寫成類中的屬性或者map的key值或者為接口中注解的參數(shù)名。
mybatis 提供了bind 標(biāo)簽。下面舉個例子
<select id="select01" resultMap="BasicResultMap"> <bind name="emp_name" value="'%'+ _parameter.getEmp_name() +'%'"/> SELECT * FROM oa_employee WHERE emp_name LIKE #{emp_name} </select>
他是在#{}表達(dá)式自動填入value值,值得注意的是“_parameter.getEmp_name()
” 調(diào)用的方法是對象中作為查詢參數(shù)的屬性的get方法
多條件查詢
多種條件查詢的要點是判斷查詢條件是否為空,拼接sql語句。在mybatis中提供了if標(biāo)簽和where 標(biāo)簽。 下面來介紹兩種標(biāo)簽的用法。
if標(biāo)簽
<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標(biāo)簽有些類似于EL表達(dá)式的使用,test中可以直接寫入類中的屬性或者key值。
where標(biāo)簽
<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標(biāo)簽 替換了前一段代碼的 where 1=1 。 mybatis中的where 標(biāo)簽會判斷標(biāo)簽內(nèi)是否有內(nèi)容, 如果有內(nèi)容就自動生成where 并把 where 后面的第一個and +一個空格,or+一個空格 去掉。
choose , when 和 otherwise 標(biāo)簽
<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>
當(dāng)所有條件不滿足時,執(zhí)行otherwise標(biāo)簽的內(nèi)容。
trim標(biāo)簽
<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標(biāo)簽的屬性及其含義
- - prefix : 標(biāo)簽之間有內(nèi)容在最前面加入
- - prefixOverrides: 檢查內(nèi)容的最前面是否匹配,匹配就刪除
- - suffix: 標(biāo)簽之間有內(nèi)容在最后面加入
- - suffixOverrides:檢查內(nèi)容的最后面是否匹配,匹配就刪除
set標(biāo)簽
set標(biāo)簽常用于update操作,并且會自動抹掉無關(guān)的,
<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標(biāo)簽
foreach 用于處理數(shù)組或者list集合,下面是一個批量添加的例子
<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>
其中 如果參數(shù)為數(shù)組 則collection只能為“array” 參數(shù)為List集合則collection只能為 “l(fā)ist” item類似JSTL 中的var的作用, 指代容器中的每一個對象。separator=”,”的含義是每條數(shù)據(jù)以 , 分割。 未注明的屬性有 open 和 close 他們的含義是在遍歷開始和結(jié)束時分別添加其內(nèi)容。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Java優(yōu)化for循環(huán)嵌套的高效率方法
這篇文章主要介紹了Java優(yōu)化for循環(huán)嵌套的高效率方法,幫助大家更好的提升java程序性能,感興趣的朋友可以了解下2020-09-09Spring Boot 和 Spring 到底有啥區(qū)別你知道嗎
Spring Boot框架的核心就是自動配置,只要存在相應(yīng)的jar包,Spring就幫我們自動配置。接下來通過本文給大家介紹Spring與Spring boot的區(qū)別介紹,非常不錯,需要的朋友參考下吧2021-08-08RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報錯404的解決
這篇文章主要介紹了RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報錯404的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10java動態(tài)代理和cglib動態(tài)代理示例分享
這篇文章主要介紹了java動態(tài)代理和cglib動態(tài)代理示例,JDK1.3之后,Java提供了動態(tài)代理的技術(shù),允許開發(fā)者在運行期間創(chuàng)建接口的代理實例,下面我們使用示例學(xué)習(xí)一下2014-03-03