oracle+mybatis如何批量插入?


dao:

int insertBatch(List<P> pos);

xml:

<insert id="insertBatch" parameterType="list" useGeneratedKeys="false" databaseId="oracle">
  insert into table_name (
    ID,
    FIELD_ONE,
    FIELD_TWO,
    FIELD_THREE
  )
  <foreach collection="list" item="item" index="index" separator="UNION ALL">
    select get_seq_next('seq_name'),
    #{item.fieldOne, jdbcType = ???},
    #{item.fieldTwo, jdbcType = ???},
    #{item.fieldThree, jdbcType = ???} from dual
  </foreach>
</insert>

注:table_name即表名;seq_name為自增序列名;get_seq_next()是個oracle函數,用來獲取序列的下一個值,雖然按理說直接seq_name.nextval就可以了,但是這里會報錯。

附get_seq_next()函數:

CREATE OR REPLACE
function get_seq_next (seq_name in varchar2) return number
is
seq_val number ;
begin
execute immediate 'select '|| seq_name|| '.nextval from dual' into seq_val ;
return seq_val ;
end;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM