how to enum in a if with mybatis annotation

659 views
Skip to first unread message

jdeo...@gmail.com

unread,
Dec 21, 2020, 5:23:19 AM12/21/20
to mybatis-user

Hello

I have a problem to write the syntax using an enum in a if in MyBatis annotation.


@Delete("<script>DELETE FROM tb_a WHERE " 
 + "<if test='context.name().equals(ALBUM)'>"
 + " album_id = #{id}" + "</if>"
 + "<if test='context.name().equals(VIDEOLIBRARY)'>"
 + " videolibrary_id = #{id}" + "</if>" + "</script>")

 

the correct syntax as fare I know is like this 

<if test="context.name().equals('ALBUM')"> 

But I don't know how to write it with annotation. I get this error message 

Error.png

Thanks for your help

Guy Rouillier

unread,
Dec 21, 2020, 6:37:49 AM12/21/20
to MyBatis User
This is really a Java syntax question, and not related to MyBatis.  To use an enum, you have to supply the enum type in the reference.  You have two enum values in your example, ALBUM and VIDEOLIBRARY.  Those values are defined by some enum type, maybe something like com.mybusiness.testapp.MediaTypeEnum (just a guess on my part.).  To refer to a specific enum value, you would then use:

import com.mybusiness.testapp.MediaTypeEnum;
...
<if test='context.name().equals(MediaTypeEnum.ALBUM)'>


--
Guy Rouillier
--
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/4165294a-4ada-4bf8-ae2d-64a6ee3a24a0n%40googlegroups.com.

jdeo...@gmail.com

unread,
Dec 21, 2020, 6:56:01 AM12/21/20
to mybatis-user
Thanks to try to help me Guy.

If you read attentively what I wrote you will see that I have a problem to find the correct writing : 'ALBUM' or "ALBUM" writing doesn't right in annotation writing.
I also tried `ALBUM` and it looks doesn't righting too.

I hope you understand better my need

Thanks a lot

Erwan Letessier

unread,
Dec 21, 2020, 8:27:32 AM12/21/20
to mybati...@googlegroups.com
import com.mybusiness.testapp.MediaTypeEnum;
would help only if you could write :  + "<if test='context.name().equals(" + MediaTypeEnum.ALBUM + ")'>",
but the @Delete annotation takes a String as parameter, then you can only concatenate constant Strings.
Being inside <script> tags, is just like being into the XML file, so runtime resolution.
So you can try dereferencing the enum with fully qualified name: <if test='context.name().equals(com.mybusiness.testapp.MediaTypeEnum.ALBUM)'>: what is inside the test attribute is/must be valid java code.
Maybe then, reference could be shortened to the simple name by defining type alias from fully qualified name to simple name of MediaTypeEnum.


Erwan Letessier


jdeo...@gmail.com

unread,
Dec 21, 2020, 9:29:54 AM12/21/20
to mybatis-user
Guys, I'm so so sorry. I'm learning and I really understand what all of you are saying but I don't arrive to make it in practice.

So base on what you say I did this:
Enum class :

public enum EContext {
ALBUM,
VIDEOLIBRARY,
IMAGE,
VIDEO,
}


In the mapper:

@Delete("<script>DELETE FROM tb_some_table WHERE "
+ "<if test='context.name().equals(EContext.ALBUM)'>"
+ " album_Id = #{id}"
+ "</if>"
+ "<if test='context.name().equals(EContext.VIDEOLIBRARY)'>"
+ " videoLibrary_Id = #{id}"
+ "</if>"
  + "</script>")
boolean deleteShared(@Param("id") int id, @Param("context") EContext context);


Now I get this error :
nested exception is org.apache.ibatis.binding.BindingException: Parameter 'EContext' not found.

Thank thank so much to help me to understand

jdeo...@gmail.com

unread,
Dec 21, 2020, 12:08:38 PM12/21/20
to mybatis-user
I got the solution from an other forum. 
This is the correct writing.

"<if test=\"context.name().equals('ALBUM')\">"

Diolix

unread,
Dec 22, 2020, 5:07:33 PM12/22/20
to mybati...@googlegroups.com
Hello I have a problem to write the syntax using an enum in a if in MyBatis annotation.
@Delete("<script>DELETE FROM tb_a WHERE " + "<if test='context.name().equals(ALBUM)'>" + " album_id = #{id}" + "</if>" + "<if test='context.name().equals(VIDEOLIBRARY)'>" + " videolibrary_id = #{id}" + "</if>" + "</script>")
the correct syntax as fare I know is like this But I don't know how to write it with annotation. I get this error message enter image description here
Error
Thanks for your help

Sent from the mybatis-user mailing list archive at Nabble.com.
Reply all
Reply to author
Forward
0 new messages