How to Return NULL-values in iBatis?

纵饮孤独 提交于 2019-12-20 05:19:26

问题


Let's say I have an Oracle database and an interface like this:

public interface DaoMapper {

  @Select({
      "SELECT col1, col2, col3",
        "FROM my_table" })
  List<Map<String, Object>> getUntyped();

}

If I call getUntyped() and all columns have a value the map contains three entries. However, if col2 is NULL, the map has only two entries. In many cases this isn't a problem, but in a generic part of our code I actually want to call .values() on that map and want a list consisting of three entries. Any entry may be null (or an empty string as that's the same in Oracle).

Actually, what I would be really happy about is something like this where each outer list consists of lists with three entries:

  @Select({
      "SELECT col1, col2, col3",
        "FROM my_table" })
  List<List<Object>> getUntypedList();

However, iBatis tells me that this is an unsupported operation.

Therefore, I'm here to ask how I can tell iBatis to include columns that are NULL or an empty string.


回答1:


there is a configuration property callSettersOnNulls that defaults to false (more information here); you need to set it to true, but it affects all the statements not only the one you need. Also note that different mybatis versions may exhibit some bugs in the area as you can check here; so you must check the mybatis version you are using




回答2:


Thanks to Giovanni's answer I noticed the example for type handlers and went from there:

public class EmptyStringTypeHandler extends StringTypeHandler {

  @Override
  public String getResult(ResultSet rs, String columnName) throws SQLException {
    return unnulledString(super.getResult(rs, columnName));
  }

  @Override
  public String getResult(ResultSet rs, int columnIndex) throws SQLException {
    return unnulledString(super.getResult(rs, columnIndex));
  }

  @Override
  public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
    return unnulledString(super.getResult(cs, columnIndex));
  }

  private String unnulledString(String value) {
    return StringUtils.defaultString(value, "");
  }

}

The interface is now:

public interface DaoMapper {

  @Select({
      "SELECT col1, col2, col3",
        "FROM my_table" })
  @Results(value = {
      @Result(column = "col1", property = "col1", typeHandler = EmptyStringTypeHandler.class),
      @Result(column = "col2", property = "col2", typeHandler = EmptyStringTypeHandler.class),
      @Result(column = "col3", property = "col3", typeHandler = EmptyStringTypeHandler.class)
  })
  List<LinkedHashMap<String, ?>> getUntyped();

}

I should add that the big advantage is that I can specify this per column per statement. For more generic use it would be better to specify this per statement. Maybe in some future version?



来源:https://stackoverflow.com/questions/31160046/how-to-return-null-values-in-ibatis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!