開始
通過搜索引擎搜索了獲取返回值的Mybatis註解配置方式,但是都搜索不到,都是xml配置方式,嘗試自己寫出來了。
過程
- 首先要有一個存儲過程,in,out值。
- 配置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
表示 存儲過程
- 配置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
值。
總結
多動手去嘗試不同的實現方式,才能提升自己的動手、思考的能力。