mybatis 动态sql之Map参数
Mapper文件:
test表达式中不用再加#,$之类的取值符了,就直接这样写就可以取到map中key所对应的值,而其他地方需要有#{map中的key}来取得map中该key所对应的值
<pre name="code" class="html">
后台传递到mybatis的map参数,不要深究函数含义,知道下面这个map最终是传递到mybatis中的parameterType就够了
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语句:
<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参数类型:
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写上:
List<WeixinUserLocationList> findweixinUserLocations(@Param("params") Map<String, Object> map);注意就是注解@param 这个,是mybatis的
然后在xml中这样写:
<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之类的。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接