Best practice

22 views
Skip to first unread message

Simeo Reig

unread,
Jan 28, 2021, 11:02:58 AM1/28/21
to mybati...@googlegroups.com
Hi

   What is the best practice for updating an id field? This is not gonna work.

image.png
Is there other possibility than using params fields? This is a reduced model, there are many fields in real table. Can we pass other parameterType?

is it possible to "create" new vars with selectKey? One possibility could do a selectkey Before to recover old id

Many thanks !






Iwao AVE!

unread,
Jan 28, 2021, 1:21:35 PM1/28/21
to mybatis-user

Hello Simeo,

Please post code in text instead of image.
If the code is in an image, we have to type everything manually instead of copy and modify.
Plain text is just fine, but if you want syntax-coloring, try markdown-here.
https://markdown-here.com/

Anyway, you can pass multiple parameters to a statement.
Use @Param annotation to name each parameter.

int updateDocument(
  @Param("document") Document, 
  @Param("newId") Integer newId);

The statement would look like the following.
In this case, the correct parameterType is map, but it’s unnecessary.

<update id="updateDocument">
  UPDATE myschema.documents
  <set>
    <if test="newId != null">
      identitat = #{newId}
    </if>
    <if test="document.textDocument != null">
      textdocument = #{document.textDocument}
    </if>
    ...

<resultMap> is irrelevant here because it is used only when mapping query results.

Regards,
Iwao


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAJJNRpM6h4aXL%3DYO_5v%2Ba8k-%2Bn-xWa_wwv3jcsTe3LjGP8r%3DSQ%40mail.gmail.com.

Simeo Reig

unread,
Jan 28, 2021, 2:03:41 PM1/28/21
to mybati...@googlegroups.com
Hi again

  Yes, We ended up with this solution but we wanted to avoid it because we would preferd the "OO" solution by sending a resultType. In the end we have two update methods: One general with the resultType and another one that change id. Not a big deal in the end.

Many many thanks for your help and thanks for this fantastic framework!

We love mybatis!!

Missatge de Iwao AVE! <hara...@gmail.com> del dia dj., 28 de gen. 2021 a les 19:21:

Guy Rouillier

unread,
Jan 28, 2021, 9:30:46 PM1/28/21
to MyBatis User
Why do you need to create a new variable to use with selectKey?  You've already got identitat in your Documents parameterType.  Not sure what you mean by "Can we pass other parameterType?"  You can pass whatever parameter type you wish.  If you mean can you pass multiple parameters, yes you can, I see Iwao already addressed that question.  Under the covers, MyBatis puts all those parameters into a hashmap, so in reality, you are always passing a single parameterType.  Hope this helps.

--
Guy Rouillier
Reply all
Reply to author
Forward
0 new messages