mybatis动态sql之Map参数的讲解
mybatis 动态sql之Map参数
Mapper文件:
<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就够了
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之类的。
总结
以上是 mybatis动态sql之Map参数的讲解 的全部内容, 来源链接: utcz.com/z/323879.html