具有MyBatis TypeHandler的空对象模式 [英] Null Object pattern with a MyBatis TypeHandler

查看:179
本文介绍了具有MyBatis TypeHandler的空对象模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在MyBatis中创建自定义TypeHandler,以便对于数据库中的null列,MyBatis返回

I have been trying to make a custom TypeHandler in MyBatis so that for a null column in the database, MyBatis returns an implementation of a Null Object pattern instead of having a null in the domain class.

搜索后,我到达了一个出色的项目 mybatis-koans ,即

After googling for help, I reached the excelent project mybatis-koans, namely the koan 19 that addresses exactly this issue with the same approach I am using, i.e., extending BaseTypeHandler<T> (is abstract). At this point, I have a concrete TypeHandler similar to the EmailTypeHandler in that koan:

/**
 * Acts as a factory method to return the appropriate implementation of an Email.
 * Returns a Null object if the email value in the database was null/empty
 */
public class EmailTypeHandler extends BaseTypeHandler<Email> {

  @Override
  public Email getNullableResult(ResultSet rs, String colName) throws SQLException {
    return createEmail(rs.getString(colName));
  }

  @Override
  public Email getNullableResult(ResultSet rs, int colNum) throws SQLException {
    return createEmail(rs.getString(colNum));
  }


  private Email createEmail(String s) {
    System.out.println(s);
    if (s == null || s.equals("")) {
      return new NullEmail();
    } else {
      return new EmailImpl(s);
    }
  }

  @Override
  public Email getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
    return null;
  }

  @Override
  public void setNonNullParameter(PreparedStatement ps, int colNum,
                                  Email e, JdbcType t) throws SQLException {
  }
}

不幸的是,似乎作者( midpeter444 )面临着同样的问题:当数据库中的值是null时,仍然返回null而不是具体构造的对象TypeHandler.

Unfortunately, it seems that the author (midpeter444) is facing the same problem: when the value in the DB is null, null is still returned instead of the object crafted in the concrete TypeHandler.

推荐答案

发布问题后,我立即看到了解决方案.在 BaseTypeHandler 代码:

I saw the solution right after posting the question. In BaseTypeHandler code:

[...]
  public T getResult(ResultSet rs, String columnName) throws SQLException {
    T result = getNullableResult(rs, columnName);
    if (rs.wasNull()) {
      return null;
    } else {
      return result;
    }
  }

  public T getResult(ResultSet rs, int columnIndex) throws SQLException {
    T result = getNullableResult(rs, columnIndex);
    if (rs.wasNull()) {
      return null;
    } else {
      return result;
    }
  }

  public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
    T result = getNullableResult(cs, columnIndex);
    if (cs.wasNull()) {
      return null;
    } else {
      return result;
    }
  }

  public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;

  public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;

  public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;

  public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
[...]

很明显,当数据库中的值是null时,BaseTypeHandler将返回null,因为在这种情况下,cs.WasNull()true.因此,解决方案是创建一个具体的 TypeHandler<T> 返回适当的实现(在这种情况下,当数据库中的值为null时为NullObject实现),而没有子类BaseTypeHandler.

Clearly, BaseTypeHandler is going to return null when the value in the database is null, as cs.WasNull() is true in that case. Therefore, the solution is to create a concrete TypeHandler<T> returning the appropiate implementation (in this case, a NullObject implmentation when the value in the database is null) without subclassing BaseTypeHandler.

已编辑(遵循 quux00 的注释):当然,只要其方法提供的功能,我们就可以覆盖BaseTypeHandlergetResult方法. setParameters可能有用.

EDITED (folowing quux00's comment): Of course, we can just override getResult method of BaseTypeHandler, given that the functionality provided by its method setParameters can be useful.

这篇关于具有MyBatis TypeHandler的空对象模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆