Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

java - How to Enforce MyBatis PSQL custom TypeHandler on property level

I am using a DTO which contains Set<UUID> and Set<String>.

    public class MyBatisDTO implements Serializable{
    
    // other attributes
    
        private Set<UUID> uuidSet;
    
        private Set<String> stringSet;
    
      .....
}

A typeHandler has already been registered for Generic-Type Set<T>.

@MappedJdbcTypes(JdbcType.OTHER)
@MappedTypes(Set.class)
public class SetTypeHandler extends BaseTypeHandler<Set<T>> {


    @Override
    public Set<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return mapFromJson(rs.getString(columnName));
    }
 .......

Now the problem is that when above mentioned MyBatisDTO.java is being mapped from DB values where both columns uuidSet and stringset are stored as jsonb values, typeHandler picks the Generic type as String -> T extends String for Set<UUID> uuidSet; as well which is leading to auto-type conversion to all items of uuidSet as String. This is not an error but in debugging it can be seen that uuidSet contains String values. Later on, which also leads in de-serialization of JAX-RS response but thats another topic which is not important at the moment.
My question is Is there a way that we can enforce a typeHandler on property's level? so I am looking for a solution in which I may attach custom TypeHandler on property level, something like this

//SUDO_CODE

    public class MyBatisDTO implements Serializable{
            
            // other attributes
            
                @TypeHandler ("com.mybatis.handlers.typeHandler.SetUUIDTypeHandler")
                private Set<UUID> uuidSet;
            
                @TypeHandler ("com.mybatis.handlers.typeHandler.SetStringTypeHandler")
                private Set<String> stringSet;
    .....
    }

OR if there is a way to mention these typeHandlers on Mapper-Level, something like this?

@Mapper
public interface MyBatisMapper {

 
//TYPE HANDLER ATTACHMENT/CONFIGURATION HERE ..???
    @Select("SELECT uuidSet, stringSet FROM MyBatisDTO WHERE param = #{param}")
    Cursor<MyBatisDTO> getData(@Param("param") final UUID param);
.....
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it with a result map:

@Mapper
public interface MyBatisMapper {
    @Select("SELECT uuidSet, stringSet FROM MyBatisDTO WHERE param = #{param}")
    @Results({
      @Result(column = "uuidSet", property="uuidSet", typeHandler = "com.mybatis.handlers.typeHandler.SetUUIDTypeHandler"),
      @Result(column = "stringSet", property="stringSet", typeHandler = "com.mybatis.handlers.typeHandler.SetStringTypeHandler")
    })
    Cursor<MyBatisDTO> getData(@Param("param") final UUID param);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...