TypeHandler
MyBatis でカスタムを作成して、データベースの列に対して、MyBatis がドメイン クラスに含まれる代わりにNull オブジェクト パターンnull
の実装を返すようにしようとしています。null
グーグルで助けを求めた後、私は優れたプロジェクトmybatis-koans、つまり、私が使用しているのと同じアプローチ、つまり拡張(抽象的)でこの問題に正確に対処するkoan 19に到達しました。BaseTypeHandler<T>
この時点で、その koan にTypeHandler
似た具象があります。EmailTypeHandler
/**
* 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 ) も同じ問題に直面しているようです。DB の値が の場合null
、具体的なオブジェクトではなく null が返されますTypeHandler
。