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值。

总结

多动手去尝试不同的实现方式,才能提升自己的动手、思考的能力。

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