Returning values from MyBatis mapped methods

前端 未结 5 811
无人及你
无人及你 2020-12-28 08:33

I have a Java project that uses MyBatis to access a PostgreSQL database. PostgreSQL allows to return fields of a newly created row after an INSERT statement, an

5条回答
  •  执笔经年
    2020-12-28 09:10

    The return type of mapped insert method can be void or int (in which case it will return the number of the inserted row). You can do the following mechanism to return the generated id:

    
      
        SELECT currval('my_seq')
      
      INSERT INTO mytable(col1, col2) VALUES (#{val1}, #{val2})
    
    

    This will set generated id column to id property of your parameter class. After that, object you passed as parameter will have generated id set in its property.

提交回复
热议问题