ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olivier antoine <oli.anto...@gmail.com>
Subject Re: boolean JavaType and mapping
Date Fri, 20 Jan 2006 10:28:18 GMT
I have found qhat I want to do
It works very well

I discover that it already exists a class named BooleanTypeHandler which
make the staff (only for 0 and 1 value).
So we can extends that class to make it works with "yes", "no" values ...

Thanks a lot


2006/1/20, Gareth Moorst <gazj2ee@yahoo.co.uk>:
>
> Here's one I wrote to do just that. Feel free to use it (just don't tell
> my boss)
>
> package com.phones4u.datamanagement.datamodel.ibatis.typehandler;
>
> import java.sql.SQLException;
> import java.sql.Types;
>
> import com.ibatis.sqlmap.client.extensions.ParameterSetter;
> import com.ibatis.sqlmap.client.extensions.ResultGetter;
> import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
>
> /**
>  * Custom type handler for ibatis to convert between boolean and smallint
> values
>  * @author gareth.moorst
>  *
>  */
> public class SmallIntBooleanTypeHandler implements TypeHandlerCallback
> {
>
>     public void setParameter(ParameterSetter setter, Object parameter)
> throws SQLException
>     {
>         if (parameter == null) {
>             setter.setInt(0);
>         } else {
>             Boolean bool = (Boolean) parameter;
>             short boolVal = 0;
>             if (bool.booleanValue())
>                 boolVal = 1;
>             setter.setShort(boolVal);
>         }
>     }
>
>     public Object getResult(ResultGetter getter) throws SQLException
>     {
>         if (getter.wasNull())
>             return Boolean.FALSE;
>
>         short dbVal = getter.getShort();
>
>         if (dbVal == 1)
>             return Boolean.TRUE;
>
>         return Boolean.FALSE;
>     }
>
>     public Object valueOf(String s)
>     {
>         return s;
>     }
>
> }
>
>
> *Niels Beekman <n.beekman@wis.nl>* wrote:
>
> You could use a typehandler to do that:
> http://opensource2.atlassian.com/confluence/oss/display/IBATIS/How+do+I+
> use+a+Custom+Type+Handler+with+complex+property+or+Type+Safe+Enumeration
>
> Niels
>
> -----Original Message-----
> From: Olivier Antoine [mailto:oli.antoine@gmail.com]
> Sent: donderdag 19 januari 2006 23:20
> To: user-java@ibatis.apache.org
> Subject: boolean JavaType and mapping
>
> Hello everyone,
> I have to re-write a website with an existant MySQL database with an
> important number of data ...so i could not change database structure
> (column type and so on)
>
> I m'a sking if it's possible and so ...How to?
>
> make a mapping between
> a boolean from my java bean class and an integer in my database column
> associated?
>
> for example when in my database I have 1 it return true in my boolean
> and inverse.
>
> thanks a lot,
> Olivier
>
>
> ------------------------------
> *Yahoo! Photos*<http://us.rd.yahoo.com/mail/uk/taglines/default/photos/*http://uk.photos.yahoo.com/>–
> NEW, now offering a quality print service<http://us.rd.yahoo.com/mail/uk/taglines/default/photos/*http://uk.photos.yahoo.com/>from
just 8p a photo.
>
>
Mime
View raw message