Mapping multiple out params in mybatis mapper when calling an oracle stored procedure from java

Meiroa :

The problem is this, i want to call a stored procedure from java, using mybatis / ibatis, this procedure has multiple OUT params,7 precisely, 2 of them are Numbers, the other 5 Varchars, plus one IN Number param

There is a huge limitation within the context, which is having no Models, just maps and lists, though we can make an exception when calling procedures if necessary. Also, the solution has to be done in java, no xml is being used.

I've got a springboot app, there is a rest service, which calls a service bean, which invokes the Mapper Interface that defines the call to the procedure i'm trying to execute.

So far i've tried within the mapper interface mapping the result to Object, List, Maps, using @Result trying to map to a specific result class field by field, but everything failed miserabl... mybatis seems to execute the procedure without errors but nothing is returned unfortunately.

I'm posting my latest state which consisted in trying to map the result to a custom class, it seems to execute the procedure, but null is returned

//custom class
public class ProcClass {
    Integer p_tipo_cliente;
    Integer p_codigo_cliente;
    String p_nombre_cliente;
    String p_cuit_cliente;
    String p_cuit_rp;
    String p_razon_social_rp;
    String p_domicilio;

// plus constructor, getters and setters
}

//Service method 
public ProcClass callProcedure(String param){
        return asociadoMapper.callProcedure(
                Integer.getInteger(param),0,0,
                "",",","","","");
    }


//Mapper interface

@Repository
public interface AsociadoMapper extends Mapper {


    @Select(value = AsociadoQueries.getDocumentoAsociadoCall)
    @Options(statementType = StatementType.CALLABLE)
    @Results(value = {
            @org.apache.ibatis.annotations.Result
                    (property = "p_tipo_cliente", column = "p_tipo_cliente"),
            @org.apache.ibatis.annotations.Result
                    (property = "p_codigo_cliente", column = "p_codigo_cliente"),
            @org.apache.ibatis.annotations.Result
                    (property = "p_nombre_cliente", column = "p_nombre_cliente"),
            @org.apache.ibatis.annotations.Result
                    (property = "p_cuit_cliente", column = "p_cuit_cliente"),
            @org.apache.ibatis.annotations.Result
                    (property = "p_cuit_rp", column = "p_cuit_rp"),
            @org.apache.ibatis.annotations.Result
                    (property = "p_razon_social_rp", column = "p_razon_social_rp"),
            @org.apache.ibatis.annotations.Result
                    (property = "p_domicilio", column = "p_domicilio"),
    })
    ProcClass callProcedure(Integer p_id_grupo_familiar,
                            Integer p_tipo_cliente,
                            Integer p_codigo_cliente,
                            String p_nombre_cliente,
                            String p_cuit_cliente,
                            String p_cuit_rp,
                            String p_razon_social_rp,
                            String p_domicilio
                                     );

//Util class

public class AsociadoQueries {


    public static final String getDocumentoAsociadoCall = "{ CALL consultas_generales.get_detalle_cliente_gf(" +
            "#{p_id_grupo_familiar, mode=IN, jdbcType=INTEGER}," +
            "#{p_tipo_cliente, mode=OUT, jdbcType=INTEGER,},"+
            "#{p_codigo_cliente,  jdbcType=INTEGER},"+
            "#{p_nombre_cliente,  jdbcType=VARCHAR},"+
            "#{p_cuit_cliente,  jdbcType=VARCHAR},"+
            "#{p_cuit_rp,  jdbcType=VARCHAR},"+
            "#{p_razon_social_rp,  jdbcType=VARCHAR},"+
            "#{p_domicilio, jdbcType=VARCHAR}"+
            ")}";


// Pom related dependencies

<!-- MySQL -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.mybatis.dynamic-sql/mybatis-dynamic-sql -->
        <dependency>
            <groupId>org.mybatis.dynamic-sql</groupId>
            <artifactId>mybatis-dynamic-sql</artifactId>
            <version>1.1.0</version>
        </dependency>

The result expected is simple, either returning a list, map or a custom class containing the OUT params values returned by the procedure, right now, all i've got was null or an empty list...

Meiroa :

Oh well, i just managed to figure it out, i guess i have to answer myself in case it helps someone else..., turns out i finally managed to get the out params by sending only 1 map as parameter, and setting the IN values before within the service method... also, the mapper is returning void now, which means that mybatis is adding dynamically all the additional out params into the map. I also made some changes to the call statement, maybe there was an error there too?

The code ended like this:

    //service call
    public Map<String, Object> callProcedure(String param){

            Map<String, Object> map = new HashMap<String, Object>();
            Integer privilegesCount = 0;
            map.put("p_id_grupo_familiar", Integer.valueOf(param));
            asociadoMapper.callProcedure(map);

            return map;
        }
    //mapper
        @Select(value = AsociadoQueries.getDocumentoAsociadoCall)
        @Options(statementType = StatementType.CALLABLE)
        void callProcedure(Map<String,Object> params);

// Call statement

public static final String getDocumentoAsociadoCall = "{ CALL consultas_generales.get_detalle_cliente_gf(" +
            "#{p_id_grupo_familiar,  jdbcType=NUMERIC ,javaType=java.lang.Integer ,mode=IN}," +
            "#{p_tipo_cliente, jdbcType=NUMERIC,javaType=java.lang.Integer ,mode=OUT,},"+
            "#{p_codigo_cliente,  jdbcType=NUMERIC ,javaType=java.lang.Integer ,mode=OUT},"+
            "#{p_nombre_cliente,  jdbcType=VARCHAR ,javaType=java.lang.String ,mode=OUT},"+
            "#{p_cuit_cliente,  jdbcType=VARCHAR ,javaType=java.lang.String ,mode=OUT},"+
            "#{p_cuit_rp,  jdbcType=VARCHAR ,javaType=java.lang.String ,mode=OUT},"+
            "#{p_razon_social_rp,  jdbcType=VARCHAR ,javaType=java.lang.String ,mode=OUT},"+
            "#{p_domicilio, jdbcType=VARCHAR ,javaType=java.lang.String ,mode=OUT}"+
            ")}";

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151104&siteId=1