【问题标题】:How to call oracle stored function which returns table type using mybatis in spring?如何在spring中使用mybatis调用返回表类型的oracle存储函数?
【发布时间】:2016-05-17 03:10:00
【问题描述】:

我在 Spring Tool Suite 中使用 Mybatis 调用 oracle 存储函数返回表类型值时遇到了困难。请查看我下面的代码并回答我。谢谢。

首先,这是我在 Oracle sql developer 中的代码。

create or replace TYPE recommend_type as object
    (
        pno number,
        productthumimage varchar2(500),
        confidence number
    );
    /
create or replace TYPE recommend_table
        as table of recommend_type;
        /


create or replace function recommend_func 
    (p_startdata IN varchar2)
  return recommend_table
  is
    r_type recommend_table := recommend_table();
    v_conf tbl_confidence%rowtype;
    cnt number;

    v_pno tbl_product.pno%type;
    v_productthumimage tbl_product.productthumimage%type;
    v_confidence tbl_confidence.confidence%type;


    CURSOR recommendcursor is
    select * 
    from tbl_confidence
    where STARTDATA = p_startdata;        
  BEGIN 
    open recommendcursor;    
    cnt := 1;

    loop
      fetch recommendcursor into v_conf.startdata, v_conf.enddata, v_conf.confidence;
      exit when recommendcursor%NOTFOUND;

      select pno, productthumimage into v_pno, v_productthumimage
      from tbl_product
      where PNO = v_conf.enddata;  

      v_confidence := v_conf.confidence;

      r_type.extend;

      r_type(cnt) := recommend_type(v_pno, v_productthumimage, v_confidence);
      cnt := cnt+1;
    end loop; 

    return r_type;

end;
/

我可以在 Oracle sql developer 中成功获取结果行,如下所示。

select *
from table(recommend_func(38));

Spring 中的 Mapper.xml

<select id="getRecommedList" parameterType="org.ktl.domain.ConfidenceVO"
    statementType="CALLABLE" >
        {CALL RECOMMEND_FUNC
            (
                #{startdata, mode=IN, jdbcType=VARCHAR}
            )

        }
    </select>

Java bean - for in 参数和表类型返回值 (getter & setter & toString 方法被跳过。)

public class ConfidenceVO {

    private String startdata;
    private String enddata;
    private Double confidence;
    ( ... getter & setter )
}

public class RecommendVO {

    Integer pno;
    String productthumimage;
    Double confidence;

    ( ... getter & setter )
}

DAO 代码

public List<RecommendVO> getRecommedList(ConfidenceVO confidenceVO) throws Exception {
        // TODO Auto-generated method stub

        return session.selectList(namespace+".getRecommedList", confidenceVO);
    }

junit 测试代码

@Test
public void getRecommedListTest() throws Exception {

    ConfidenceVO cVO = new ConfidenceVO();
    cVO.setStartdata("38");

    System.out.println(dao.getRecommedList(cVO));

}//

错误文字

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@51081592] to prepare test instance [org.ktl.test.AprioriDAOTest@623e088f]
java.lang.IllegalStateException: Failed to load ApplicationContext


This is the end. plz help me.

【问题讨论】:

    标签: spring oracle mybatis stored-functions


    【解决方案1】:

    我不知道java,但我猜问题是您正在尝试提交函数调用。您应该只是执行一条 SQL 语句。您在 SQLDeveloper 中使用的同一 SELECT 语句。 表函数作为查询调用,而不是作为函数调用。

    【讨论】:

    • 感谢您的回复。我通过添加额外的表(tbl_recommend)和修改函数来解决这个问题。我可以使用 mybatis 调用 oracle 存储过程,如下所示。 CALL Recommendation_proc(#{startdata}) 无论如何,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多