mybatis使用foreach批量操作

参考博文
https://www.cnblogs.com/aigeileshei/p/6109355.html
https://www.cnblogs.com/anruy/p/5942044.html
https://blog.csdn.net/wuuushao/article/details/53038506
https://blog.csdn.net/qq_34657993/article/details/72938739
MyBatis 插入空值时,需要指定JdbcType.如#{name,jdbcType=VARCHAR}
参考以下博文
https://blog.csdn.net/qq_34122822/article/details/79506928

Mybatis中提供的foreach功能,可以帮助我们动态构建in集合条件查询和批量插入更新数据

参数说明
item
循环体中的具体对象,支持属性的点路径访问,item.age,item.info.details。
具体说明:若collection属性为list或array,则item代表list或array里面的一个元素。若collection属性对应一个map,则item代表的是map中的value集合中的单个value,该参数为必选。

collection
foreach遍历的对象,作为参数传入时,List对象默认用list(collection=”list”)代替作为键,数组对象有array(collection=”array”)代替作为键,Map对象没有默认的键。
当然在作为入参时可以使用@Param(“params”)来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = “ids”
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = “ids.id”
如果传入参数类型为map,这个入参有注解@Param(“params”),则map的所有的key集合可以写成params.keys,所有值集合可以写成params.values。这样foreach就可以对key集合或值集合进行迭代了。
该参数为必选。

separator
元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。

open
foreach代码的开始符号,一般是(和close=”)”合用。常用在in(),values()时。该参数可选。

close
foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。该参数可选。

index
在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

代码示例,演示查询的三种情况
传入list参数

//对应的Dao中的Mapper文件
public List<User> selectByIds(List<Integer> ids);

<select id="selectByIds" resultType="com.wuuushao.pojo.User">
        select * from user where id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
</select>

传入的参数为Array

//对应的Dao中的Mapper文件
public List<User> selectByIds(int[] ids);

<select id="selectByIds" resultType="com.wuuushao.pojo.User">
        select * from user where id in
        <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

传入的参数为Map

//对应的Dao中的Mapper文件
public List<User> selectByIds(Map<String, Object> params);

<select id="selectByIds" resultType="com.wuuushao.pojo.User">
        select * from user where  id in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
//map的时候需要注意的是:collection的值“ids”是存储在map中的key
//(比如:map.put("ids",ids));这个不是随便乱写的,尤其需要注意;

再举一些插入更新的例子

//批量插入数据,传入参数list 
     <insert id="insertInfo">
        insert into tableName(
            id....
        )
        values
        <foreach collection="list" item="entity" index="index" 
        separator=",">
        (
            #{entity.id,jdbcType=VARCHAR}....
         )
        </foreach>
    </insert>

解释一下jdbcType=VARCHAR
在执行SQL时MyBatis会自动通过对象中的属性给SQL中参数赋值,它会自动将Java类型转换成数据库的类型。
而一旦传入的是null它就无法准确判断这个类型应该是什么,就有可能将类型转换错误,从而报错。
要解决这个问题,需要针对这些可能为空的字段,手动指定其转换时用到的类型。
一般情况下,我们没有必要按个字段去识别/判断它是否可以为空,而是将所有的字段都当做可以为空,全部手动设置转换类型。
(以前从来没有人说过要写这些)

<insert id="save"  
        parameterType="com.tarena.entity.Cost">  
        insert into cost values(  
            cost_seq.nextval,  
            #{name,jdbcType=VARCHAR},  
            #{base_duration,jdbcType=INTEGER},  
            #{base_cost,jdbcType=DOUBLE},  
            #{unit_cost,jdbcType=DOUBLE},  
            #{status,jdbcType=CHAR},  
            #{descr,jdbcType=VARCHAR},  
            #{creatime,jdbcType=TIMESTAMP},  
            #{startime,jdbcType=TIMESTAMP},  
            #{cost_type,jdbcType=CHAR}  
        )  
    </insert>

批量修改数据

//针对修改相同的数据
<update id="update" parameterType="java.util.Map">
    UPDATE tableNamw SET name = #{name} 
    WHERE id IN
    <foreach collection="list" item="entity" index="index" open="(" separator="," close=")">
        #{entity.xxx}
    </foreach>
</update>

//针对修改不同的数据
<update id="batchUpdate" parameterType="java.util.List">  
        <foreach collection="list" index="index" item="item" 
        separator=";">  
            update tableName set 
                name=#{item.name,jdbcType=VARCHAR},
                age=#{item.age,jdbcType=INTEGER}  
            where 
                id=#{item.id,jdbcType=INTEGER}  
        </foreach>  
</update>

猜你喜欢

转载自blog.csdn.net/qq_20338923/article/details/81567528
今日推荐