Typehandler for a List of a user-defined class

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Typehandler for a List of a user-defined class

david Lu
I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Typehandler for a List of a user-defined class

Guy Rouillier-2
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.
Reply | Threaded
Open this post in threaded view
|

Re: Typehandler for a List of a user-defined class

david Lu
courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re[2]: Typehandler for a List of a user-defined class

Guy Rouillier-2
Hmm, that seems like a strange choice for storing the Course data.  Course data contains simple native data types - two string values.  I would think you'd want to normalize this and store in a separate relation.  The Student data is stored in a regular table with separate columns for ID and AGE, but then the courseList is stored as JSON in a single COURSELIST column in JSON format?But perhaps the database was defined by someone else and you have no say in it?

Regardless, sure, you can define a TypeHandler.  The value coming from the database would be a JSON string, and you would translate into Course objects.  Going into the database, you'd start with Course objects, and translate them into a JSON string.

--
Guy Rouillier

On 5/15/2019 4:52:41 AM, "david Lu" <[hidden email]> wrote:

courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Typehandler for a List of a user-defined class

david Lu
I use student-course just for better explanation, there is no need to create a table for "course" in the project. 

The thing is how to define such a Typehandler? I defined a ListHandler as below, but it can't get the class "Course" and use LinkedMap for deserialization/serialization. 


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)
}
}

On Thu, May 16, 2019 at 2:36 PM Guy Rouillier <[hidden email]> wrote:
Hmm, that seems like a strange choice for storing the Course data.  Course data contains simple native data types - two string values.  I would think you'd want to normalize this and store in a separate relation.  The Student data is stored in a regular table with separate columns for ID and AGE, but then the courseList is stored as JSON in a single COURSELIST column in JSON format?But perhaps the database was defined by someone else and you have no say in it?

Regardless, sure, you can define a TypeHandler.  The value coming from the database would be a JSON string, and you would translate into Course objects.  Going into the database, you'd start with Course objects, and translate them into a JSON string.

--
Guy Rouillier

On 5/15/2019 4:52:41 AM, "david Lu" <[hidden email]> wrote:

courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTYBTeOCLCbeNHz68EL3DPAh_FERiyD84dsdY8%2B3c2an%3Dg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Typehandler for a List of a user-defined class

Iwao AVE!

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


On Thu, May 16, 2019 at 5:33 PM david Lu <[hidden email]> wrote:
I use student-course just for better explanation, there is no need to create a table for "course" in the project. 

The thing is how to define such a Typehandler? I defined a ListHandler as below, but it can't get the class "Course" and use LinkedMap for deserialization/serialization. 


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)
}
}

On Thu, May 16, 2019 at 2:36 PM Guy Rouillier <[hidden email]> wrote:
Hmm, that seems like a strange choice for storing the Course data.  Course data contains simple native data types - two string values.  I would think you'd want to normalize this and store in a separate relation.  The Student data is stored in a regular table with separate columns for ID and AGE, but then the courseList is stored as JSON in a single COURSELIST column in JSON format?But perhaps the database was defined by someone else and you have no say in it?

Regardless, sure, you can define a TypeHandler.  The value coming from the database would be a JSON string, and you would translate into Course objects.  Going into the database, you'd start with Course objects, and translate them into a JSON string.

--
Guy Rouillier

On 5/15/2019 4:52:41 AM, "david Lu" <[hidden email]> wrote:

courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTYBTeOCLCbeNHz68EL3DPAh_FERiyD84dsdY8%2B3c2an%3Dg%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2QcF8eL4F9u24HWsanek5TxDiAJRJEF3NbOobAf6oq_-A%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Typehandler for a List of a user-defined class

david Lu
wow, this may be the solution. But since Course is a static inner class, the compilation failed saying that it can't find the class, so it means mybatis is not able to deal with static inner class?

also, how about the JDBCType?

On Mon, May 20, 2019 at 12:48 AM Iwao AVE! <[hidden email]> wrote:

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


On Thu, May 16, 2019 at 5:33 PM david Lu <[hidden email]> wrote:
I use student-course just for better explanation, there is no need to create a table for "course" in the project. 

The thing is how to define such a Typehandler? I defined a ListHandler as below, but it can't get the class "Course" and use LinkedMap for deserialization/serialization. 


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)
}
}

On Thu, May 16, 2019 at 2:36 PM Guy Rouillier <[hidden email]> wrote:
Hmm, that seems like a strange choice for storing the Course data.  Course data contains simple native data types - two string values.  I would think you'd want to normalize this and store in a separate relation.  The Student data is stored in a regular table with separate columns for ID and AGE, but then the courseList is stored as JSON in a single COURSELIST column in JSON format?But perhaps the database was defined by someone else and you have no say in it?

Regardless, sure, you can define a TypeHandler.  The value coming from the database would be a JSON string, and you would translate into Course objects.  Going into the database, you'd start with Course objects, and translate them into a JSON string.

--
Guy Rouillier

On 5/15/2019 4:52:41 AM, "david Lu" <[hidden email]> wrote:

courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTYBTeOCLCbeNHz68EL3DPAh_FERiyD84dsdY8%2B3c2an%3Dg%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2QcF8eL4F9u24HWsanek5TxDiAJRJEF3NbOobAf6oq_-A%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTb%3DG24jifEQtwqVJGL_Ww9okoJ%3D1dLjg07GgHdOi6pB7Q%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Typehandler for a List of a user-defined class

Guy Rouillier-2
No, it means the syntax used to reference the static nested class is incorrect:


The JDBCType I'm assuming is just VARCHAR, but you haven't shown us your table definition.

--
Guy Rouillier

On Mon, 2019-05-20 at 12:33 +0800, david Lu wrote:
wow, this may be the solution. But since Course is a static inner class, the compilation failed saying that it can't find the class, so it means mybatis is not able to deal with static inner class?

also, how about the JDBCType?

On Mon, May 20, 2019 at 12:48 AM Iwao AVE! <[hidden email]> wrote:

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


On Thu, May 16, 2019 at 5:33 PM david Lu <[hidden email]> wrote:
I use student-course just for better explanation, there is no need to create a table for "course" in the project. 

The thing is how to define such a Typehandler? I defined a ListHandler as below, but it can't get the class "Course" and use LinkedMap for deserialization/serialization. 


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
)

    }

}


On Thu, May 16, 2019 at 2:36 PM Guy Rouillier <[hidden email]> wrote:
Hmm, that seems like a strange choice for storing the Course data.  Course data contains simple native data types - two string values.  I would think you'd want to normalize this and store in a separate relation.  The Student data is stored in a regular table with separate columns for ID and AGE, but then the courseList is stored as JSON in a single COURSELIST column in JSON format?But perhaps the database was defined by someone else and you have no say in it?

Regardless, sure, you can define a TypeHandler.  The value coming from the database would be a JSON string, and you would translate into Course objects.  Going into the database, you'd start with Course objects, and translate them into a JSON string.

--
Guy Rouillier

On 5/15/2019 4:52:41 AM, "david Lu" <[hidden email]> wrote:

courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTYBTeOCLCbeNHz68EL3DPAh_FERiyD84dsdY8%2B3c2an%3Dg%40mail.gmail.com.


--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/e83b8b2067c30e0ef43130d513d961227a897172.camel%40gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Typehandler for a List of a user-defined class

david Lu
In reply to this post by Iwao AVE!
I tried by adding javaType, but it doesn't work coz the generic Type T can't be inferred as Course, have you tried it? 

On Mon, May 20, 2019 at 12:48 AM Iwao AVE! <[hidden email]> wrote:

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


On Thu, May 16, 2019 at 5:33 PM david Lu <[hidden email]> wrote:
I use student-course just for better explanation, there is no need to create a table for "course" in the project. 

The thing is how to define such a Typehandler? I defined a ListHandler as below, but it can't get the class "Course" and use LinkedMap for deserialization/serialization. 


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)
}
}

On Thu, May 16, 2019 at 2:36 PM Guy Rouillier <[hidden email]> wrote:
Hmm, that seems like a strange choice for storing the Course data.  Course data contains simple native data types - two string values.  I would think you'd want to normalize this and store in a separate relation.  The Student data is stored in a regular table with separate columns for ID and AGE, but then the courseList is stored as JSON in a single COURSELIST column in JSON format?But perhaps the database was defined by someone else and you have no say in it?

Regardless, sure, you can define a TypeHandler.  The value coming from the database would be a JSON string, and you would translate into Course objects.  Going into the database, you'd start with Course objects, and translate them into a JSON string.

--
Guy Rouillier

On 5/15/2019 4:52:41 AM, "david Lu" <[hidden email]> wrote:

courseList is stored as a json in t_student table, how can typehandler deal with it?

On Wed, May 15, 2019 at 10:55 AM Guy Rouillier <[hidden email]> wrote:
All your properties are native types.  You do not need a TypeHandler for native types, as MyBatis already handles them automatically.  And MyBatis also handles List automatically.

What makes you believe you need a TypeHandler?

--
Guy Rouillier

On 5/14/2019 10:50:01 AM, "david Lu" <[hidden email]> wrote:

I have a entity class like :

    
class Student{
    int id;
    int age;
    List<Course> courseList;
    
    
    static class Course{
        String name;
        String code;
        ...
    }
}

I want to make a type handler for courseList, how can I do? Thanks!

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/acafb759-0aae-402d-8cea-2e5a3bab7b4c%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd50be574-a0ee-4069-a0be-3b6324d3fff5%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTa1oTNLUQNMykrL14z10%2BudwZtGuYejR%3D9ZRegNdCCVzw%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/em0710f3ca-4827-4708-96f7-08d1874f20da%40asus.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTYBTeOCLCbeNHz68EL3DPAh_FERiyD84dsdY8%2B3c2an%3Dg%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2QcF8eL4F9u24HWsanek5TxDiAJRJEF3NbOobAf6oq_-A%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAC82OTb0oap3EbAfv1CZVutAQxiY5rS9zXNjL%2BUtF%3DyN3hNQ_w%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Typehandler for a List of a user-defined class

Iwao AVE!

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

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2T4WC0Gc%2BeT_4xJHM5rYa1w2sOeeqb-dte2Pe2HVNABJw%40mail.gmail.com.