如何使用ibatis传递ARRAY来存储过程

时间:2018-08-16 15:58:49

标签: java stored-procedures plsql oracle10g spring-mybatis

im使用mybatis-spring-boot lib和im将自定义数组传递给映射器时遇到问题, 当我的日志显示数组正在设置但存储过程未从数组中获取值时,请让我向我展示配置以更好地向我解释:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
<dependency>
    <groupId>oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>LATEST</version>
</dependency>

我的存储过程如下:

create or replace PROCEDURE SP_VALIDATE_INFO_PERSON(
    cod_bscs_list IN APIBSCS.STRING_COLLECTION,
    cust_code OUT VARCHAR2,
    description OUT VARCHAR2,
    co_id OUT VARCHAR2,
    status OUT NUMBER,
    message OUT VARCHAR)

IS

BEGIN 

SELECT * INTO ...
FROM(
    SELECT ...
    FROM ...
    WHERE ...
    AND ca.custcode in (SELECT * FROM TABLE(cod_bscs_list)) --swcodbscs );

    status :=0;
    message := 'OK';

EXCEPTION WHEN NO_DATA_FOUND THEN
    FOR i IN 1 .. cod_bscs_list.count LOOP
        message := message||' ' || i ||'=' || cod_bscs_list(i);
    END LOOP;
status  := -1;
message := message || ' APIBSCS.SP_VALIDATOR_INFO_PERSON. ERROR: ' ||
                   'No se encontro informacion. ' || SQLERRM ;

WHEN OTHERS THEN
    status := -2;
    message := 'APIBSCS.SP_VALIDATOR_INFO_PERSON. ERROR: ' ||
                   ' ' || SQLERRM||' '||dbms_utility.format_error_backtrace;

END SP_VALIDATE_INFO_PERSON;

这是我的类型

create or replace TYPE STRING_COLLECTION AS TABLE OF VARCHAR2(50);

我的映射器:

<mapper namespace="com.appmovil.mapper.ValidatorInfoMapper">
    <select id="validateInfo" parameterType="java.util.Map" statementType="CALLABLE">
        { call APIBSCS.SP_VALIDATE_INFO_PERSON(
        #{cod_bscs_list, mode=IN, jdbcType=ARRAY, jdbcTypeName=APIBSCS.STRING_COLLECTION,
           typeHandler=com.appmovil.percistence.handler.StringCollectionTypeHandler },
        #{cust_code,javaType=String,jdbcType=VARCHAR,mode=OUT},
        #{description,javaType=String,jdbcType=VARCHAR,mode=OUT},
        #{co_id,javaType=String,jdbcType=VARCHAR,mode=OUT},
        #{status,javaType=java.lang.Integer,jdbcType=NUMERIC,mode=OUT},
        #{message,javaType=String,jdbcType=VARCHAR,mode=OUT}
        )}
    </select>
</mapper>

我的映射器界面:

@Mapper
public interface ValidatorInfoMapper {
    void validateInfo(Map map);
}

我的处理程序看起来像

@MappedJdbcTypes(JdbcType.ARRAY)
public class StringCollectionTypeHandler implements TypeHandler {

private static final String STRING_COLLECTION_TYPE = "APIBSCS.STRING_COLLECTION";
private static final Logger LOG = Logger.getLogger(StringCollectionTypeHandler.class);

@Override
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
    C3P0NativeJdbcExtractor cp30NativeJdbcExtractor = new C3P0NativeJdbcExtractor();
    OracleConnection oracleConn = (OracleConnection)
        cp30NativeJdbcExtractor.getNativeConnection(ps.getConnection());
    if (parameter == null) {
        ps.setNull(i, Types.ARRAY, STRING_COLLECTION_TYPE);
        return;
    }

    String[] strList = (String[]) parameter;
    LOG.info("TYPE HANDLER PARAMETER length " + strList.length + " content first " + strList[0] + " param i " + i);
    ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor(STRING_COLLECTION_TYPE,
        oracleConn);
    ARRAY oracleArray = new ARRAY(arrayDescriptor, oracleConn, strList);
    oracleArray.setObjArray(strList);
    ps.setObject(i, oracleArray);
}

当我执行此操作时,结果是:

: ==>  Preparing: { call APIBSCS.SP_VALIDATE_INFO_PERSON( ?, ?, ?, ?, ?, ? )} 
: TYPE HANDLER PARAMETER length  5 content first 4.5030.00.00.100002 param i 1
: ==> Parameters: [4.5030.00.00.100002, 4.5030, 4.5030.00.00.100000, 4.5030.00.00.100001, 4.5030.00.00.100001](ARRAY)
: END CALL to pe.com.entel.mobileservices.appmovil.repository.impl.SubscriberRepositoryImpl.validatePersonInfo() - [PARAMETERS:] {"cod_bscs_list":["4.5030.00.00.100002","4.5030","4.5030.00.00.100000","4.5030.00.00.100001","4.5030.00.00.100001"],"message":" 1\u003d 2\u003d 3\u003d 4\u003d 5\u003d APIBSCS.SP_VALIDATOR_INFO_PERSON. ERROR: No se encontro informacion. ORA-01403: no data found","status":-1}
: estado de la ejecucion: -1:  1= 2= 3= 4= 5= APIBSCS.SP_VALIDATOR_INFO_PERSON. ERROR: No se encontro informacion. ORA-01403: no data found
: Exception in pe.com.entel.mobileservices.appmovil.dao.impl.SubscriberDaoImpl.validateInfoPerson() with cause = No existe información asociada al documento ingresado. and exception = {}
如您所见,

“:estado de la ejecucion:-1:1 = 2 = 3 = 4 = 5 =”数组以正确的大小但没有值发送,当我把查询和用原始值执行它可以正常工作,我不知道我是否缺少某些东西,我已经用谷歌搜索过,但是没有成功,希望您能为我提供帮助

0 个答案:

没有答案