mybatis中参数是一个list,而传的形式是一个map
<if test="params.typeList != null">
AND t.type IN
<foreach collection="params.typeList" item="type" index="index" open="(" close=")" separator=",">
#{params.typeList[${index}]}
</foreach>
</if>
oracle批量插入数据
<insert id="insertFinanceReceptionObjs" parameterType="java.util.List">
insert into FIN_MAIN_RECEPTION_OBJECT
(id,fid,name,position,com,createdate,createuser,modifyuser,modifydate,del_flag)
SELECT A.*
FROM(
<foreach collection="list" item="item" index="index" separator="UNION ALL" >
SELECT
#{item.id,jdbcType=VARCHAR} ID,
#{item.fid,jdbcType=VARCHAR} FID,
#{item.name,jdbcType=VARCHAR} NAME,
#{item.position,jdbcType=VARCHAR} POSITION,
#{item.com,jdbcType=VARCHAR} COM ,
#{item.createdate,jdbcType=TIMESTAMP} CREATEDATE,
#{item.createuser,jdbcType=VARCHAR} CREATEUSER,
#{item.modifyuser,jdbcType=VARCHAR} MODIFYUSER,
#{item.modifydate,jdbcType=TIMESTAMP} MODIFYDATE,
#{item.delFlag,jdbcType=VARCHAR} DEL_FLAG
FROM DUAL
</foreach>
) A
</insert>
mysql批量插入数据
<insert id="addTrainRecordBatch" useGeneratedKeys="true" parameterType="java.util.List">
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into t_train_record (add_time,emp_id,activity_id,flag)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.addTime},#{item.empId},#{item.activityId},#{item.flag})
</foreach>
</insert>
对于foreach标签的解释参考了网上的资料,具体如下:
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map
批量更新
<update id="updateLicaiAllList" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";"> update tmi_licai_all t set t.est_amount=#{item.estAmount,jdbcType=NUMERIC} where t.licai_id = #{item.licaiId,jdbcType=NUMERIC} </foreach> </update>
可能需要将 open="begin" close=";end;"清空 open="" close=""
oracle和mysql数据库的批量update在mybatis中配置不太一样:
oracle数据库:
<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";"> update test <set> test=${item.test}+1 </set> where id = ${item.id} </foreach> </update>
mysql数据库:
mysql数据库采用一下写法即可执行,但是数据库连接必须配置:&allowMultiQueries=true
例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update test <set> test=${item.test}+1 </set> where id = ${item.id} </foreach> </update>
批量删除
一. 这里主要考虑两种参数类型:数组或者集合.
而这点区别主要体现在EmpMapper.xml文件中标签的collection属性:
- 当collection=”array“时,表名参数为数组;
- 当collection=”list“时,表名参数为集合.
二. 注意:
无论Mybatis是与MySQL数据库结合,还是与Oracle数据库,都同样适合如下设置与操作.
三. 具体示例如下:
EmpMapper.xml:
<!-- 批量删除员工信息 --> <delete id="batchDeleteEmps" parameterType="int"> delete from emp where empno in <foreach item="empnoItem" collection="array" open="(" separator="," close=")"> #{empnoItem} </foreach> </delete> <!-- 参数说明 --> <!-- emp : 表名 empno : 字段名 collection:表示类型,这里参数是数组,就写成array,如果是集合,就写成list item : 是一个变量名,自己随便起名 -->
EmpMapper.Java :
/* * 批量删除员工信息 */ void batchDeleteEmps(int[] empno);