【问题标题】:Call stored procedure in MyBatis with multiples IN OUT parameters and annotations在 MyBatis 中调用存储过程,带有多个 IN OUT 参数和注解
【发布时间】:2017-04-29 21:58:17
【问题描述】:

我在 Oracle 中调用一个存储过程,它接收 7 个参数并返回 5 个,我只对两个参数感兴趣。 这是我的 MyBatis 选择

@Select(value = "{CALL prc_ultimo_nombramiento(" +
        "#{tipoIdentificacion,    mode=IN}," +
        "#{numeroIdentificacion, mode=IN}," +
        "#{idEt, jdbcType=VARCHAR}," +
        "#{fechaPosesion, mode=OUT, jdbcType=VARCHAR}," +
        "#{idTipoNombramiento, mode=OUT, jdbcType=VARCHAR}," +
        "#{validar, jdbcType=VARCHAR}," +
        "#{mensaje, jdbcType=VARCHAR}" +
        ")}")
@Options(statementType = StatementType.CALLABLE)
@ResultType(CPDatosDocente.class)
CPDatosDocente obtenerDatosFechaPosesionIdNombramiento(CPDatosDocente datosDocente);

我的 CPDatosDocente 是一个 POJO,它包含我需要的所有变量。

String idTipoNombramiento;
String validar;
String mensaje;
String fechaPosesion;
String tipoIdentificacion;
String numeroIdentificacion;
String idEt;
//Getters and setters...

我有一个调用 MyBatis Sentence 的 dao,但是当我调用过程时,我的 object(CPDatosDocente) 为 null

  public CPDatosDocente obtenerFechaPosesionIdNombramiento(Long tipoIdentificacion,
        Long numeroIdentificacionDocente) {
    SqlSession session = sf.openSession();
    try {
        // Se abre conexión con el mapper
        CPDatosDocenteMapper mapper = session.getMapper(CPDatosDocenteMapper.class);
        // Se ejecuta la consulta para obtener la fecha de posesión y el
        // tipo de nombramiento   
        CPDatosDocente datos = new CPDatosDocente();
        datos.setTipoIdentificacion(tipoIdentificacion);
        datos.setNumeroIdentificacion(numeroIdentificacionDocente);

        CPDatosDocente datosDocente  =     mapper.obtenerDatosFechaPosesionIdNombramiento(datos);
        System.out.println(datosDocente.getFechaPosesion());


        return datosDocente;

    } finally {
        session.close();
    }

}

我尝试了很多东西,但我无法获得我需要的带有参数 OUT 的对象。

【问题讨论】:

  • 您是否收到任何错误消息?除此之外,您说有 5 个 OUT 参数,但您只提供了 2 个。尝试提供所有 5 个参数,即使您只使用其中的 2 个。 Oracle 必须接收与过程签名匹配的过程调用。

标签: java oracle stored-procedures mybatis


【解决方案1】:

不要将带有 Out 参数的过程调用视为选择。

你的mapper方法必须返回void,忘记@ResultType。

@Select(value = "{CALL prc_ultimo_nombramiento(" +
        "#{tipoIdentificacion,    mode=IN}," +
        "#{numeroIdentificacion, mode=IN}," +
        "#{idEt, jdbcType=VARCHAR}," +
        "#{fechaPosesion, mode=OUT, jdbcType=VARCHAR}," +
        "#{idTipoNombramiento, mode=OUT, jdbcType=VARCHAR}," +
        "#{validar, jdbcType=VARCHAR}," +
        "#{mensaje, jdbcType=VARCHAR}" +
        ")}")
@Options(statementType = StatementType.CALLABLE)
void obtenerDatosFechaPosesionIdNombramiento(CPDatosDocente datosDocente);

OUT 参数写在datosDocente 参数中传递给mapper 方法。它携带IN参数,是OUT参数的目标。

【讨论】:

  • 谢谢blackwizard,你是对的,我已经尝试了很多,我发现我的错误,我正在使用一个我不需要的对象CPDatosDocente,当我执行我的程序时,所有参数存储在“datos”而不是“datosDocente”中,因此我搜索的所有内容都是空的。再次感谢。
  • @Allanh :对于 SO 读者,您可以将问题标记为已回答。
  • 好的。我将把问题标记为已回答,但我需要发布我为此所做的更改。
【解决方案2】:

我可以通过删除“datosDocente”并使用“datos”来解决这个问题,所以我做了一些更改。

Mapper.java:

@Select(value = "{CALL prc_ultimo_nombramiento(#{tipoIdentificacion,    mode=IN},#{numeroIdentificacion, mode=IN},#{idEt, jdbcType=VARCHAR},#{fechaPosesion, mode=OUT, jdbcType=VARCHAR},#{idTipoNombramiento, mode=OUT, jdbcType=VARCHAR},#{validar, jdbcType=VARCHAR},#{mensaje, jdbcType=VARCHAR})}")
@Options(statementType = StatementType.CALLABLE)
@ResultType(CPDatosDocente.class)
void obtenerDatosFechaPosesionIdNombramiento(CPDatosDocente datosDocente);
 //I put this field void because I don't need this method return nothing.

DAO.java

其他变化。

 public CPDatosDocente obtenerFechaPosesionIdNombramiento(String tipoIdentificacion,
        String numeroIdentificacionDocente) {
    SqlSession session = sf.openSession();
    try {
        // Se abre conexión con el mapper
        CPDatosDocenteMapper mapper = session.getMapper(CPDatosDocenteMapper.class);
        // Se ejecuta la consulta para obtener la fecha de posesión y el
        // tipo de nombramiento
        CPDatosDocente datosDocente = new CPDatosDocente();
        //Se setean los parámetros necesarios para ejecutar el procedimiento
        datosDocente.setTipoIdentificacion(tipoIdentificacion);
        datosDocente.setNumeroIdentificacion(numeroIdentificacionDocente);          
        mapper.obtenerDatosFechaPosesionIdNombramiento(datosDocente);
        return datosDocente;
    } finally {
        session.close();
    }

}

我正在重用我的对象 datosDocente,当我调用方法“obtenerFechaPosesionIdNombramiento”时,他会自动将参数保存在对象中。 现在一切正常。我听从了 blackwizard 的建议,并在 mapper 中放了 void 方法。

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2011-07-29
    • 2020-09-22
    • 1970-01-01
    • 2018-09-19
    • 2023-03-26
    相关资源
    最近更新 更多