Mybatis如何調用oracle存儲過程?入參爲日期類型

 

存儲過程以下:java

傳入開始、結束日期,並返回對應日期內的數據!mybatis

create or replace PROCEDURE PROC_GETGONGGUREPORT(
    in_beginDate Date, //入參:開始日期
    in_endDate Date,	//入參:結束日期
    cursor_out out sys_refcursor	//出參:遊標
) 
AS in_nextDate date :=in_endDAte +1;

BEGIN
。。。。//省略代碼

 

1. 使用Map封裝入參、出參數據

Controller:app

    @RequestMapping("/getNoCarInfo")
    @ResponseBody
    public R TESTPRO2(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime
    ) throws ParseException {
        HashMap map = new HashMap();
        map.put("in_beginDate", startTime); //入參:開始日期
        map.put("in_endDate", endTime);	//入參:結束日期
        map.put("cursor", null);	//出參:遊標
        nocarService.getNocarInfo(map); //調用存儲過程
        System.out.println(map.get("cursor") + "***************");
        List<Map> returnMap = (List<Map>) map.get("cursor"); //以map的形式獲取結果

Serviceide

@Service
public class NocarService {


    @Autowired
    NocarDao nocarDao;

    public void getNocarInfo(HashMap map){

        nocarDao.getNocarInfo(map);
    }
}

Daoui

@Mapper
public interface NocarDao {
    void getNocarInfo(@Param("map") HashMap map);
}

XML文件spa

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.chinalife.renshang.nocar.NocarDao">

	 <!--遊標結果封裝 -->
    <resultMap id="cursorNocarMap" type="java.util.LinkedHashMap"></resultMap>
    
    <!--調用存儲過程 -->
    <select id="getNocarInfo" statementType="CALLABLE" parameterType="map">

        <![CDATA[
            call PROC_GETGONGGUREPORT(
                 to_date( #{map.in_beginDate,mode=IN,jdbcType=DATE},'yyyy-MM-dd'),
                  to_date(#{map.in_endDate,mode=IN,jdbcType=DATE},'yyyy-MM-dd'),
                  #{map.cursor,mode=OUT,jdbcType=CURSOR,resultMap=cursorNocarMap
                   })
        ]]>

    </select>

</mapper>

調用結果:
在這裏插入圖片描述
 orm

2. 使用對象封裝入參、出參數據

對象xml

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestParam {

    private Date in_beginDate;  //入參:開始日期
    private Date in_endDate;	//入參:結束日期
    private List<Map> cursor;	//出參:遊標
}

Controller對象

    @RequestMapping("/getCarInfo")
    @ResponseBody
    public R getCarInfo(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime
    ) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = format.parse(startTime);
        Date end = format.parse(endTime);
        //封裝對象
        TestParam testParam = new TestParam(start, end, null);
        //調用存儲過程
        carService.getCarInfo1(testParam);
        List<Map> carMap = testParam.getCursor();

Daoblog

@Mapper
public interface CarDao {
    void getCarInfo1(@Param("testParam")TestParam testParam);
}

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.chinalife.renshang.car.CarDao">


    <resultMap id="cursorCarMap" type="java.util.LinkedHashMap"></resultMap>
    
    <!--調用存儲過程 -->
    <select id="getCarInfo1" statementType="CALLABLE" parameterType="com.chinalife.renshang.car.TestParam" >
    
		<![CDATA[
	      	call PROC_GETGONGGUREPORT(
	              #{testParam.in_beginDate,mode=IN,jdbcType=DATE},
	               #{testParam.in_endDate,mode=IN,jdbcType=DATE},
	                #{testParam.cursor,mode=OUT,jdbcType=CURSOR,resultMap=cursorNocarMap
	                 })
	 	]]>

    </select>

</mapper>

一樣能夠調用成功

相關文章
相關標籤/搜索