Mybatis传入参数类型为Map

参考:

https://www.cnblogs.com/seeusmile-cnblog/p/6221340.html

https://www.cnblogs.com/huzi007/p/5969711.html

方式一:

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之类的。

  • 27
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Mybatis可以使用Map类型作为参数传递给Mapper接口方法。通过使用Map类型作为参数,可以在SQL语句中直接使用Map的key来获取参数值。比如,在Mapper接口中定义一个使用Map类型作为参数的方法,如下所示: ```java User checkLoginByMap(Map<String, Object> map); ``` 在Mapper的XML文件中,可以通过`#{key}`来获取Map中对应key的value值,如下所示: ```xml <select id="checkLoginByMap" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND password = #{password} </select> ``` 这样,在调用Mapper接口方法时,可以将参数封装到一个Map中传递给Mapper方法,Mybatis会自动将Map中的key-value对映射到SQL语句中的参数位置,执行查询并返回结果。 请注意,使用Map作为参数传递可能会导致SQL语句的可读性降低,因为无法直观地了解参数的含义。因此,在实际开发中,应根据具体情况选择使用Map还是自定义的POJO类作为参数类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mybatis传参1 - 传入map类型参数](https://blog.csdn.net/m0_53753920/article/details/124155904)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【MyBatis】04 - MyBatis 使用Map传参](https://blog.csdn.net/nyc__/article/details/119221970)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值