栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis动态sql之Map参数的讲解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis动态sql之Map参数的讲解

mybatis 动态sql之Map参数

Mapper文件:


 
 select * from prod_purchase where 1=1
  and purc_id=#{purc_id}
  and prod_id=#{prod_id}
  and ch_id=#{ch_id}
  and ch_id in ( select ch_id from channel where ch_name
  like '%#{ch_name}%')
  and purc_time=#{purc_time} order by #{purc_time} desc
 
 

test表达式中不用再加#,$之类的取值符了,就直接这样写就可以取到map中key所对应的值,而其他地方需要有#{map中的key}来取得map中该key所对应的值

后台传递到mybatis的map参数,不要深究函数含义,知道下面这个map最终是传递到mybatis中的parameterType就够了

public Map parseMap(HttpServletRequest req){
 Map map=new HashMap();
 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 test 
set createdate = #{createdate},
creator = #{creator}
where id in 

#{ids}

传入map参数类型:

HashMap map = new HashMap();
map.put("creator", "creator");
map.put("createdate", "createdate");
String[] ids = {"1","2"};
map.put("ids", ids );

方式二:

第一步在你的mapper写上:

List findweixinUserLocations(@Param("params") Map map);

注意就是注解@param 这个,是mybatis的

然后在xml中这样写:


      and a.accountid=#{params.accountId}
    
    
      and a.nickname like '%${params.nickname}%'
    
    
      and date_format(a.createtime,'%Y-%m-%d')>=${params.beginDate}
    
    
       
    

${params.nickname}这种写法参数默认是传字符串,#{params.accountId}可以取Long,Integer之类的。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号