Creating a custom type handler without annotations

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

Creating a custom type handler without annotations

sreekanth.r.vanguru
Hi,

I'm new to mybatis. I am trying to map a JDBC integer to a custom class. All the examples that I have seen on this have used annotations, is it possible to not use annotations and do this? Any example would be greatly appreciated.

Sreekanth

--
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
|

Re: Creating a custom type handler without annotations

Guy Rouillier-2
Certainly, the TypeHandler concept precedes annotations.  Take a look at the MyBatis User's Guide, the typeHandlers section (3.1.4 in version 3.4.5).  To map a column to a particular TypeHandler, register your TypeHandler like so:

    <typeHandlers>
        <typeHandler javaType="com.mycompany.db.MyOption" jdbcType="INTEGER" handler="com.mycompany.db.MyOptionTypeHandler"/>
    </typeHandlers>

This *should* be sufficient to allow MyBatis to automatically invoke your TypeHandler whenever it retrieves an integer from the database, and the target for that value is the MyOption class.  If for some reason that doesn't happen automatically, then you can specify the typehandler for that column in your XML mapper file.  In my experience, MyBatis is able to automatically perform this mapping the majority of the time.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 5/4/2018 12:04:04 PM
Subject: Creating a custom type handler without annotations

Hi,

I'm new to mybatis. I am trying to map a JDBC integer to a custom class. All the examples that I have seen on this have used annotations, is it possible to not use annotations and do this? Any example would be greatly appreciated.

Sreekanth

--
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
|

Re: Creating a custom type handler without annotations

sreekanth.r.vanguru
Guy,
 Thanks a lot for your reply, this work for me. I have another follow up question.

I have a POJO that has a field VlanIdInner whose setters and getters look as follows:

public class POJO {
    private Tag _IdInner;
    public Tag getIdInner() {
        return _IdInner;
    }
    public void setIdInner(final VlanTag value) {
        this._Inner = value;
    }
}

where Tag is as follows:
public class Tag {
 rivate final java.lang.Long _value;
 public Tag(java.lang.Long _value) {
        this._value = _value;
    }
}

I'm using a custom typehandler to convert between POJO object field IdInner and the DB object where DB object type is jdbctype.LONG.
Is there a better way (more efficient) to do this conversion instead g of writing a custom type handler because VlanTag construction take long as an argument in it's constructor?

--
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
|

Re[2]: Creating a custom type handler without annotations

Guy Rouillier-2
Is your class Tag just an example, and the real one has a bunch of other fields?  I ask because I don't understand why you are creating a class with one field.  Why not just make a simple field in the outer class called Tag that is a long?

If you want to maintain the inner class, you don't need a TypeHandler to set it.  You can do that in a straightforward ResultMap.  See "association" in the User's Guide.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 5/7/2018 3:40:10 PM
Subject: Re: Creating a custom type handler without annotations

Guy,
 Thanks a lot for your reply, this work for me. I have another follow up question.

I have a POJO that has a field VlanIdInner whose setters and getters look as follows:

public class POJO {
    private Tag _IdInner;
    public Tag getIdInner() {
        return _IdInner;
    }
    public void setIdInner(final VlanTag value) {
        this._Inner = value;
    }
}

where Tag is as follows:
public class Tag {
 rivate final java.lang.Long _value;
 public Tag(java.lang.Long _value) {
        this._value = _value;
    }
}

I'm using a custom typehandler to convert between POJO object field IdInner and the DB object where DB object type is jdbctype.LONG.
Is there a better way (more efficient) to do this conversion instead g of writing a custom type handler because VlanTag construction take long as an argument in it's constructor?

--
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
|

Re: Re[2]: Creating a custom type handler without annotations

sreekanth.r.vanguru


HI Guy,
Yes, Tag has only field which is of type long, This is a POJO and I can't really change it. Also, IdInner is one of the fields in the final class that I want to create from DB. It has other fields like NameInner whose setters and getter are like:

public class POJO {
    private Name _NameInner;
    public Tag getNameInner() {
        return _NameInner;
    }
    public void setNameInner(final Name value) {
        this._NameInner = value;
    }
}

where Name is as follows:
public class Name {
 rivate final String _value;
 public Tag(String _value) {
        this._value = _value;
    }
}

Most of fields in the POJO are like this where they are built on top of simple data types like long, String etc instead of using simple data type directly. So, does it mean that I have no other choice but to use a separate custom type handler for each field type?

Sreekanth

--
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
|

Re[4]: Creating a custom type handler without annotations

Guy Rouillier-2
No, you don't need a TypeHandler.  You can set the values of the inner class in a ResultMap.  Look at association in the User's Guide.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 5/8/2018 8:39:04 AM
Subject: Re: Re[2]: Creating a custom type handler without annotations



HI Guy,
Yes, Tag has only field which is of type long, This is a POJO and I can't really change it. Also, IdInner is one of the fields in the final class that I want to create from DB. It has other fields like NameInner whose setters and getter are like:

public class POJO {
    private Name _NameInner;
    public Tag getNameInner() {
        return _NameInner;
    }
    public void setNameInner(final Name value) {
        this._NameInner = value;
    }
}

where Name is as follows:
public class Name {
 rivate final String _value;
 public Tag(String _value) {
        this._value = _value;
    }
}

Most of fields in the POJO are like this where they are built on top of simple data types like long, String etc instead of using simple data type directly. So, does it mean that I have no other choice but to use a separate custom type handler for each field type?

Sreekanth

--
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
|

Re: Re[4]: Creating a custom type handler without annotations

sreekanth.r.vanguru

 Thanks Guy for the useful pointers, I'm able to do this with associations.

 

--
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.