使用ibatis的时候调用存储过程,通过游标可以返回所有字段数据,这样避免一个个的into到相应字段。
Oracle实现:
CREATE OR REPLACE PACKAGE ABCDE_PKG_PTS_TEST as
TYPE T_CURSOR IS REF CURSOR;
PROCEDURE query_send_msg_info(
maxDate in varchar2,
out_cursor out t_cursor);
procedure query_process_task_reportno(
regist_no in varchar2,
report_id in varchar2,
car_mark in varchar2,
out_cursor out t_cursor);
procedure query_need_confirm_repair(
regist_no in varchar2,
out_cursor out t_cursor);
procedure query_need_contact_customer(
regist_no in varchar2,
out_cursor out t_cursor);
END ABCDE_PKG_PTS_TEST;
/
create or replace package body ABCDE_PKG_PTS_TEST
as
procedure query_send_msg_info(
maxDate in varchar2,
out_cursor out t_cursor) is
begin
if (maxDate is not null) then
begin
open out_cursor for
select *
from (select p.id_pts_partner_task task_id,
p.car_mark,
t.report_name,
p.regist_no,
TO_CHAR(p.date_created, 'YYYY-MM-DD HH24:MI:SS') as date_created,
(case
when (select t.damage_id
from abc_recommend t
where t.report_id = p.report_id and rownum=1) = '123' then
'0'
else
'1'
end) as flag_type
from abc_task p, abc_reportcase t
where t.report_id = p.report_id
and p.task_type = '0'
and p.date_created >
to_date(maxDate, 'yyyy-MM-dd hh24:mi:ss')
order by flag_type asc,p.date_created asc)
where rownum < 501;
DBMS_OUTPUT.PUT_LINE(maxDate);
end;
end if;
exception
when others then
rollback;
end query_send_msg_info;
procedure query_process_task_reportno(
regist_no in varchar2,
report_id in varchar2,
car_mark in varchar2,
out_cursor out t_cursor) is
v_condition varchar2(1000);
v_sql varchar2(10000);
begin
if (regist_no is not null) then
v_condition := ''''||regist_no||'''';
begin
if report_id is not null then
v_condition := ''''||regist_no ||'''and p.report_id ='''|| report_id||'''';
if car_mark is not null then
v_condition :=''''||regist_no ||'''and p.car_mark ='''|| car_mark ||'''and p.report_id ='''|| report_id||'''';
end if;
else
if car_mark is not null then
v_condition := ''''||regist_no ||'''and p.car_mark ='''|| car_mark||'''';
end if;
end if;
v_sql:='select p.id_pts_partner_task task_id,
p.car_mark,
TO_CHAR(p.date_created, ''YYYY-MM-DD HH24:MI:SS'') as date_created,
p.report_id,
p.regist_no,
(case
when p.task_status = ''04'' and
p.contact_customer_result = ''02'' and
p.keep_repair_result is null and p.task_type = ''0'' then
''联系失败''
when p.task_status = ''04'' and p.keep_repair_result = ''01'' and
p.task_type = ''0'' then
''留修成功''
when p.task_status = ''04'' and p.keep_repair_result = ''02'' and
p.task_type = ''0'' then
''留修失败''
when p.task_status = ''04'' and p.keep_repair_result = ''01'' and
p.task_type = ''1'' then
''客户自主到店''
end) as processed_Tasks_Result,
(select distinct (select cm.model_name
from car_model cm
where cm.model_code = tcp.vehicle_id)
from abc_reportcase tcr,
abc_policy tcp,
abc_relation tpr
where tcr.damage_id = tpr.damage_id
and tpr.order_id = tcp.order_id
and tcr.report_id = p.report_id) as car_type,
(case
when (select t.damage_id
from abc_recommend t
where t.report_id = p.report_id) = ''123'' then
''0''
else
''1''
end) as flag_type
from abc_task p
where p.regist_no = '|| v_condition ||'
order by flag_type desc,p.date_updated desc ';
open out_cursor for v_sql;
DBMS_OUTPUT.PUT_LINE(v_condition);
DBMS_OUTPUT.PUT_LINE(v_sql);
end;
end if;
exception
when others then
rollback;
end query_process_task_reportno;
procedure query_need_confirm_repair(
regist_no in varchar2,
out_cursor out t_cursor) is
begin
if (regist_no is not null) then
begin
open out_cursor for
select p.id_pts_partner_task task_id,
p.car_mark,
TO_CHAR( p.date_confirm, 'YYYY-MM-DD HH24:MI:SS') as date_created,
p.report_id,
p.regist_no,
p.contact_customer_result,
(select distinct (select cm.model_name
from car_model cm
where cm.model_code = tcp.vehicle_id)
from abc_reportcase tcr,
abc_policy tcp,
abc_relation tpr
where tcr.damage_id = tpr.damage_id
and tpr.order_id = tcp.order_id
and tcr.report_id = p.report_id) as car_type,
(case
when (select t.damage_id
from abc_recommend t
where
t.report_id = p.report_id) = '123' then
'0'
else
'1'
end) as flag_type
from abc_task p
where p.regist_no = regist_no
-- and p.TASK_STATUS = '02'
-- and p.date_created > trunc(sysdate) - 1
order by flag_type desc,p.date_updated desc ;
end;
end if;
exception
when others then
rollback;
end query_need_confirm_repair;
procedure query_need_contact_customer(
regist_no in varchar2,
out_cursor out t_cursor) is
begin
if (regist_no is not null) then
begin
open out_cursor for
select p.id_pts_partner_task task_id,
p.car_mark,
TO_CHAR(p.date_created, 'YYYY-MM-DD HH24:MI:SS') as date_created,
p.report_id,
p.regist_no,
p.contact_customer_result,
(case
when (select t.damage_id
from abc_recommend t
where t.report_id = p.report_id) = '123' then
'0'
else
'1'
end) as flag_type,
(select r.report_name
from abc_reportcase r
where r.report_id = p.report_id) report_name
from abc_task p
where p.regist_no = regist_no
--and p.TASK_STATUS = '00'
-- and p.date_created > trunc(sysdate) - 1
order by flag_type desc,p.date_updated desc;
end;
end if;
exception
when others then
rollback;
end query_need_contact_customer;
end ABCDE_PKG_PTS_TEST;
/
注:这里有个疑惑,不能关闭游标释放资源(因为关闭游标ibatis就不能拿到数据),所以怀疑是自动关闭游标,或者ibatis代码里面进行了游标关闭。但是在测试的时候当点击游标值(<Cursor>)的时候,只能点击一次,第二次就没有数据了(说没有执行),因此这里也可以看为是数据关闭了。
ibatis实现部分举例一:
<resultMap id="querySmsResultSet" class="com.dto.ibatis.QuerySmsLogDTO">
<result property="msgId" column="task_id"/>
<result property="msgContent" column="car_mark"/>
<result property="mobileNo" column="report_name"/>
<result property="serviceId" column="regist_no"/>
<result property="date_created" column="date_created"/>
<result property="flag_type" column="flag_type"/>
</resultMap>
<parameterMap id="querysmsinfo_getSmsInfoMap" class="java.util.Map">
<parameter property="maxDate" jdbcType="VarChar" javaType="java.lang.String" mode="IN" />
<parameter property="out_cursor" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="querySmsResultSet" />
</parameterMap>
<procedure id="query_send_msg_info" parameterMap="querysmsinfo_getSmsInfoMap">
<![CDATA[
call pacde.ABCDE_PKG_PTS_TEST.query_send_msg_info(?,?)
]]>
</procedure>
java实现:
public List getSmsInfoList(Map param) throws PafaDAOException
{
this.getSqlMapClientTemplate().queryForList("query_send_msg_info", param);
QuerySmsLogDTO querySmsLogDTO = null;
Iterator it = (List)param.get("out_cursor").iterator();
while(it.hasNext()){
querySmsLogDTO = (QuerySmsLogDTO)it.next();
}
return (List)param.get("out_cursor");}