vinothkr
vinothkr

Reputation: 1270

MyBatis mapper to call factory method

I want mybatis to call a factory method to create an object instead of a constructor. So that for null valued attributes i can return a NULL object(which has overridden behavior to handle all the edge cases) instead of actual object. Can i achieve that with mapper.xml?

Upvotes: 2

Views: 3520

Answers (2)

user507484
user507484

Reputation:

Define your own ObjectFactory:

https://mybatis.org/mybatis-3/configuration.html#objectFactory

Upvotes: 1

quux00
quux00

Reputation: 14604

To answer your specific question, there is no way to specify a factory method directly (and only) in the mapper.xml file itself, as far as I know. However, there are two options in MyBatis to do what you want:

  1. As stated in Bhaskar's answer you can use an ObjectFactory.
  2. In theory, you can also define a TypeHandler, but I was unable to get this to work in my recent testing.

If you would like to see a working example of how to use a MyBatis ObjectFactory to implement a Null object, see koan19 of my MyBatis koans: https://github.com/midpeter444/mybatis-koans. (Look in the completed-koans/koan19 directory for the solution I came up with.)

Upvotes: 0

Related Questions