Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iBatis mapping: map a string field into a List<String>

is it possible to map a string field with a particular format like:

aaa,bbb,ccc,ddd

into a List having elements [aaa, bbb, ccc, ddd] using iBatis?

What I need is to have in my model something like:

public class Request{
    private List<String> fieldOne;
    public List<String> getFieldOne(){....}
    public void setFieldOne(){....}
}

even if in my table the field is a simple string. Is this possible?

Thanks Roberto

like image 390
Roberto Avatar asked Apr 22 '10 09:04

Roberto


1 Answers

You can do it through a CustomType Handler:

For example, in your mapping you define:

<result column="FIELD_ONE" property="fieldOne" 
        jdbcType="VARCHAR" typeHandler="com.xxx.StringSplitTypeHandlerCallBack" />

and then you code your class StringSplitTypeHandlerCallBack implements TypeHandlerCallback , which would call String.split() inside the getResult() method.

UPDATE: Of course, if this conversion is only need for one field of one class, it might be more simple to define a pair of alternative setter/getters getFieldOneAsString(), setFieldOneAsString() in your class, and use property="fieldOneAsString" in your mapping

like image 91
leonbloy Avatar answered Sep 23 '22 19:09

leonbloy