从Java调用oracle存储过程时,在mybatis映射器中映射多个out参数

时间:2019-03-27 18:54:28

标签: java oracle mybatis ibatis spring-mybatis

问题是这样的,我想使用mybatis / ibatis从Java调用存储过程,该过程有多个OUT参数,确切地说是7个,其中2个是Numbers,另外5个Varchars,再加上一个IN Number参数< / p>

上下文中有一个很大的限制,即没有模型,只有映射和列表,尽管在必要时我们可以在调用过程时将其设为例外。另外,该解决方案必须使用Java来完成,不使用xml。

我有一个springboot应用程序,其中有一个rest服务,该服务调用一个service bean,该bean调用Mapper接口,该接口定义了对我要执行的过程的调用。

到目前为止,我已经尝试在mapper界面中使用@Result将结果映射到Object,List,Maps,试图逐个字段映射到特定的结果类,但是一切失败了……mybatis似乎执行了程序没有错误,但是很遗憾,没有任何返回。

我正在发布我的最新状态,其中包括试图将结果映射到自定义类,它似乎执行了该过程,但是返回了空值

//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>

期望的结果很简单,可以返回列表,映射或包含该过程返回的OUT参数值的自定义类,现在,我所拥有的只是null或空列表...

1 个答案:

答案 0 :(得分:1)

哦,好吧,我设法弄清楚了,我想我必须回答自己,以防它对别人有帮助...,事实证明,我终于设法通过仅发送1个地图作为参数来获取参数,并且在service方法中之前设置IN值...而且,映射器现在返回void,这意味着mybatis正在向映射中动态添加所有其他out参数。我还对call语句做了一些更改,也许那里也有错误?

代码以这样的方式结束:

    //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}"+
            ")}";