2016-05-17 13 views
0

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

起初,这是我在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)); 

Mapper.xml春

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

     } 
    </select> 

的Java bean - (吸气&二传手& 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.springframewor[email protected]51081592] to prepare test instance [[email protected]] 
java.lang.IllegalStateException: Failed to load ApplicationContext 


This is the end. plz help me. 

回答

0

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

+0

感谢您的回复。我通过添加附加表(tbl_recommend)并修改过程的功能来解决问题。我可以用下面的mybatis调用oracle存储过程。 <刀片ID = “insertRecommedList” statementType = “CALLABLE”> \t \t CALL \t recommend_proc(#{STARTDATA}) 无论如何真的感谢。 – wjheo

相关问题