MyBatils批量插入/更新

本文介绍如何使用MyBatis的批量插入和更新功能,展示了`batchInsertOrUpdate`方法,以及如何利用`trim`标签动态拼接SQL,包括`ON DUPLICATE KEY UPDATE`和不同条件下的`CASE`语句。
 int batchInsertOrUpdate(@Param("list") List<AuntRestInfo> list);
<insert id="batchInsertOrUpdate" parameterType="map">
    <!--@mbg.generated generated on Tue Oct 20 10:53:28 CST 2020.-->
    insert into aunt_rest_info
    (rest_id, aunt_id, rest_day, rest1_start_time, rest1_end_time, rest2_start_time,
      rest2_end_time, creater, modifier, yn)
    values
      <foreach collection="list" item="item" separator=",">
      (#{item.restId,jdbcType=BIGINT}, #{item.auntId,jdbcType=BIGINT}, #{item.restDay,jdbcType=TINYINT},
        #{item.rest1StartTime,jdbcType=TIME}, #{item.rest1EndTime,jdbcType=TIME}, #{item.rest2StartTime,jdbcType=TIME},
        #{item.rest2EndTime,jdbcType=TIME}, #{item.creater,jdbcType=VARCHAR}, #{item.modifier,jdbcType=VARCHAR}, #{item.yn,jdbcType=TINYINT}
        )
      </foreach>
    ON DUPLICATE KEY UPDATE
    rest1_start_time = values(rest1_start_time),
    rest1_end_time = values(rest1_end_time),
    rest2_start_time = values(rest2_start_time),
    rest2_end_time = values(rest2_end_time),
    modifier = values(modifier),
    yn = values(yn)
  </insert>

MyBatils批量更新

<update id="batchUpdate" parameterType="java.util.List">
    update time_stock
    <trim prefix="set" suffixOverrides=",">
        <trim prefix="time_stock_type =case" suffix="end,">
            <foreach collection="list" item="item">
                <if test="item.timeStockType != null">
                    when time_stock_id = #{item.timeStockId} then #{item.timeStockType}
                </if>
                <if test="item.timeStockType == null">
                    when time_stock_id = #{item.timeStockId} then time_stock.time_stock_type
                </if>
            </foreach>
        </trim>
        <trim prefix="time_stock_status =case" suffix="end,">
            <foreach collection="list" item="item">
                <if test="item.timeStockStatus != null">
                    when time_stock_id = #{item.timeStockId} then #{item.timeStockStatus}
                </if>
                <if test="item.timeStockStatus == null">
                    when time_stock_id = #{item.timeStockId} then time_stock.time_stock_status
                </if>
            </foreach>
        </trim>
    </trim>
    <where>
        <foreach collection="list" separator="or" item="item">
            time_stock_id = #{item.timeStockId}
            and time_stock_status = #{item.preTimeStockStatus}
            and time_stock_type = #{item.preTimeStockType}
        </foreach>
    </where>
</update>
 <update id="batchUpdate" parameterType="java.util.List">
     update ls_voucher_bind
    <trim prefix="set" suffixOverrides=",">
     <trim prefix="expired_status =case" suffix="end,">
       <foreach collection="list" item="item" index="index">
         <if test="item.expiredStatus !=null">
           when voucher_num=#{item.voucherNum} then #{item.expiredStatus}
         </if>
       </foreach>
     </trim>
      <trim prefix="verified_status=case" suffix="end">
        <foreach collection="list" item="item" index="index">
          <if test="item.verifiedStatus != null">
          when voucher_num=#{item.voucherNum} then #{item.verfiedStatus}
          </if>
        </foreach>
      </trim>
      <trim prefix="invalid_status =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          <if test="item.invalidStatus != null" >
            when voucher_num=#{item.voucherNum} then #{item.invalidStatus}
          </if>
        </foreach>
      </trim> 
    </trim>
    where voucher_num in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.voucherNum,jdbcType=BIGINT}
    </foreach>
  </update>
    
   知识点:
      trim:具有动态拼接SQL的能力
      trim具有四个属性:
           prefix:表示在trim包裹的SQL前添加指定内容
           suffix:表示在trim包裹的SQL末尾添加内容
      prefixOverrides:表示去掉(覆盖)trim包裹的SQL的指定首部内容
      suffixOverrides:表示去掉(覆盖)trim包裹的SQL的指定尾部内容
   
在 MyBatis 中,当 SQL 查询中使用字段别名并期望返回结果映射为大写时,可能会遇到字段名在结果 Map 中被自动转为小写的问题。这是由于 MyBatis 默认使用 `mapUnderscoreToCamelCase` 配置进行自动映射,或者数据库驱动本身对字段名的处理方式导致的。以下是几种可行的解决方案: 1. **使用 `@MapKey` 注解** 在 Mapper 接口的方法定义中,可以使用 `@MapKey` 注解明确指定返回 Map 的键值字段,确保字段名不会被自动转换。例如: ```java @MapKey("ID") Map<String, Object> selectDemo(); ``` 2. **在 SQL 中使用别名并结合 `resultMap` 显式映射** 通过定义 `resultMap`,可以显式指定数据库列与 Java 对象属性的映射关系,避免自动转换带来的问题。例如: ```xml <resultMap id="demoResultMap" type="map"> <result property="MyField" column="MY_FIELD"/> </resultMap> <select id="demo" resultMap="demoResultMap"> SELECT MY_FIELD AS MY_FIELD FROM demoDB </select> ``` 3. **修改 MyBatis 配置关闭自动映射行为** 在 `mybatis-config.xml` 中,可以关闭自动将下划线转为驼峰命名的设置,从而避免字段名被自动转换: ```xml <settings> <setting name="mapUnderscoreToCamelCase" value="false"/> </settings> ``` 4. **在 SQL 中使用双引号或反引号包裹字段别名** 某些数据库(如 Oracle)要求使用双引号来保留字段名的大小写,而 MySQL 则使用反引号。例如: ```xml <select id="demo" resultType="map"> SELECT my_field AS "MyField" FROM demoDB </select> ``` 或者在 MySQL 中: ```xml <select id="demo" resultType="map"> SELECT my_field AS `MyField` FROM demoDB </select> ``` 5. **使用 Java 方法处理字段名大小写** 在 `<foreach>` 等动态 SQL 中,可以通过引入 Java 方法来处理字段名的大小写转换。例如,使用 `${str.toUpperCase()}` 来动态生成大写别名: ```xml <select id="demo" resultType="map"> SELECT <foreach collection="demoList" item="item" separator=","> ${item} AS ${str.toUpperCase()}${item} </foreach> FROM demoDB </select> ``` 这种方式适用于需要动态生成别名的场景,并确保字段名在结果 Map 中以大写形式存在[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值