How can I modify the default IntegerTypeHandler in Mybatis

24 views
Skip to first unread message

yuan sun

unread,
Oct 3, 2022, 10:47:14 PM10/3/22
to mybatis-user

I try to use <select> to query int value in mysql with mybatis, it return null when nothing matches.

Howerver the corresponding method in XXXMapper.java return the primitive type int, which case exception: attempted to return null from a method with a primitive return type (int).

So i want to modify the default IntegerTypeHandler in mybatis, i want MyConfigIntegerTypeHandler returns -1 when nothing found.

I know the best way to solve this problem is to modify the method defination, to make the query method return Integer rather than int. I just want to know how to fix it without corrent the origin code.

Guy Rouillier

unread,
Oct 4, 2022, 2:08:45 AM10/4/22
to mybatis-user
Are you able to modify the SQL used in this SELECT?  If so, MySQL has the IFNULL() function.  That would be simpler than implementing a specific type handler. But if you cannot alter SQL for some reason, then just create your own typehandler for this column.  No need to modify the default IntegerTypeHandler.

--
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/600bd6f2-bf8b-47d5-a529-d4c466caa158n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages