从 java 调用 oracle 存储过程时在 mybatis 映射器中映射多个输出参数
Mapping multiple out params in mybatis mapper when calling an oracle stored procedure from java
问题是这样的,我想从java调用一个存储过程,使用mybatis / ibatis,这个过程有多个OUT参数,准确地说是7个,其中2个是数字,另外5个是Varchars,加上一个 IN Number 参数
上下文中有一个很大的限制,它没有模型,只有映射和列表,尽管我们可以在必要时调用过程时进行异常处理。此外,解决方案必须在 java 中完成,没有使用 xml。
我有一个 springboot 应用程序,有一个 rest 服务,它调用一个服务 bean,它调用映射器接口,该接口定义了对我试图执行的过程的调用。
到目前为止,我已经尝试在映射器界面中将结果映射到对象、列表、地图,使用@Result 尝试逐字段映射到特定结果class,但一切都失败了。 . mybatis 似乎执行过程没有错误,但不幸的是没有返回任何内容。
我正在发布我的最新状态,其中包括尝试将结果映射到自定义 class,它似乎执行了该过程,但返回了 null
//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 参数值的自定义 class,现在,我得到的只是 null 或空列表。 ..
哦,好吧,我刚刚弄明白了,我想我必须自己回答以防它帮助别人...,结果我终于设法通过仅发送 1 个地图作为参数来获取输出参数,并在服务方法之前设置 IN 值...此外,映射器现在返回 void,这意味着 mybatis 正在将所有额外的输出参数动态添加到映射中。我也对调用语句做了一些修改,也许那里也有错误?
代码是这样结束的:
//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}"+
")}";
问题是这样的,我想从java调用一个存储过程,使用mybatis / ibatis,这个过程有多个OUT参数,准确地说是7个,其中2个是数字,另外5个是Varchars,加上一个 IN Number 参数
上下文中有一个很大的限制,它没有模型,只有映射和列表,尽管我们可以在必要时调用过程时进行异常处理。此外,解决方案必须在 java 中完成,没有使用 xml。
我有一个 springboot 应用程序,有一个 rest 服务,它调用一个服务 bean,它调用映射器接口,该接口定义了对我试图执行的过程的调用。
到目前为止,我已经尝试在映射器界面中将结果映射到对象、列表、地图,使用@Result 尝试逐字段映射到特定结果class,但一切都失败了。 . mybatis 似乎执行过程没有错误,但不幸的是没有返回任何内容。
我正在发布我的最新状态,其中包括尝试将结果映射到自定义 class,它似乎执行了该过程,但返回了 null
//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 参数值的自定义 class,现在,我得到的只是 null 或空列表。 ..
哦,好吧,我刚刚弄明白了,我想我必须自己回答以防它帮助别人...,结果我终于设法通过仅发送 1 个地图作为参数来获取输出参数,并在服务方法之前设置 IN 值...此外,映射器现在返回 void,这意味着 mybatis 正在将所有额外的输出参数动态添加到映射中。我也对调用语句做了一些修改,也许那里也有错误?
代码是这样结束的:
//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}"+
")}";