mybatis的使用-Mapper文件各種語(yǔ)法介紹
一、查詢
mybatis自定義查詢條件,queryString、queryMap、limit,Mapper文件寫(xiě)法如下:
<select parameterType='com.systom.base.BaseDaoQueryParam' resultMap='BaseResultMap'> SELECT * FROM user WHERE 1 = 1 <if test='paramString != null'> and ${paramString} </if> <foreach collection='paramMap.keys' item='k' separator=''> <if test='null != paramMap[k]'> and ${k} = #{paramMap.${k}} </if> </foreach> <if test='paramInt1 != null and paramInt1 > 0 and paramInt2 != null and paramInt2 > 0'> limit #{paramInt1,jdbcType=INTEGER}, #{paramInt2,jdbcType=INTEGER} </if> </select>
以及傳入?yún)⑷氲腷ean類(lèi):
package com.systom.base; import java.io.Serializable;import java.util.HashMap;import java.util.Map; public class BaseDaoQueryParam implements Serializable { private static final long serialVersionUID = -8917191044499296040L; private String paramString; private Map<String, Object> paramMap = new HashMap<String, Object>(); private int paramInt1; private int paramInt2; private String orderBy; private String orderType; public BaseDaoQueryParam(String paramString, Map<String, Object> paramMap, int paramInt1, int paramInt2) { super(); this.paramString = paramString; if(paramMap != null) this.paramMap = paramMap; this.paramInt1 = paramInt1; this.paramInt2 = paramInt2; } public BaseDaoQueryParam(String paramString, Map<String, Object> paramMap, int paramInt1, int paramInt2, String orderBy, String orderType) { super(); this.paramString = paramString; if(paramMap != null) this.paramMap = paramMap; this.paramInt1 = paramInt1; this.paramInt2 = paramInt2; this.orderBy = orderBy; this.orderType = orderType; } public String getParamString() { return paramString; } public void setParamString(String paramString) { this.paramString = paramString; } public Map<String, Object> getParamMap() { return paramMap; } public void setParamMap(Map<String, Object> paramMap) { this.paramMap = paramMap; } public int getParamInt1() { return paramInt1; } public void setParamInt1(int paramInt1) { this.paramInt1 = paramInt1; } public int getParamInt2() { return paramInt2; } public void setParamInt2(int paramInt2) { this.paramInt2 = paramInt2; } public String getOrderBy() { return orderBy; } public void setOrderBy(String orderBy) { this.orderBy = orderBy; } public String getOrderType() { return orderType; } public void setOrderType(String orderType) { this.orderType = orderType; }}
補(bǔ)充 知識(shí):mybatis的mapper文件的大于號(hào)特殊符號(hào)使用
第一種方法:
用了轉(zhuǎn)義字符把>和<替換掉,然后就沒(méi)有問(wèn)題了。
SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DATE AND end_date >= CURRENT_DATE
附:XML轉(zhuǎn)義字符
<
<
小于號(hào)
>
>
大于號(hào)
&
&
和
'
’
單引號(hào)
"
'
雙引號(hào)
第二種方法:
因?yàn)檫@個(gè)是xml格式的,所以不允許出現(xiàn)類(lèi)似“>”這樣的字符,但是都可以使用<![CDATA[ ]]>符號(hào)進(jìn)行說(shuō)明,將此類(lèi)符號(hào)不進(jìn)行解析
你的可以寫(xiě)成這個(gè):
mapper文件示例代碼
<![CDATA[ when min(starttime)<=’12:00’ and max(endtime)<=’12:00’ ]]>
在mybatis 的mapper配置文件sql語(yǔ)句中, 有時(shí)用到 大于, 小于等等的比較, 直接寫(xiě)在里面就被當(dāng)做標(biāo)簽的開(kāi)頭來(lái)處理了, 所以不可.現(xiàn)在又2種解決方法:
一, 用<![CDATA[ ]]>標(biāo)識(shí),例如:
<if test='menu.authority != null'> <![CDATA[ and authority < #{menu.authority}]]> </if>
其中不但能用大于’>’, 小于’<’, 小于等于’<=’, 大于等于’>=’ 也是可以的.
二, 轉(zhuǎn)義, 例如:
<if test='menu.authority != null'> and authority < #{menu.authority} </if>
如此這般......
同樣可以可以和等號(hào)’=’一起來(lái)使用, 來(lái)表示大于等于, 小于等于等.如
<if test='menu.authority != null'> and authority >= #{menu.authority} </if>
以上這篇mybatis的使用-Mapper文件各種語(yǔ)法介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. MariaDB數(shù)據(jù)庫(kù)的外鍵約束實(shí)例詳解2. MariaDB的安裝與配置教程3. MariaDB性能調(diào)優(yōu)工具mytop的使用詳解4. Window7安裝MariaDB數(shù)據(jù)庫(kù)及系統(tǒng)初始化操作分析5. access不能打開(kāi)注冊(cè)表關(guān)鍵字錯(cuò)誤處理方法(80004005錯(cuò)誤)6. SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串7. SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)8. SQLite 性能優(yōu)化實(shí)例分享9. Centos7 下mysql重新啟動(dòng)MariaDB篇10. centos 7安裝mysql5.5和安裝 mariadb使用的命令
