Java 如何在iBatis中返回空值?

Java 如何在iBatis中返回空值?,java,ibatis,Java,Ibatis,假设我有一个Oracle数据库和如下界面: public interface DaoMapper { @Select({ "SELECT col1, col2, col3", "FROM my_table" }) List<Map<String, Object>> getUntyped(); } 但是,iBatis告诉我这是一个不受支持的操作 因此,我在这里询问如何告诉iBatis包含NULL或空字符串的列。有一个默认为false

假设我有一个Oracle数据库和如下界面:

public interface DaoMapper {

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

}
但是,iBatis告诉我这是一个不受支持的操作


因此,我在这里询问如何告诉iBatis包含
NULL
或空字符串的列。

有一个默认为false的配置属性
callSettersOnNulls
(详细信息);您需要将其设置为true,但它会影响所有语句,而不仅仅是您需要的语句。另外请注意,不同的mybatis版本可能会在您可以检查的区域显示一些bug;因此,您必须检查正在使用的mybatis版本

有一个默认为false的配置属性
callSettersOnNulls
(详细信息);您需要将其设置为true,但它会影响所有语句,而不仅仅是您需要的语句。另外请注意,不同的mybatis版本可能会在您可以检查的区域显示一些bug;因此,您必须检查您正在使用的mybatis版本,这要感谢我注意到并从那里开始:

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, "");
  }

}
界面现在是:

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();

}
公共接口DaoMapper{
@挑选({
“选择列1、列2、列3”,
“从我的_表”})
@结果(值={
@结果(column=“col1”,property=“col1”,typeHandler=EmptyStringTypeHandler.class),
@结果(column=“col2”,property=“col2”,typeHandler=EmptyStringTypeHandler.class),
@结果(column=“col3”,property=“col3”,typeHandler=EmptyStringTypeHandler.class)
})
List getUntyped();
}
我应该补充一点,最大的优点是我可以在每条语句的每列中指定它。对于更通用的用法,最好指定此per语句。也许在未来的版本中?

多亏了我注意到了,然后从那里开始:

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, "");
  }

}
界面现在是:

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();

}
公共接口DaoMapper{
@挑选({
“选择列1、列2、列3”,
“从我的_表”})
@结果(值={
@结果(column=“col1”,property=“col1”,typeHandler=EmptyStringTypeHandler.class),
@结果(column=“col2”,property=“col2”,typeHandler=EmptyStringTypeHandler.class),
@结果(column=“col3”,property=“col3”,typeHandler=EmptyStringTypeHandler.class)
})
List getUntyped();
}

我应该补充一点,最大的优点是我可以在每条语句的每列中指定它。对于更通用的用法,最好指定此per语句。也许在将来的版本中?

您能告诉我如何设置此属性吗?它将返回空值吗?int的默认值呢?你能告诉我如何设置这个属性吗?它将返回空值吗?int的默认值呢?