批量merge

Oracle

  1. 参数做判断,需要传入map
    1
    2
    3
    4
    5
    6
    
    <statement id="merge" parameterClass="map">
    <iterate property="list" open="begin" close=";end;" conjunction=";">
        merge into...
            <isNotEmpty property="list[].id">
                id=id,
            </isNotEmpty>
    
  2. 不用判断参数,传入list
    1
    2
    3
    
    <statement id="merge" parameterClass="list">
    <iterate open="begin" close=";end;" conjunction=";">
        merge into... #list[].id#
    

mysql

1
2
3
4
5
6
7
insert into table (column)
values
<foreach collection="list" item="item" index="index" separator=",">
    (#{item.column,jdbcType=VARCHAR})
</foreach>
ON DUPLICATE KEY UPDATE
    column = values(column)

集合字段映射

1
2
3
<resultMap class="" id="queryResult">
    <result property="ids" column="id" select="queryIds"/>
</resultMap>

常见错误

  • Invalid bound statement (not found)
    只有Mapper结尾的xml文件才会被Mybatis扫描到

去除多余的and或者or

1
2
3
4
5
6
7
8
9
<trim prefix="WHERE (" suffix=")" prefixOverrides="AND |OR ">
    <if test="null != name and '' != name ">
        or name = #{name,jdbcType=VARCHAR}
    </if>
    <if test="null != mobile and '' != mobile ">
        or mobile = #{mobile,jdbcType=VARCHAR}
    </if>
</trim>
and status = 1