Best practice

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Best practice

Simeo Reig
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 !






--
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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Best practice

Iwao AVE!

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


On Fri, Jan 29, 2021 at 1:03 AM Simeo Reig <[hidden email]> wrote:
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 !






--
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2SE2GvMTWuLuYEzEmAKB-ZL_-yCPmWwf_EcMOaVw7sJHw%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Best practice

Simeo Reig
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! <[hidden email]> del dia dj., 28 de gen. 2021 a les 19:21:

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


On Fri, Jan 29, 2021 at 1:03 AM Simeo Reig <[hidden email]> wrote:
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 !






--
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2SE2GvMTWuLuYEzEmAKB-ZL_-yCPmWwf_EcMOaVw7sJHw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAJJNRpP_Eot_nuw4VJELNN3FS9DjS%2BRDnZijG4sZfw6Z3JtUKA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Best practice

Guy Rouillier-2
In reply to this post by Simeo Reig
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

On 1/28/2021 11:02:27 AM, "Simeo Reig" <[hidden email]> wrote:

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 !






--
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/ema5030c63-4718-4543-a166-79536ff5ac31%40asus.