MyBatis 中调用存储过程,带有多个 IN OUT 参数和注解

Call stored procedure in MyBatis with multiples IN OUT parameters and annotations

我在 Oracle 中调用一个存储过程,它接收 7 个参数和 return 5 个,我只对两个参数感兴趣。 这是我的 MyBatis Select

@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...

我有一个 dao 调用 MyBatis Sentence,但是当我调用过程时,我的对象 (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 的对象。

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

你的映射器方法必须return无效,忘记@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 传递给映射器方法的参数中。携带IN参数,是OUT参数的目标。

我可以通过删除 "datosDocente and using "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 的建议,并将 void 方法放入 mapper。