开发者

one-to-many insert with iBATIS

开发者 https://www.devze.com 2023-04-06 09:20 出处:网络
I have got the type Word with the property Topics which is a List object. I have two tables in the database: Words and Topics. I want write a word in Words table and store each topic with its correspo

I have got the type Word with the property Topics which is a List object. I have two tables in the database: Words and Topics. I want write a word in Words table and store each topic with its corresponding idWord in Topics. For the insert statement of the word I use generatedKey and keyPro开发者_StackOverflowperty to obtain the idWord that mySQL assign to the word.

But I don't know how to do this, I've read iBATIS documentation but this is too brief with INSERT statements, I could get data from Words and Topics to Word type with but not instead.

Thanks a lot!!

P.S. Could you suggest more iBATIS documentation??


You can use selectKey tag inside your first table inert query; this will return identity column value that got generated. Use this id to populate the second table.

 <insert id="InsertLineItem" parameterClass="LineItem">
  INSERT INTO [LinesItem] 
  (Order_Id, LineItem_LineNum, Item_Id, LineItem_Quantity, LineItem_UnitPrice)
  VALUES
  (#Order.Id#, #LineNumber#, #Item.Id#, #Quantity#, #Item.ListPrice#)

  <selectKey resultClass="int" keyProperty="id" >
    SELECT @@IDENTITY AS ID
  </selectKey>
  </insert>
0

精彩评论

暂无评论...
验证码 换一张
取 消