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 <[EMAIL PROTECTED]> wrote:
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 <[EMAIL PROTECTED]> 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:[EMAIL PROTECTED]
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 NEW, now offering a quality print service from just 8p a photo.