【问题标题】:MyBatis multiple operationsMyBatis 多重操作
【发布时间】:2014-08-22 10:55:51
【问题描述】:

我在code 下方进行更新并获取下一个值。

   <update id="updateNextReference">
        UPDATE table_name
        SET value
        WHERE condition
   </update>


   <select id="selectNextReference" resultType="java.lang.Integer">     
        SELECT value
        FROM <<table>>
        WHERE <<condition>>
   </select>

是否可以以我使用SelectKey 更新和获取值的方式执行这两种操作。

我尝试在update 中添加SelectKey,但这不起作用。

【问题讨论】:

    标签: sql select sql-update mybatis


    【解决方案1】:

    如果您在映射器 xml 文件中使用标签 &lt;insert&gt; 而不是 &lt;update&gt;,它将起作用。

    当我尝试使用 &lt;update&gt; 标记进行相同操作时,在读取映射器文件时出现异常。

    <insert id="selectKeyTest" parameterType="MyClass">
            <selectKey keyProperty="prop1" order="AFTER" resultType="Long" >
                select col1 from myTable WHERE col2 = #{prop2}
            </selectKey>
            UPDATE myTable 
            SET col1 = 1
            WHERE col2= #{prop2}
    </insert>
    

    update 语句执行后,select 语句会将 col1 的值设置为 MyClass 中的 prop1 属性。所以,在java中:

    MyClass myObj = new MyClass();
    myObj.setProp2("2");
    myService.getMapper(SelectKeyMapper.class).selectKeyTest(myObj);
    String resultOfSelectKey = myObj.getProp1();
    

    【讨论】:

    • 但是如何在java端获取col1的值呢?
    • 如果您将 myClass 对象作为参数传递,“prop1”将是此类中的一个属性。调用“selectKeyTest”后,可以通过getProp1() 获取col1 的值。或者,您可以通过 @Param("prop1") String prop1 将 prop1 直接传递给“selectKeyTest”。在您调用“selectKeyTest”之后,您将再次在“prop1”中获得 col1 的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多