custom setter method

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

custom setter method

Raj Nagappan
Hi all,

Is there a way to direct Mybatis 3 to use a custom setter prefix if it
can find one? In our public setter methods we often have to do some
custom setup code, so at the moment we end up with something like:

// for Ibatis 2.x
private void setFoo(Foo foo) {
    this.foo = foo;
}

// for all other code
public void setFooLazyLoad(Foo foo) {
    setFoo(foo);
    // other setup code here
}

Ideally what I would like is some setter prefix in the config to tell
Mybatis to search for methods starting with that prefix if it exists
for setting fields. e.g.

<setterPrefix>init</setterPrefix> or <setting name="setterPrefix"
value="init"/>

would cause Mybatis to search for
1. initFoo(), then
2. setFoo()
in the above example.

Or perhaps the ability to specify the setter method on the result map?

Alternatively is there some way to control Mybatis to set directly on
the field and to bypass the setter method? I prefer the above init...
() solution though as it's a bit easier to debug ;-)

Raj.
Reply | Threaded
Open this post in threaded view
|

Re: custom setter method

Simone Tripodi
Hi Raj,
honestly I found it a little confusing, I'd suggest something like:

<result column="foo_column" property="foo" setterMethod="setFooLazyLoad" />

where if setterMethod is not specified, then the traditional setFoo
will be used.
Just my 2 cents, have a nice day,
Simo

http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Mon, Sep 13, 2010 at 1:15 AM, Raj Nagappan <[hidden email]> wrote:

> Hi all,
>
> Is there a way to direct Mybatis 3 to use a custom setter prefix if it
> can find one? In our public setter methods we often have to do some
> custom setup code, so at the moment we end up with something like:
>
> // for Ibatis 2.x
> private void setFoo(Foo foo) {
>    this.foo = foo;
> }
>
> // for all other code
> public void setFooLazyLoad(Foo foo) {
>    setFoo(foo);
>    // other setup code here
> }
>
> Ideally what I would like is some setter prefix in the config to tell
> Mybatis to search for methods starting with that prefix if it exists
> for setting fields. e.g.
>
> <setterPrefix>init</setterPrefix> or <setting name="setterPrefix"
> value="init"/>
>
> would cause Mybatis to search for
> 1. initFoo(), then
> 2. setFoo()
> in the above example.
>
> Or perhaps the ability to specify the setter method on the result map?
>
> Alternatively is there some way to control Mybatis to set directly on
> the field and to bypass the setter method? I prefer the above init...
> () solution though as it's a bit easier to debug ;-)
>
> Raj.
Reply | Threaded
Open this post in threaded view
|

RE: custom setter method

Poitras Christian
To use a specific setter methods, you can name the property differently in the resultMap. For example, you have a foo property with a setFooMyBatis and getFooMyBatis methods:
Protected/private FooClass getFooMyBatis() {return foo;}
protected/private void setFooMyBatis(FooClass foo)  {this.foo = foo; /*some other code;*/}
In the resultMap
<result property="fooMyBatis" column="foo"/>


Note that MyBatis will set the field directly if it cannot find a setter method. So you can remove the setFoo() method.


Specifying a setterMethod in the resultMap would require a change in the MyBaits code so I don't think it's the best option. That seems even worse if MyBatis would need to call the regular getter - getFoo - to set foo's sub-properties but a custom setter to set the base property - initFoo() in your case.

Christian

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de Simone Tripodi
Envoyé : September-13-10 2:05 AM
À : [hidden email]
Objet : Re: custom setter method

Hi Raj,
honestly I found it a little confusing, I'd suggest something like:

<result column="foo_column" property="foo" setterMethod="setFooLazyLoad" />

where if setterMethod is not specified, then the traditional setFoo
will be used.
Just my 2 cents, have a nice day,
Simo

http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Mon, Sep 13, 2010 at 1:15 AM, Raj Nagappan <[hidden email]> wrote:

> Hi all,
>
> Is there a way to direct Mybatis 3 to use a custom setter prefix if it
> can find one? In our public setter methods we often have to do some
> custom setup code, so at the moment we end up with something like:
>
> // for Ibatis 2.x
> private void setFoo(Foo foo) {
>    this.foo = foo;
> }
>
> // for all other code
> public void setFooLazyLoad(Foo foo) {
>    setFoo(foo);
>    // other setup code here
> }
>
> Ideally what I would like is some setter prefix in the config to tell
> Mybatis to search for methods starting with that prefix if it exists
> for setting fields. e.g.
>
> <setterPrefix>init</setterPrefix> or <setting name="setterPrefix"
> value="init"/>
>
> would cause Mybatis to search for
> 1. initFoo(), then
> 2. setFoo()
> in the above example.
>
> Or perhaps the ability to specify the setter method on the result map?
>
> Alternatively is there some way to control Mybatis to set directly on
> the field and to bypass the setter method? I prefer the above init...
> () solution though as it's a bit easier to debug ;-)
>
> Raj.
Reply | Threaded
Open this post in threaded view
|

Re: custom setter method

Raj Nagappan
Thanks guys. Poitras that is a good idea I will give it a try.

Raj.

On Sep 13, 9:51 pm, Poitras Christian <[hidden email]>
wrote:

> To use a specific setter methods, you can name the property differently in the resultMap. For example, you have a foo property with a setFooMyBatis and getFooMyBatis methods:
> Protected/private FooClass getFooMyBatis() {return foo;}
> protected/private void setFooMyBatis(FooClass foo)  {this.foo = foo; /*some other code;*/}
> In the resultMap
> <result property="fooMyBatis" column="foo"/>
>
> Note that MyBatis will set the field directly if it cannot find a setter method. So you can remove the setFoo() method.
>
> Specifying a setterMethod in the resultMap would require a change in the MyBaits code so I don't think it's the best option. That seems even worse if MyBatis would need to call the regular getter - getFoo - to set foo's sub-properties but a custom setter to set the base property - initFoo() in your case.
>
> Christian
>
> -----Message d'origine-----
> De : [hidden email] [mailto:[hidden email]] De la part de Simone Tripodi
> Envoyé : September-13-10 2:05 AM
> À : [hidden email]
> Objet : Re: custom setter method
>
> Hi Raj,
> honestly I found it a little confusing, I'd suggest something like:
>
> <result column="foo_column" property="foo" setterMethod="setFooLazyLoad" />
>
> where if setterMethod is not specified, then the traditional setFoo
> will be used.
> Just my 2 cents, have a nice day,
> Simo
>
> http://people.apache.org/~simonetripodi/http://www.99soft.org/
>
> On Mon, Sep 13, 2010 at 1:15 AM, Raj Nagappan <[hidden email]> wrote:
> > Hi all,
>
> > Is there a way to direct Mybatis 3 to use a custom setter prefix if it
> > can find one? In our public setter methods we often have to do some
> > custom setup code, so at the moment we end up with something like:
>
> > // for Ibatis 2.x
> > private void setFoo(Foo foo) {
> >    this.foo = foo;
> > }
>
> > // for all other code
> > public void setFooLazyLoad(Foo foo) {
> >    setFoo(foo);
> >    // other setup code here
> > }
>
> > Ideally what I would like is some setter prefix in the config to tell
> > Mybatis to search for methods starting with that prefix if it exists
> > for setting fields. e.g.
>
> > <setterPrefix>init</setterPrefix> or <setting name="setterPrefix"
> > value="init"/>
>
> > would cause Mybatis to search for
> > 1. initFoo(), then
> > 2. setFoo()
> > in the above example.
>
> > Or perhaps the ability to specify the setter method on the result map?
>
> > Alternatively is there some way to control Mybatis to set directly on
> > the field and to bypass the setter method? I prefer the above init...
> > () solution though as it's a bit easier to debug ;-)
>
> > Raj.