Mybatis使用註解的方式執行存儲過程並獲取返回值

開始

通過搜索引擎搜索了獲取返回值的Mybatis註解配置方式,但是都搜索不到,都是xml配置方式,嘗試自己寫出來了。

過程

  1. 首先要有一個存儲過程,in,out值。
  2. 配置mapper:
    部分代碼:
    //mybatis 註解 調用存儲過程
	@Select({
			"call execute_seckill(",
			"#{map.seckillId,mode=IN,jdbcType=BIGINT},",
			"#{map.userPhone,mode=IN,jdbcType=BIGINT},",
			"#{map.killTime,mode=IN,jdbcType=TIMESTAMP},",
			"#{map.result,mode=OUT,jdbcType=INTEGER});"
	})
	@Results({
			@Result(column="result", property="result", jdbcType= JdbcType.INTEGER)
	})
	@Options(statementType = StatementType.CALLABLE)
	void killByProcedure(@Param("map") Map map);

StatementType.CALLABLE 表示 存儲過程

  1. 配置service
    部分代碼:
        Map<String, Object> map = new HashMap<>();
		map.put("seckillId", seckillId);
		map.put("userPhone", userPhone);
		map.put("killTime", killTime);
		map.put("result", null);
		try {
			seckillCustomMapper.killByProcedure(map);
			// 獲取result
			System.out.println(map.get("result"));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

通過map相對應的可以獲取到result值。

總結

多動手去嘗試不同的實現方式,才能提升自己的動手、思考的能力。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章