mybatis动态sql之Map参数的讲解
mybatis动态sql之Map参数
Mapper文件:
select*fromprod_purchasewhere1=1 andpurc_id=#{purc_id} andprod_id=#{prod_id} andch_id=#{ch_id} andch_idin(selectch_idfromchannelwherech_name like'%#{ch_name}%') andpurc_time=#{purc_time}orderby#{purc_time}desc
test表达式中不用再加#,$之类的取值符了,就直接这样写就可以取到map中key所对应的值,而其他地方需要有#{map中的key}来取得map中该key所对应的值
后台传递到mybatis的map参数,不要深究函数含义,知道下面这个map最终是传递到mybatis中的parameterType就够了
publicMapparseMap(HttpServletRequestreq){ Map map=newHashMap (); 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); returnmap; }
Mybatis传入参数类型为Map
方式一:
mybatis更新sql语句:
updatetest setcreatedate=#{createdate}, creator=#{creator} whereidin #{ids}
传入map参数类型:
HashMapmap=newHashMap (); map.put("creator","creator"); map.put("createdate","createdate"); String[]ids={"1","2"}; map.put("ids",ids);
方式二:
第一步在你的mapper写上:
ListfindweixinUserLocations(@Param("params")Map map);
注意就是注解@param这个,是mybatis的
然后在xml中这样写:
anda.accountid=#{params.accountId} anda.nicknamelike'%${params.nickname}%' anddate_format(a.createtime,'%Y-%m-%d')>=${params.beginDate}
${params.nickname}这种写法参数默认是传字符串,#{params.accountId}可以取Long,Integer之类的。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接