IBatis mapping: string field mapping in List <String>

Is it possible to map a string field to a specific format, for example:

aaa, bbb, sss, ddd

to a list containing the elements [aaa, bbb, ccc, ddd] using iBatis?

I need something like in my model:

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

even if in my table the field is a simple row. Is it possible?

Thanks Roberto

+4
source share
3 answers

You can do this with the CustomType Handler :

For example, in your mapping, you define:

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

and then enter the class StringSplitTypeHandlerCallBack implements TypeHandlerCallback code class StringSplitTypeHandlerCallBack implements TypeHandlerCallback , which will call String.split() inside the getResult() method.

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

+5
source

Use TypeHandler, Example:

 <result property="strList" column="rp_str" typeHandler="com.yourproject.repository.orm.StringSplitTypeHandler" /> 

 public class StringSplitTypeHandler implements TypeHandler<List<String>> { @Override public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException { if (parameter != null) { ps.setString(i, parameter.toString()); } } @Override public List<String> getResult(ResultSet rs, String columnName) throws SQLException { String columnValueStr = rs.getString(columnName); if (columnValueStr != null) { return Arrays.asList(columnValueStr.replaceAll("[ ]", "").split(",")); } return null; } @Override public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException { String columnValueStr = rs.getString(columnIndex); if (columnValueStr != null) { return Arrays.asList(columnValueStr.replaceAll("[ ]", "").split(",")); } return null; } @Override public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException { String columnValueStr = cs.getString(columnIndex); if (columnValueStr != null) { return Arrays.asList(columnValueStr.replaceAll("[ ]", "").split(",")); } return null; } } 
+2
source

I'm not sure why you want iBatis to do this, but you could just use String.split () to do the work, and display the results.

0
source

Source: https://habr.com/ru/post/1307643/


All Articles