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>

 

posted @ 2018-07-16 17:30  程序猿001  阅读(250)  评论(0编辑  收藏  举报