Does MyBatis support specifying type handlers with string substitution ${} instead of prepared statement substitution #{}?
I am trying to populate an order by clause with an enum value so I am using a TypeHandler for this but I can't get it to work.
EnumTypeHandler
public class EnumTypeHandler implements TypeHandler<MyEnum> {
@Override
public void setParameter(PreparedStatement ps, int i, MyEnum parameter,
JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter.getValue());
}
@Override
public MyEnum getResult(ResultSet rs, String columnName) throws SQLException {
// Not implemented
return null;
}
@Override
public MyEnum getResult(ResultSet rs, int columnIndex) throws SQLException {
// Not implemented
return null;
}
@Override
public MyEnum getResult(CallableStatement cs, int columnIndex) throws SQLException {
// Not implemented
return null;
}
}
MyBatis XML
order by ${searchCriteria.sortBy, typeHandler=com.example.EnumTypeHandler}
Error
org.apache.ibatis.binding.BindingException: Parameter 'com' not found. Available parameters are [offset, searchCriteria, limit, param3, param1, param2]
PreparedStatement. If you use text substitution i.e.${}, it's not a parameter of thePreparedStatementanymore, so type handler is of no use. As you can use OGNL expression in${}, you can invoke the method likeorder by ${searchCriteria.sortBy.getValue()}ororder by ${searchCriteria.sortBy.value}.