Ibatis TypeHandlerCallback Enum Example

Friday, June 11th, 2010

Problem

You need Ibatis to perform custom processing before parameters are set on a Prepared Statement or after the results are retrieved from the result set. Maybe to convert Y/N to boolean, map results to custom objects etc

Solution

You probably want to look at implementing Ibatis’ TypeHandlerCallBack, the API docs for which are here

Example

In this example I map a “prev” or “next” coming back from the DB (as “direction”) to the Enum described in this post.

Ibatis Config

<parameterMap id="findByCompanyIdParams" class="map">
    <parameter mode="IN" property="companyId" javaType="java.lang.String" jdbcType="VARCHAR"/>
    <parameter mode="IN" property="nextKeyId" javaType="java.lang.String" jdbcType="VARCHAR"/>
    <parameter mode="IN" property="maxOccurs" javaType="java.lang.Integer" jdbcType="NUMBER"/>
    <parameter mode="IN" property="direction" javaType="OBJECT" 
        typeHandler="foo.bar.PagingCriteriaDirectionTypeHandlerCallback" jdbcType="VARCHAR"/>
    <parameter mode="OUT" property="outCursor" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet"/>
</parameterMap>

Java code

package foo.bar;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
import foo.bar.MyRuntimeException;

public class PagingCriteriaDirectionTypeHandlerCallback implements TypeHandlerCallback{
	
	public Object getResult(ResultGetter getter) throws SQLException {
		String s = getter.getString();
		if (PagingCriteriaDirection.NEXT.getDescription().equalsIgnoreCase(s)) {
			return PagingCriteriaDirection.NEXT;
		} else if (PagingCriteriaDirection.PREV.getDescription().equalsIgnoreCase(s)) {
			return PagingCriteriaDirection.PREV;
		} else {
			throw new SQLException("Unexpected value " + s + " found where "
					+ PagingCriteriaDirection.PREV.getDescription() + " or " + PagingCriteriaDirection.NEXT.getDescription() + " was expected.");
		}
	}


	public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
		PagingCriteriaDirection direction = (PagingCriteriaDirection) parameter;
		
		if (direction.equals(PagingCriteriaDirection.NEXT)) {
			setter.setString(PagingCriteriaDirection.NEXT.getDescription());
		}
		else {
			setter.setString(PagingCriteriaDirection.PREV.getDescription());
		}
	}
	
	public Object valueOf(String s) {
	    if (PagingCriteriaDirection.NEXT.getDescription().equalsIgnoreCase(s)) {
	        return PagingCriteriaDirection.NEXT;
	    } 
	    else if (PagingCriteriaDirection.PREV.getDescription().equalsIgnoreCase(s)) {
	        return PagingCriteriaDirection.PREV;
	    }
	    else {
	    	throw new MyRuntimeException("Unexpected value " + s + " found where "
					+ PagingCriteriaDirection.PREV.getDescription() + " or " + PagingCriteriaDirection.NEXT.getDescription() + " was expected.");
	    }
	}


}

You can leave a response, or trackback from your own site.

Tags: ,
Posted in: Examples



Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>