Java通过Mybatis实现批量插入数据到Oracle中

Posted 程序猿001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java通过Mybatis实现批量插入数据到Oracle中相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oracle.mapper.AccountInfoMapper"><!--  接口的全类名 -->
    <!-- type:实体类的全类名 -->
    <resultMap id="BaseResultMap" type="com.oracle.entity.AccountInfo">
        <id column="ID" property="id" jdbcType="DECIMAL" />
        <result column="USERNAME" property="userName" jdbcType="VARCHAR" />
        <result column="PASSWORD" property="password" jdbcType="VARCHAR" />
        <result column="GENDER" property="gender" jdbcType="CHAR" />
        <result column="EMAIL" property="email" jdbcType="VARCHAR" />
        <result column="CREATE_DATE" property="createDate" jdbcType="DATE" />
    </resultMap>
    <!-- id 跟接口中的方法名称保持一致 -->
    <select id="queryAllAccountInfo" resultMap="BaseResultMap">
        select ID,
        USERNAME,PASSWORD,
        GENDER, EMAIL, CREATE_DATE from ACCOUNT_INFO
    </select>
    <insert id="batchInsertAccountInfo" parameterType="java.util.List">
        INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
        (
        <foreach collection="list" index="" item="accountInfo"
            separator="union all">
            select
            #{accountInfo.id},
            #{accountInfo.userName},
            #{accountInfo.password},
            #{accountInfo.gender},
            #{accountInfo.email},
            #{accountInfo.createDate}
            from dual
        </foreach>
        )
    </insert>
    
    <insert id="batchInsertAccountInfoUseSeq" parameterType="java.util.List">
        <selectKey resultType="long" keyProperty="id" order="BEFORE"> 
            SELECT ACCOUNT_SEQ.NEXTVAL FROM dual
        </selectKey> 
        INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
        SELECT ACCOUNT_SEQ.NEXTVAL, m.* FROM(
        <foreach collection="list" index="" item="accountInfo"
            separator="union all">
            select
            #{accountInfo.userName},
            #{accountInfo.password},
            #{accountInfo.gender},
            #{accountInfo.email},
            sysdate
            from dual
        </foreach>
        ) m
    </insert>
    
    <insert id="insertOne" parameterType="com.oracle.entity.AccountInfo">
        <selectKey resultType="long" keyProperty="id" order="BEFORE"> 
            SELECT ACCOUNT_SEQ.NEXTVAL FROM dual
        </selectKey> 
        INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
        values(
            #{id},
            #{userName},
            #{password},
            #{gender},
            #{email},
            sysdate
        )
    </insert>
</mapper>

 

以上是关于Java通过Mybatis实现批量插入数据到Oracle中的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis 批量插入带oracle序列例子+ORA-02287: 此处不允许序号

MyBatis 批量插入包含 BLOB 类型的数据到 Oracle 异常问题分析

MyBatis 批量插入包含 BLOB 类型的数据到 Oracle 异常问题分析

mybatis 批量插入时报错:ORACLE ORA-00933: SQL 命令未正确结束

通过Mybatis执行拼接的SQL批量插入数据并返回数据库自增ID

通过Mybatis执行拼接的SQL批量插入数据并返回数据库自增ID