在mybatis中插入值列表

时间:2017-05-30 08:29:31

标签: java oracle weblogic mybatis

我使用Oracle DB和Apache mybatis在Weblogic 12c下运行Java应用程序。

对于单个插入,我使用此映射器:

(x,y)

从java我正在做以下事情:

 <resultMap id="DataQualityCRUDPlainResultMap" type="de.vkd.niswrapper.model.persistence.DataQualityCRUD">                                                          
    <id     column="ID"                  property="id"              jdbcType="DECIMAL"   />
    <result column="REGION"              property="region"          jdbcType="DECIMAL"   />
    <result column="ONB_NUM"             property="onbNum"          jdbcType="DECIMAL"   />
    <result column="ASB_NUM"             property="asbNum"          jdbcType="DECIMAL"   />
    <result column="FIBERNODES_TOTAL"    property="fiberNodesTotal" jdbcType="DECIMAL"   />
    <result column="FIBERNODES_COUNT"    property="fiberNodesCount" jdbcType="DECIMAL"   />     
    <result column="FIBERROUTE_TOTAL_COUNT"       property="fiberrouteTotalCount"     jdbcType="DECIMAL"   />
    <result column="FIBERROUTE_LONGITUDE"         property="fiberrouteLongitude"      jdbcType="DECIMAL"   />
    <result column="FIBERROUTE_TOTAL_COUNT_USED"  property="fiberrouteTotalCountUsed" jdbcType="DECIMAL"   />
    <result column="FIBERROUTE_LONGITUDE_USED"    property="fiberrouteLongitudeUsed"  jdbcType="DECIMAL"   />       
    <result column="DT_CREATION"         property="dtCreation"      jdbcType="TIMESTAMP" />
</resultMap>

<sql id="cols">
    ${alias}ID,     
    ${alias}REGION,
    ${alias}ONB_NUM, 
    ${alias}ASB_NUM, 
    ${alias}FIBERNODES_COUNT, 
    ${alias}FIBERNODES_TOTAL,       
    ${alias}FIBERROUTE_TOTAL_COUNT, 
    ${alias}FIBERROUTE_LONGITUDE, 
    ${alias}FIBERROUTE_TOTAL_COUNT_USED, 
    ${alias}FIBERROUTE_LONGITUDE_USED,                  
    ${alias}DT_CREATION 
</sql>

<insert  id="insert"  parameterType="de.vkd.niswrapper.model.persistence.DataQualityCRUD">
INSERT INTO NW_NIS_DATA_QUALITY (
<include refid="cols">
    <property name="alias" value="" />
</include>
) 
VALUES ( 
    #{id},
    #{region},
    #{onbNum},
    #{asbNum},
    #{fiberNodesTotal},
    #{fiberNodesCount},         
    #{fiberrouteTotalCount},
    #{fiberrouteLongitude},
    #{fiberrouteTotalCountUsed},
    #{fiberrouteLongitudeUsed},                             
    #{dtCreation, jdbcType=TIMESTAMP}
)
</insert>

这是有效的,但不是最佳的。

我想消除循环中的for (final DataQuality aDQ : aDQList) { DataQualityCRUD myDDQCrud = new DataQualityCRUD(); fillFields(aRegion, now, aDQ, myDDQCrud); dataQualityCRUDMapper.insert(myDDQCrud); } 并执行值列表的insert

为实现这一目标,我做了以下工作:

insert

我已将Map<String, Object> map = new HashMap<String, Object>(); for (final DataQuality aDQ : aDQList) { DataQualityCRUD myDDQCrud = new DataQualityCRUD(); fillFields(aRegion, now, aDQ, myDDQCrud); resultList.add(myDDQCrud); } map.put("list", resultList); dataQualityCRUDMapper.insertAll(map); Mapper规范添加到insertAll规范中,如下所示:

    <insert  id="insertAll"  parameterType="java.util.Map">
    INSERT INTO NW_NIS_DATA_QUALITY (
        <include refid="cols">
        <property name="alias" value="" />
    </include>
    ) VALUES
    <foreach collection="list" item ="element" open = "(" separator="),(" close=")">            
        #{element.id},
        #{element.region},
        #{element.onbNum},
        #{element.asbNum},
        #{element.fiberNodesTotal},
        #{element.fiberNodesCount},         
        #{element.fiberrouteTotalCount},
        #{element.fiberrouteLongitude},
        #{element.fiberrouteTotalCountUsed},
        #{element.fiberrouteLongitudeUsed},                             
        #{element.dtCreation, jdbcType=TIMESTAMP}       
    </foreach>
</insert>

不幸的是,这会产生如下异常(我不理解):

### Error updating database. Cause: java.sql.SQLSyntaxErrorException: 
ORA-00933: SQL command not properly ended ### The error may involve 
de.vkd.niswrapper.adapter.gis.mapping.DataQualityCRUDMapper.insertAll-
Inline ### The error occurred while setting parameters ### SQL: INSERT 
INTO NW_NIS_DATA_QUALITY ( ID, REGION, ONB_NUM, ASB_NUM, FIBERNODES_COUNT, 
FIBERNODES_TOTAL, FIBERROUTE_TOTAL_COUNT, 
FIBERROUTE_LONGITUDE, FIBERROUTE_TOTAL_COUNT_USED,
FIBERROUTE_LONGITUDE_USED, DT_CREATION ) 
VALUES 
( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ),
( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ),
( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ),
( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )

### Cause: java.sql.SQLSyntaxErrorException: 
ORA-00933: SQL command not properly ended ; bad SQL grammar []; 
nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: 
SQL command not properly ended 

任何提示?

1 个答案:

答案 0 :(得分:1)

Oracle DB不支持多行插入。 (至少在 9i 之前,而不是你尝试过的语法。)

有关替代语法,请参阅this question