mybatis 動態sql之map參數
mapper文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<mapper namespace= "com.cn.shoje.oa.modules.logistics.dao.purcdao" > <select id= "findall" parametertype= "map" resulttype= "purchase" > select * from prod_purchase where 1 = 1 < if test= "purc_id!=''" > and purc_id=#{purc_id}</ if > < if test= "prod_id!=''" > and prod_id=#{prod_id}</ if > < if test= "ch_id!=''" > and ch_id=#{ch_id}</ if > < if test= "ch_name!=''" > and ch_id in ( select ch_id from channel where ch_name like '%#{ch_name}%' )</ if > < if test= "purc_time!=''" > and purc_time=#{purc_time} order by #{purc_time} desc </ if > </select> </mapper> |
test表達式中不用再加#,$之類的取值符了,就直接這樣寫就可以取到map中key所對應的值,而其他地方需要有#{map中的key}來取得map中該key所對應的值
<pre name="code" class="html">
后臺傳遞到mybatis的map參數,不要深究函數含義,知道下面這個map最終是傳遞到mybatis中的parametertype就夠了
1
2
3
4
5
6
7
8
9
|
public map<string,string> parsemap(httpservletrequest req){ map<string,string> map= new hashmap<string,string>(); map.put( "prod_id" , prod_id); map.put( "purc_id" , purc_id ); map.put( "ch_name" , ch_name ); map.put( "ch_id" , ch_id); map.put( "purc_time" , purc_time); return map; } |
mybatis傳入參數類型為map
方式一:
mybatis更新sql語句:
1
2
3
4
5
6
7
8
9
|
<update id= "publisht00_notice" parametertype= "map" > update test set createdate = #{createdate}, creator = #{creator} where id in <foreach collection= "ids" item= "ids" separator= "," open= "(" close= ")" > #{ids} </foreach> </update> |
傳入map參數類型:
1
2
3
4
5
|
hashmap<string,object> map = new hashmap<string, object>(); map.put( "creator" , "creator" ); map.put( "createdate" , "createdate" ); string[] ids = { "1" , "2" }; map.put( "ids" , ids ); |
方式二:
第一步在你的mapper寫上:
1
|
list<weixinuserlocationlist> findweixinuserlocations( @param ( "params" ) map<string, object> map); |
注意就是注解@param 這個,是mybatis的
然后在xml中這樣寫:
1
2
3
4
5
6
7
8
9
10
11
12
|
< if test= "params.accountid!=null" > and a.accountid=#{params.accountid} </ if > < if test= "params.nickname!=null and params.nickname !=''" > and a.nickname like '%${params.nickname}%' </ if > < if test= "params.begindate!=null and params.begindate!=''" > and date_format(a.createtime, '%y-%m-%d' )>=${params.begindate} </ if > < if test= "params.enddate!=null and params.enddate!=''" > <![cdata[ and date_format(a.createtime, '%y-%m-%d' )<=${params.enddate} ]]> </ if > |
${params.nickname}這種寫法參數默認是傳字符串,#{params.accountid}可以取long,integer之類的。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/u012557538/article/details/50978371