How to Return NULL-values in iBatis?

大兔子大兔子 提交于 2019-12-02 05:41:07

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

sjngm

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?

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