OracleConnection 的 CreateOracleArray 始终在内部创建一个数组 null

问题描述 投票:0回答:0

我在使用 MyBatis 的 TypeHandler 中遇到问题。在调用我的过程之前调用的 setParamter 方法中,我创建了必须在 PreparedStatement 中设置的 java.sql.Array。在调试中我看到数组是用他的 datumArray 创建的。这个 datumArray 有正确的 2 个内部 datumArray,它们在我应该找到我的字符串的位置是空的。

这是我在调试中看到的 java.sql.Array arr:

这是我的类型处理器:

public class ElencoCampiTypeHandler implements TypeHandler<ElencoCampiModificati> {

    @Override
    public void setParameter(PreparedStatement ps, int i, ElencoCampiModificati parameter, JdbcType jdbcType)
            throws SQLException {
        // TODO Auto-generated method stub
        Array arr = ps.getConnection().unwrap(OracleConnection.class).createOracleArray("DYNAPP.V_ELENCO_CAMPI_MODIFICATI", Arrays.stream(parameter.getCampiModificati()).toArray());
        ps.setArray(i, arr);
    }

这是给 setParameter 方法的参数:

        CampoModificato mod = new CampoModificato("CAMPO_MODIFICATO","VALORE_OLD","VALORE_NEW");
        CampoModificato mod1 = new CampoModificato("CAMPO_MODIFICATO1","VALORE_OLD1","VALORE_NEW1");
        CampoModificato[] arr = new CampoModificato[] {mod, mod1};
ElencoCampiModificati elenco = new ElencoCampiModificati(arr);

这是 ElencoCampiModificati 的结构:

package com.ubiss.domain.logger;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;

public class ElencoCampiModificati implements Array{
    private CampoModificato[] campiModificati;
    
    public ElencoCampiModificati() {}

    public ElencoCampiModificati(CampoModificato[] campiModificati) {
        this.campiModificati = campiModificati;
    }

    public CampoModificato[] getCampiModificati() {
        return campiModificati;
    }

    public void setCampiModificati(CampoModificato[] campiModificati) {
        this.campiModificati = campiModificati;
    }

    @Override
    public String getBaseTypeName() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getBaseType() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getArray() throws SQLException {
        // TODO Auto-generated method stub
         return campiModificati == null ? null : Arrays.copyOf(campiModificati, campiModificati.length);
    }

    @Override
    public Object getArray(Map<String, Class<?>> map) throws SQLException {
        // TODO Auto-generated method stub
        return this.getArray();
    }

    @Override
    public Object getArray(long index, int count) throws SQLException {
        // TODO Auto-generated method stub
        return campiModificati == null ? null : Arrays.copyOfRange(campiModificati, (int)index, (int)index + count );
    }

    @Override
    public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException {
        // TODO Auto-generated method stub
        return this.getArray(index, count);
    }

    @Override
    public ResultSet getResultSet() throws SQLException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    @Override
    public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    @Override
    public ResultSet getResultSet(long index, int count) throws SQLException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    @Override
    public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException();
    }

    @Override
    public void free() throws SQLException {
        // TODO Auto-generated method stub
        
    }
    
    
}

这是 CampoModificato:

package com.ubiss.domain.logger;

import java.io.Serializable;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class CampoModificato implements SQLData, Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String nome_campo;
    private String valore_old;
    private String valore_new;
    
    public CampoModificato() {}
    
    public CampoModificato(String nome_campo, String valore_old, String valore_new) {
        this.nome_campo = nome_campo;
        this.valore_old = valore_old;
        this.valore_new = valore_new;
    }
    public String getNomeCampo() {
        return nome_campo;
    }
    public void setNomeCampo(String nome_campo) {
        this.nome_campo = nome_campo;
    }
    public String getValoreOld() {
        return valore_old;
    }
    public void setValoreOld(String valore_old) {
        this.valore_old = valore_old;
    }
    public String getValoreNew() {
        return valore_new;
    }
    public void setValoreNew(String valore_new) {
        this.valore_new = valore_new;
    }

    @Override
    public String getSQLTypeName() throws SQLException {
        // TODO Auto-generated method stub
        return "TY_CAMPI_MODIFICATI";
    }

    @Override
    public void readSQL(SQLInput stream, String typeName) throws SQLException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void writeSQL(SQLOutput stream) throws SQLException {
        // TODO Auto-generated method stub
        
    }
    
    
}

我试图将其他对象用作 ArrayDescriptor 或 ARRAY,但已弃用。我尝试使用 ps.getConnection().unwrap(OracleConnection.class).createARRAY 或 ps.getConnection().unwrap(OracleConnection.class).createArrayOf 方法,但结果是一样的。

java oracle jdbc mybatis ibatis
© www.soinside.com 2019 - 2024. All rights reserved.