Mybatis中使用集合、数组
一、简述
本文讲Mybatis中如何将传入参数为数组或者集合对象,进行遍历,组合Where条件中如in条件等内容。
有3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .或者使用传入参数中@param定义的参数名称。
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .或者使用传入参数中@param定义的参数名称。
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
二、例子
<!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -->
<select id="getEmployeesListParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="list" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select> <!--Array:forech中的collection属性类型是array,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -->
<select id="getEmployeesArrayParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="array" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select> <!--Map:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的departmentId -->
<select id="getEmployeesMapParams" resultType="Employees">
select *
from EMPLOYEES e
<where>
<if test="departmentId!=null and departmentId!=''">
e.DEPARTMENT_ID=#{departmentId}
</if>
<if test="employeeIdsArray!=null and employeeIdsArray.length!=0">
AND e.EMPLOYEE_ID in
<foreach collection="employeeIdsArray" item="employeeId"
index="index" open="(" close=")" separator=",">
#{employeeId}
</foreach>
</if>
</where>
</select> Mapper类:
public interface EmployeesMapper { List<Employees> getEmployeesListParams(List<String> employeeIds); List<Employees> getEmployeesArrayParams(String[] employeeIds); List<Employees> getEmployeesMapParams(Map<String,Object> params);
} 根据数组批量查询
List<Privilege> selectPrivilegeByIds(@Param("privilegeIds") Integer[] privilegeIds); <select id="selectPrivilegeByIds" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from diary_privilege
where id in
<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
根据集合插入批量插入 int insertEmployeeRole(EmployeeRoleVo employeeRole);(EmployeeRoleVo中包含List<Role> roleList) <insert id="insertEmployeeRole" parameterType="com.jimmy.demo.vo.EmployeeRoleVo" >
insert into diary_employee_role (employeeId,roleId)
values
<foreach collection="roleList" item="item" index="index" separator="," >
(#{employee.eid},#{item.id})
</foreach>
</insert>
本文例子参考自:https://www.cnblogs.com/jimmy-muyuan/p/5467252.html 非常感谢原作者煮海焚天分享。
Mybatis中使用集合、数组的更多相关文章
- mybatis中传集合时 报异常 invalid comparison: java.util.Arrays$ArrayList and java.lang.String
犯了一个低级的错误,在传集合类型的参数时,把他当成字符串处理了,导致报类型转换的错误 把 and nsrsbh!=' ' 删掉就行了
- 在MyBatis中,前台传数组批量传id处理数据方式
<update id = "dishBatchSaleOrDown"> <if test="ids != null"> <if t ...
- 4.产生10个1-100的随机数,并放到一个数组中 (1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。 (2)把数组中的数字放到当前文件夹的numArr.txt文件中
package cn.it.text; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayLis ...
- 【mybatis】mybatis中 返回map集合
关于mybatis返回map集合的操作: 1.mapper.xml中写一个查询返回map的sql <select id="findMap" parameterType=&qu ...
- mybatis中参数为list集合时使用 mybatis in查询
mybatis中参数为list集合时使用 mybatis in查询 一.问题描述mybatis sql查询时,若遇到多个条件匹配一个字段,sql 如: select * from user where ...
- Mybatis中的in查询和foreach标签
Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- mybatis ForEach Collection集合等规范解析(转)
转自:http://blog.csdn.net/wj3319/article/details/9025349 在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了fo ...
- mybatis中常见的问题总结
如下所有举例基于springboot+mybatis项目中,SSH使用mybatis的写法也一样,只是形式不同而已 问题1.org.apache.ibatis.binding.BindingExcep ...
随机推荐
- 055 kafka可靠性与高性能
一:可靠性 1. 二:高性能 1.
- 048 SparkSQL自定义UDAF函数
一:程序 1.需求 实现一个求平均值的UDAF. 这里保留Double格式化,在完成求平均值后与系统的AVG进行对比,观察正确性. 2.SparkSQLUDFDemo程序 package com.sc ...
- javascript记忆
Math.round()和ToFixed() Math.round(1.6)=2 Math.round(-1.4)=-1 var k = 1.74.toFixed(1), m = 1.75.toFix ...
- Python NLTK 自然语言处理入门与例程(转)
转 https://blog.csdn.net/hzp666/article/details/79373720 Python NLTK 自然语言处理入门与例程 在这篇文章中,我们将基于 Pyt ...
- Codeforces 853B Jury Meeting (差分+前缀和)
<题目链接> 题目大意: 有$ n(n<=1e5)$个城市和一个首都(0号城市),现在每个城市有一个人,总共有$ m (m<=1e5)$次航班,每个航班要么从首都起飞,要么飞到 ...
- MyEclipse、Eclipse使用配置及部分问题
简单总结一下myeclipse首次使用的配置,eclipse类似.总结的不是很全面,如有新的看法,欢迎下方留言. 最优设置 1.myeclipse激活 myeclipse.eclipse程序及激活工具 ...
- Tomcat v9.0 Could not publish to the server. java.lang.IndexOutOfBoundsException
今天使用Tomcat启动一个java项目,出现报错: Could not publish to the server. java.lang.IndexOutOfBoundsException 众寻之下 ...
- http Content-Type 知多少
前言 作为前端开发,工作中少不了与接口请求打交道.对于常见的content-type,也能说上来几个,感觉还算了解.直到有一天,我要在查看google的批量接口合并时发现Content-Type: m ...
- logback中logger详解
前言 logback实践笔记 上一篇主要对root进行了实践总结,现在基于上一篇中的springboot代码环境对logback.xml中的logger来进行实践和自己遇到的坑. logger简介 ...
- MySQL Unable to convert MySQL datetime value to System.DateTime 解决方案
Unable to convert MySQL date/time value to System.DateTime 解决方案 这个问题发生在MySQL数据里面有Date类型数据,在C#中查询出来时候 ...