ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ingmar Lötzsch <iloetz...@asci-systemhaus.de>
Subject Re: how to pass two different types of values to query in IBATIS
Date Wed, 14 Apr 2010 12:53:44 GMT
I prefer the use of java.sql.Array and a custom TypeHandler and to keep the sql as simple as
possible.
Here is an example for Ibatis 2.

Create an IntArrayTypeHandler class and register it in the config.

public class IntArrayTypeHandler
implements TypeHandlerCallback
{
    public void setParameter(ParameterSetter setter, Object parameter)
    throws SQLException
    {
        Collection<Integer> keys = (Collection<Integer>) parameter;
        IntArray intArray = new IntArray(keys);
        setter.setArray(intArray);
    }

    public Object getResult(ResultGetter getter)
    throws SQLException
    {
        Array array = getter.getArray();
        return array;
    }

    public Object valueOf(String string)
    {
        return string;
    }
}

<typeAlias alias="IntArrayTypeHandler"
type="com.asci.common.ibatis.IntArrayTypeHandler" />

I omit the implementation of the IntArray class.

Now you can handle Set<Integer> as a simple parameter. Collection<Integer> would
fit too.

<update id="insertSelect" parameterClass="map">
	INSERT ...
	SELECT ...
	WHERE id = ANY (#ids,handler=IntArrayTypeHandler#)
</update>

public int insertSelect(Set<Integer> ids)
{
	Map<String, Object> params = new HashMap<String, Object>();
	params.put("ids", ids);
	int rows = getSqlMapClientTemplate().update("insertSelect", params);
	return rows;
}

Martin Ellis schrieb:
> 2010/4/12 François Schiettecatte <fschiettecatte@gmail.com>:
>   
>> I think Mukhi is trying to pass a string and an array of numbers, the string being
the entryRefno and the list of nunbers to be added to the IN() clause of the SELECT statement
in the example given.
>>
>> The Map is the way to go, you just need to add the string as shows in the code sample
below. The array of numbers is more complicated because I dont think there is way to iterate
over an array in the map xml (correct me if I am wrong here). The way I would handle this
is to turn the array into a comma separated string of numbers and pass that, so in the example
below I would turn the array (90,89,93,45,67) into a string "90,89,93,45,67" and pass that.
Ugly but workable.
>>     
>
>
> The 'foreach' example in the User Guide shows how to iterate over a
> collection to build an 'IN' clause from a collection.  There's no need
> to build a string.
>
> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Mime
View raw message