class Student{ int id; int age; List<Course> courseList; static class Course{ String name; String code; ... }}
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.
class ListHandler<T> extends BaseTypeHandler<List<T>> {
private Class<T> clazz
private TypeReference<List<T>> typeRef
ListTypeHandler(Class<T> clazz) {
this.clazz = clazz
this.typeRef = new TypeReference<List<T>>() {}
}
@Override
void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType) throws SQLException {
String json = JsonUtil.toString(parameter)
ps.setString(i, json)
}
@Override
List<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
return parseArray(rs.getString(columnName))
}
@Override
List<T> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return parseArray(rs.getString(columnIndex))
}
@Override
List<T> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return parseArray(cs.getString(columnIndex))
}
private List<T> parseArray(String jsonStr) {
return JsonUtil.parseArray(jsonStr, typeRef)
}
}
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.
Hi David,
I’m not sure how your JsonUtil
works, but to receive Class
in a type handler constructor, you may need to specify javaType
in the result map.
<resultMap id="studentRM" type="pkg.Student">
<id column="id" property="id" />
<result column="age" property="age" />
<result column="courseList" property="courseList"
javaType="pkg.Course" typeHandler="pkg.ListHandler" />
Regards,
Iwao
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTYBTeOCLCbeNHz68EL3DPAh_FERiyD84dsdY8%2B3c2an%3Dg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2QcF8eL4F9u24HWsanek5TxDiAJRJEF3NbOobAf6oq_-A%40mail.gmail.com.
--
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2QcF8eL4F9u24HWsanek5TxDiAJRJEF3NbOobAf6oq_-A%40mail.gmail.com.
Just be sure to use $
when referencing inner classes in MyBatis.
e.g. javaType="pkg.Student$Course"
the generic Type T can’t be inferred as Course, have you tried it?
That is not a MyBatis issue anymore. :)
If you need further assistance, please show us the JsonUtil
implementation (or a link to it).Class
seems to be enough, but if JsonUtil
requires TypeReference
(whatever that is), you are supposed to create a subclass of ListHandler
(i.e. extends ListHandler<Cource>
), probably.
Regards,
Iwao