NutzCN Logo
问答 dao怎样调用数据库函数并返回结果列表(结果为多行数据),谢谢!
发布于 2737天前 作者 wangliangyue 1925 次浏览 复制 上一个帖子 下一个帖子
标签:

如标题,望大神指教!

7 回复

请兽大神帮忙!

自定义sql,文档有

麻烦看下!

select pkg_pda.Get_resultAll('1000037491', '0601', 'YAOKURKSJMX')  from dual ;
 FUNCTION Get_resultAll( prm_danjuid In Varchar2,prm_yingyongid In Varchar2, prm_caozuolx   IN  VARCHAR2)
RETURN SYS_REFCURSOR
IS
  return_cursor SYS_REFCURSOR;
  sqlstr   varchar2(1000);
BEGIN
  if prm_caozuolx ='YAOKURKSJMX' then 
     sqlstr :='select pkg_pda.Fun_GY_ChuRuKFSMC(d.yingyongid,d.churukfs) as churukfsmc,d.rukudh as danjuhm , b.yaopinmc as yaopinmc ,b.yaopingg as yaopingg,b.chandimc  as chandimc ,a.rukusl||b.baozhuangdw as  shuliang  , a.shengchanph  as shengchanph ,a.yaopinxq as yaopinxq  , pkg_pda.Fun_GY_GetBaiFangWZ(a.jiageid,d.yingyongid) baifangwz,
      pkg_pda.Fun_GY_GetKuCunSL(a.jiageid,d.yingyongid,1,1) + a.rukusl || b.baozhuangdw as kucunsl from  yk_rukudan2 a  , gy_yaopincdjg2 b , yk_rukudan1 d   
       where  d.rukudid = :prm_danjuid  and  
              a.rukudid =d.rukudid    and 
              a.jiageid  = b.jiageid  
              order by baifangwz asc  '; 
    elsif  prm_caozuolx ='YAOKUCKXJMX' then 
       sqlstr :='select pkg_pda.Fun_GY_ChuRuKFSMC(d.yingyongid,d.churukfs) as churukfsmc,d.chukudh as danjuhm , b.yaopinmc as yaopinmc ,b.yaopingg as yaopingg,b.chandimc  as chandimc ,a.chukusl||b.baozhuangdw as  shuliang  , a.shengchanph  as shengchanph ,a.yaopinxq as yaopinxq  , pkg_pda.Fun_GY_GetBaiFangWZ(a.jiageid,d.yingyongid) baifangwz,
      pkg_pda.Fun_GY_GetKuCunSL(a.jiageid,d.yingyongid,1,1) + a.chukusl || b.baozhuangdw as kucunsl from  yk_chukudan2 a  , gy_yaopincdjg2 b , yk_chukudan1 d   
       where  d.chukudid = :prm_danjuid  and  
              a.chukudid =d.chukudid    and 
              a.jiageid  = b.jiageid  
              order by baifangwz asc '; 
     
    elsif  prm_caozuolx =  'FAYAOXJJJ' then 
     sqlstr :='   select   c.bingqumc,a.fayaosj ,a.fayaoxh  from by_shenqingdan  a  ,gy_bingqu c 
    where fayaoxh =:prm_danjuid and  a.shenqingbq  = c.bingquid  and rownum =1   ';
    end if;
  --sqlstr :='SELECT yaopinmc as a1 ,jiageid  FROM gy_yaopincdjg2 where jiageid =:prm_Input   ';
  OPEN return_cursor FOR sqlstr  using prm_danjuid ;
  RETURN return_cursor;

END Get_resultAll;

可能刚才没表达清楚 pkg_pda.Get_resultAll 查询后返回的只有一行记录, 该记录是一个 SYS_REFCURSOR ,需通过解析返回出来的 SYS_REFCURSOR 来处理成列表数据。该怎么解决?

    @Test
    public void testDBFun() {
        Dao dao2=ioc.get(Dao.class,"dao2");
        Sql sql = Sqls.create("select pkg_pda.Get_resultAll('1000037491', '0601', 'YAOKURKSJMX') as fun_rs from dual");
        sql.setCallback(new QueryRecordCallback(){
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                ResultSetLooping ing = new ResultSetLooping() {
                    protected boolean createObject(int index, ResultSet rs, SqlContext context, int rowCout) {
                        Record subRecord = Record.create(rs);
                        System.out.println("######" + Json.toJson(subRecord));
                        this.list.add(subRecord);
                        return true;
                    }
                };
                ing.doLoop(rs, sql.getContext());
                return ing.getList(); //return super.invoke(conn, rs, sql);
            }
        });
        dao2.execute(sql);
        List<Record> list = sql.getList(Record.class);
        log.debug("list::"+ Json.toJson(list));
    }

返回的结果:

[DEBUG] 2017-06-25 15:12:40,947 org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:189) - select expert : org.nutz.dao.impl.jdbc.oracle.OracleJdbcExpert
[DEBUG] 2017-06-25 15:12:40,954 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:200) - JDBC Driver --> 10.2.0.3.0
[DEBUG] 2017-06-25 15:12:40,954 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:201) - JDBC Name   --> Oracle JDBC driver
[DEBUG] 2017-06-25 15:12:40,954 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:203) - JDBC URL    --> jdbc:oracle:thin:@//192.168.1.237:1521/orcl
[DEBUG] 2017-06-25 15:12:40,955 org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:222) - Database info --> ORACLE:[Oracle - Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options]
[DEBUG] 2017-06-25 15:12:41,004 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - select pkg_pda.Get_resultAll('1000037491', '0601', 'YAOKURKSJMX') as fun_rs from dual
######{
   "fun_rs": {
      "cursorIndex": 0,
      "fetchRowCount": 0,
      "constructNano": 0,
      "closeCount": 0,
      "readStringLength": 0,
      "readBytesLength": 0,
      "openInputStreamCount": 0,
      "openReaderCount": 0,
      "attributes": {
      }
   }
}
[DEBUG] 2017-06-25 15:12:41,295 cn.wizzer.test.MyTest.testDBFun(MyTest.java:69) - list::[{
   "fun_rs": {
      "cursorIndex": 0,
      "fetchRowCount": 0,
      "constructNano": 0,
      "closeCount": 0,
      "readStringLength": 0,
      "readBytesLength": 0,
      "openInputStreamCount": 0,
      "openReaderCount": 0,
      "attributes": {
      }
   }
}]

不明白你想怎么做,要不用dao.run拿到数据库连接,爱咋搞就咋搞

添加回复
请先登陆
回到顶部