IBATIS中关于iterate$与#的应用
生活随笔
收集整理的這篇文章主要介紹了
IBATIS中关于iterate$与#的应用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)包含List元素的HashMap參數(shù)賦給sqlMap
public int getCountById(String id, String title, List ids) throws Exception { ?
??????? Map paramMap=new HashMap(); ?
??????? paramMap.put("id", id); ?
??????? paramMap.put("title", title); ?
??????? paramMap.put("ids",ids); ?
??????? return (int) this.sqlMapClient.queryForObject("getCountById", paramMap); ?
??? }
上面的ids元素是一個(gè)List,里面包含了一個(gè)id列表。id和title都是String。
<select id="getOrderformByBiOid" parameterClass="java.util.HashMap" resultMap="Result"> ?
??????? select *? ?
??????? from MYTABLE??? ?
??????? <dynamic prepend="where"> ?
?????????? <isNotNull prepend="and" property="id"> ?
???????????? ID=#id# ?
?????????? </isNotNull> ?
?????????? <isNotNull prepend="and" property="title"> ?
???????????? TITLE like '%$title$%'
?????????? </isNotNull> ?
?????????? <isNotNull prepend="and" property="ids"> ?
???????????? IDS in ?
??????????? <iterate? property="ids" open="(" close=")" conjunction=","> ?
???????????? #ids[]# ?
??????????? </iterate> ?
?????????? </isNotNull> ?
??????? </dynamic> ?
??????? order by TIME DESC ?
??? </select>
在上面的sqlMap中要注意的地方有兩個(gè)。第一個(gè)地方是,<iterate>標(biāo)簽中一定要記得寫property="ids",不要因?yàn)樵谏厦娴?
<isNotNull prepend="and" property="ids">標(biāo)簽中判斷了該屬性,就在<iterate>這個(gè)標(biāo)簽中忽略了property的書寫(如果你忘了
寫iterate中的property屬性的話系統(tǒng)會(huì)報(bào)這樣的錯(cuò)誤Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or
property was not a Collection, Array or Iterator.),并且要記住,
#ids[]# 中的‘[]’符號(hào),只有這樣ibatis才把ids作為一個(gè)可迭代的對(duì)象(list,數(shù)組...),不然的話ibatis會(huì)把ids作為普通的string來(lái)處理。
第二個(gè)地方就是,上面的關(guān)于‘$‘與‘#’的應(yīng)用。
??????????? 下面就來(lái)說(shuō)說(shuō)關(guān)于‘$‘與‘#’的應(yīng)用。在通常情況下ibatis的參數(shù)在sqlmap中使用#param#的形式,參數(shù)名以’#‘包著,但當(dāng)使用
sql的like語(yǔ)句時(shí)就發(fā)生了問(wèn)題,在單引號(hào)中無(wú)法使用#param#這種形式。解決辦法有:
??????????? 1.當(dāng)應(yīng)用select * from? table1 where? col like ’value%’時(shí),如果要把‘value’以參數(shù)代替,可以把整個(gè)like后面的字符串
全改為參數(shù)。即:select * from? table1 where? col like #param#? ,此時(shí)參數(shù)param的值為字符串"value%"
?????????? 2.使用‘$’將參數(shù)名包起來(lái)。例如:name like ‘%#name#%’。我們的解決方法有2。(a)把name like ‘%#name#%’的
#換成$,即:name like ‘%$name$%’。(b)用||連接字符串的方式,寫成,name like ‘%’|| #name#‘%’。
public int getCountById(String id, String title, List ids) throws Exception { ?
??????? Map paramMap=new HashMap(); ?
??????? paramMap.put("id", id); ?
??????? paramMap.put("title", title); ?
??????? paramMap.put("ids",ids); ?
??????? return (int) this.sqlMapClient.queryForObject("getCountById", paramMap); ?
??? }
上面的ids元素是一個(gè)List,里面包含了一個(gè)id列表。id和title都是String。
<select id="getOrderformByBiOid" parameterClass="java.util.HashMap" resultMap="Result"> ?
??????? select *? ?
??????? from MYTABLE??? ?
??????? <dynamic prepend="where"> ?
?????????? <isNotNull prepend="and" property="id"> ?
???????????? ID=#id# ?
?????????? </isNotNull> ?
?????????? <isNotNull prepend="and" property="title"> ?
???????????? TITLE like '%$title$%'
?????????? </isNotNull> ?
?????????? <isNotNull prepend="and" property="ids"> ?
???????????? IDS in ?
??????????? <iterate? property="ids" open="(" close=")" conjunction=","> ?
???????????? #ids[]# ?
??????????? </iterate> ?
?????????? </isNotNull> ?
??????? </dynamic> ?
??????? order by TIME DESC ?
??? </select>
在上面的sqlMap中要注意的地方有兩個(gè)。第一個(gè)地方是,<iterate>標(biāo)簽中一定要記得寫property="ids",不要因?yàn)樵谏厦娴?
<isNotNull prepend="and" property="ids">標(biāo)簽中判斷了該屬性,就在<iterate>這個(gè)標(biāo)簽中忽略了property的書寫(如果你忘了
寫iterate中的property屬性的話系統(tǒng)會(huì)報(bào)這樣的錯(cuò)誤Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or
property was not a Collection, Array or Iterator.),并且要記住,
#ids[]# 中的‘[]’符號(hào),只有這樣ibatis才把ids作為一個(gè)可迭代的對(duì)象(list,數(shù)組...),不然的話ibatis會(huì)把ids作為普通的string來(lái)處理。
第二個(gè)地方就是,上面的關(guān)于‘$‘與‘#’的應(yīng)用。
??????????? 下面就來(lái)說(shuō)說(shuō)關(guān)于‘$‘與‘#’的應(yīng)用。在通常情況下ibatis的參數(shù)在sqlmap中使用#param#的形式,參數(shù)名以’#‘包著,但當(dāng)使用
sql的like語(yǔ)句時(shí)就發(fā)生了問(wèn)題,在單引號(hào)中無(wú)法使用#param#這種形式。解決辦法有:
??????????? 1.當(dāng)應(yīng)用select * from? table1 where? col like ’value%’時(shí),如果要把‘value’以參數(shù)代替,可以把整個(gè)like后面的字符串
全改為參數(shù)。即:select * from? table1 where? col like #param#? ,此時(shí)參數(shù)param的值為字符串"value%"
?????????? 2.使用‘$’將參數(shù)名包起來(lái)。例如:name like ‘%#name#%’。我們的解決方法有2。(a)把name like ‘%#name#%’的
#換成$,即:name like ‘%$name$%’。(b)用||連接字符串的方式,寫成,name like ‘%’|| #name#‘%’。
轉(zhuǎn)載于:https://my.oschina.net/wowona/blog/52957
總結(jié)
以上是生活随笔為你收集整理的IBATIS中关于iterate$与#的应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Silverlight实例教程 - Va
- 下一篇: nginx添加ssl证书认证