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