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

    @select 添加判断

    添加两个 使用例子, 供参考:
    @Select({"<script>",
    "select",
    " seatName,SUM(profit) as profit",
    "from td_seat_single_code_profit",
    "where code = #{queryType,jdbcType=VARCHAR} ",
    "<![CDATA[ and tradeDate >= #{startDate,jdbcType=INTEGER} and tradeDate <= #{endDate,jdbcType=INTEGER} ]]>",
    "GROUP BY seatName ",
    "<if test='!flag'>ORDER BY profit </if> ",
    "<if test='flag'>ORDER BY profit desc</if> ",
    "LIMIT #{rankSize,jdbcType=INTEGER}",
    "</script> "})
    List<TdSeadeProfit> getSeatProfitListByParam(@Param("rankSize") int rankSize, @Param("startDate") int startDate, @Param("endDate") int endDate, @Param("queryType") String queryType, @Param("flag") boolean flag);

    @Select({"<script>",
    "SELECT seatName,SUM(profit) as profit FROM td_seat_code_chain_profit where ",
    " type = #{queryFlag,jdbcType=INTEGER}",
    " <if test='queryFlag ==1'>and tradingCode =#{queryType,jdbcType=VARCHAR} </if>",
    " <if test='queryFlag ==2'>and industrialChain =#{queryType,jdbcType=VARCHAR} </if>",
    "<![CDATA[ and tradeDate >= #{startDate,jdbcType=INTEGER} and tradeDate <= #{endDate,jdbcType=INTEGER} ]]>",
    "GROUP BY seatName ",
    "<if test='!flag'>ORDER BY profit </if> ",
    "<if test='flag'>ORDER BY profit desc</if> ",
    "LIMIT #{rankSize,jdbcType=INTEGER}",
    "</script> "})
    List<TdSeatCinProfit> getSeatProfitListByParam(@Param("rankSize") int rankSize, @Param("startDate") int startDate, @Param("endDate") int endDate, @Param("queryFlag") int queryFlag, @Param("queryType")String queryType,@Param("flag") boolean flag);

    注意大于小于的转义或者使用 <![CDATA[ ]]>包裹表达式


    今天要获得存储过程的返回值,但不想用call方法感觉太麻烦, 

    <select id="getUpdateHumanDisease" resultType="java.lang.String">
        declare @result varchar(50)
        exec updateHumanDisease #{0},#{1},#{2},@result output
        select @result
    </select>
  • 相关阅读:
    java 日期格式 毫秒 表示方法
    Java的框架是什么意思
    圣思源Java视频36节练习源码分享(自己的190+行代码对比老师的39行代码)
    预装win8的笔记本如何重装win7
    Word 2013 无法撤销操作的错误
    面试经验分享(跳槽季~~~❀)
    一些可能有点用处的C#开发经验
    电子设计解决方案透视
    突破限速
    当区块链遇上Rust
  • 原文地址:https://www.cnblogs.com/huoqm/p/13877245.html
Copyright © 2020-2023  润新知