首页 > 其他分享 >mybatis foreach循环使用的两种传参方式

mybatis foreach循环使用的两种传参方式

时间:2023-09-26 16:25:16浏览次数:41  
标签:传参 Mapper selectByIds List ids Param foreach mybatis

方式一:传参ids是用逗号隔开

Mapper.java
	List<> selectByIds(@Param("ids") String ids);
    
Mapper.xml
    <select id="selectByIds" parameterType="String" resultType="String">
		select * from table a where a.id in
		<foreach item="item" index="index" collection="ids.split(',')"
	               open="(" separator="," close=")"> #{item}
	    </foreach>
    </select>

方式二:传参ids是用List<String> 集合

Mapper.java
		List<> selectByIds(@Param("ids") List<String> ids);


 Mapper.xml
	  <select id="selectByIds" parameterType="list" resultMap="BaseResultMap">
		select * from table a where a.id in
	    <foreach item="item" index="index" collection="ids"
                 open="(" separator="," close=")"> #{item}
        </foreach>
	</select>

  

标签:传参,Mapper,selectByIds,List,ids,Param,foreach,mybatis
From: https://www.cnblogs.com/jsliao/p/17730341.html

相关文章

  • Mybatis-Plus 系列:简介和基本使用
    目录一、简介二、特性三、基本使用1、初始化数据库2、初始化工程3、精简SpringBoot相关日志一、简介官网:https://www.baomidou.comMyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,主要作用为简化开发、提高效率。二、特性无侵入:只做增强不做改......
  • Kotlin | 在for、forEach循环中正确的使用break、continue
    Kotlin有三种结构化跳转表达式:return:默认从最直接包围它的函数或者匿名函数返回。break:终止最直接包围它的循环。continue:继续下一次最直接包围它的循环。for循环中使用break、continuefor(iin1..5){if(i==3)break//1这里分别使用breakcontinuereturnprintl......
  • mybatis中使用in
     <iftest="areaCode!=nullandareaCode!=''">andAREACODEin(${areaCode})</if> 如果直接传入拼接好的wherein条件,比如('111','222','333'),则需要使用${areaCode}传参,即绝对引用,而不能使用#......
  • java——mybatis随笔
    教程:https://www.cnblogs.com/xiaobaibailongma/p/17019484.html    本地示例:https://www.cnblogs.com/xiaobaibailongma/p/17019676.html      =========================================================================      gitee:示例......
  • Springboot+Mybatis(四)
    单独说一下Mybatis-plus上一篇中介绍了Mybatis的使用方法,对于Mybatisplus,我理解就是把一些方法提前封装好了,不需要自己定义接口类中的内容只需要直接调用即可,把接口类添加继承关系 这里要说的是BaseMapper后面的<User>是要搜索的类的名词,且最好类的名字和表的名字保持一致,......
  • mybatis plus生成的日期时间格式LocalDateTime与String的相互转换
    mybatisplus生成的日期时间格式为LocalDateTime LocalDateTime转为String:将现在的时间转StringStringnowDate=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss"))  将指定的String日期转DatetimeLocalDateTimeldatetime=Lo......
  • Springboot+Mybatis(三)
    今天学习了如何使用Mybatis实现增删改查为此做一个梳理发表一篇博客,也是为了总结一下首先,要使用Mybatis需要添加依赖从建立项目的时候选择java8+Springboot2这样的方式,避免高版本存在一些兼容性的问题然后添加Mybatis-plus依赖,老师讲的是添加了plus会自动添加Mybatis的依赖,但......
  • MyBatis MySQL limit分页含运算动态语句
    MySQL基本的分页语句select*fromtablelimitoffset,pageSize正常情况下是没问题的,如果offset是动态的select*fromtablelimit(pageNum-1)*pageSize,pageSize这样就无法执行了。具体原因可以在MySQL中直接执行这种含运算符的语句试下就知道了。现在给出两种解决方案使用MySQ......
  • mybatis大于小于等于的写法
    第一种写法(1):原符号<<=>>=&'"替换符号<<=>>=&'"例如:sql如下:create_date_time>=#{startTime}andcreate_date_time<=#{endTime}第二种写法(2):大于等于=]]>小于等于例如:sql如下:create_date_time=]]>#{startTime}......
  • 记一次操蛋的springboot整合mybatis的配置
    这是我的xml映射器配置:点击查看代码<mappernamespace="com.yige.askroadserver.dao.AdminMapper"><insertid="insertAdmin"parameterType="com/yige/askroadmodel/entity/user/Admin">--INSERTINTOadmin_info(na......