mybatis批量插入自带判断是否存在

<insert id="insertBatchIfUnexist" parameterType="java.util.List">
          insert into [t_direct_airline_limit]
        (
            plat
            ,shop
            ,dep
            ,arr
            ,valid
        )
        <foreach collection="list" index="index" item="each" separator="UNION ALL">
            select
              #{each.plat,jdbcType=INTEGER}, 
              #{each.shop,jdbcType=INTEGER},
              #{each.dep,jdbcType=CHAR},
              #{each.arr,jdbcType=CHAR},
              #{each.valid,jdbcType=CHAR}
            where 
            not exists(
                select 
                t.id 
                from [t_direct_airline_limit] t 
                with(nolock,index=_i_plat_shop,index=_i_dep,index=_i_arr) 
                where (1=1) 
                and t.plat=#{each.plat,jdbcType=INTEGER} 
                and t.shop=#{each.shop,jdbcType=INTEGER}
                and t.dep=#{each.dep,jdbcType=CHAR}
                and t.arr=#{each.arr,jdbcType=CHAR}
            )
        </foreach>
      </insert>
int insertBatchIfUnexist(List<DirectAirlineLimit> list)throws SQLException;
//test
//模拟生成list代码忽略...
PagenationUtil<DirectAirlineLimit> page = new PagenationUtil<DirectAirlineLimit>(list, 3);
        int total = page.getTotalPage();
        for (int i =1;i<=total;i++) {
            page.setPage(i);
            List<DirectAirlineLimit> eachlist = page.getPageList();
            directAirlineLimitMapper.insertBatchIfUnexist(eachlist);
        }

PS:

PagenationUtil是个按设定数量分页集合的工具类
posted @ 2018-09-28 11:06  稚语希听  阅读(1858)  评论(0)    收藏  举报