Provide spring bean as a type-handler

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Provide spring bean as a type-handler

Jatin Puri
Hello.

First thanks for all the help that this group has provided.
I have a small problem in my hand. Some preface: we currently use only XML for mybatis and not annotations. So of-course our typehandler's are also declared in the XML file. And we use spring-mybatis.

The use case is that: in a table, there is a column named `userID`, and we wish to read this user-id from the database, and access our `application cache` to obtain the cached-user-object for that id, and then set that id in the domain object.

This can be achieved using the type-handlers: 

<result property="userObject" column="userID" typeHandler="com.company.UserHandler"/>

`UserHandler` would extract the integer from the column `userID` and access application cache to return the `User` object. Which would be set for property `userObject`

This works, but I was wondering if the type handler itself could be a spring bean. This way all the dependencies of accessing cache would be set and nothing much to be done. How do I achieve this?

Jatin

--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Provide spring bean as a type-handler

Iwao AVE!
Hi Jatin,

It should work if you register the bean (i.e. an instance of UserHandler) to TypeHandlerRegistry before mappers are parsed.

If you have difficulty, please create MCVE/SSCCE so that we can test it.
Here are some project templates.

Regards,
Iwao


2017-04-05 19:48 GMT+09:00 Jatin Puri <[hidden email]>:
Hello.

First thanks for all the help that this group has provided.
I have a small problem in my hand. Some preface: we currently use only XML for mybatis and not annotations. So of-course our typehandler's are also declared in the XML file. And we use spring-mybatis.

The use case is that: in a table, there is a column named `userID`, and we wish to read this user-id from the database, and access our `application cache` to obtain the cached-user-object for that id, and then set that id in the domain object.

This can be achieved using the type-handlers: 

<result property="userObject" column="userID" typeHandler="com.company.UserHandler"/>

`UserHandler` would extract the integer from the column `userID` and access application cache to return the `User` object. Which would be set for property `userObject`

This works, but I was wondering if the type handler itself could be a spring bean. This way all the dependencies of accessing cache would be set and nothing much to be done. How do I achieve this?

Jatin

--
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].
For more options, visit https://groups.google.com/d/optout.

--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Provide spring bean as a type-handler

Ikchan Sim
In reply to this post by Jatin Puri

2017년 4월 5일 (수) 오후 7:48, Jatin Puri <[hidden email]>님이 작성:
Hello.

First thanks for all the help that this group has provided.
I have a small problem in my hand. Some preface: we currently use only XML for mybatis and not annotations. So of-course our typehandler's are also declared in the XML file. And we use spring-mybatis.

The use case is that: in a table, there is a column named `userID`, and we wish to read this user-id from the database, and access our `application cache` to obtain the cached-user-object for that id, and then set that id in the domain object.

This can be achieved using the type-handlers: 

<result property="userObject" column="userID" typeHandler="com.company.UserHandler"/>

`UserHandler` would extract the integer from the column `userID` and access application cache to return the `User` object. Which would be set for property `userObject`

This works, but I was wondering if the type handler itself could be a spring bean. This way all the dependencies of accessing cache would be set and nothing much to be done. How do I achieve this?

Jatin

--
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].
For more options, visit https://groups.google.com/d/optout.
--
 
==================================
IkChan SIM
Software Architeture.
[hidden email]
H.P : 010 - 3479 - 3242
Nothing in the world can take the place of persistence.
==================================

--
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].
For more options, visit https://groups.google.com/d/optout.
Loading...