How to use UUID type handler with @Many annotation in MyBatis?

大兔子大兔子 提交于 2021-01-28 01:12:59

问题


I`m using mybatis-spring-boot-starter with 2.1.0 version. And I need to process UUID type to get a nested collection.

 @Select("SELECT id, name FROM t_service s")
    @Results(value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.OTHER, typeHandler = UuidTypeHandler.class),
            @Result(column = "name", property = "name"),
            @Result(property = "rates", column = "id", javaType = List.class, many = @Many(select = "getAllRates"))
    })
    List<Service> findAll();

 @Select("SELECT date_from, date_to, currency FROM t_rate where t_rate.service_id = #{serviceId, javaType=java.util.UUID, jdbcType=OTHER, typeHandler=my.package.UuidTypeHandler}")
    @Results(value = {
            @Result(property = "dateFrom", column = "date_from"),
            @Result(property = "dateTo", column = "date_to"),
            @Result(property = "currency", column = "currency")
 })
    List<Rate> getAllRates(@Param("serviceId") UUID serviceId);

UuidTypeHandler:

@MappedJdbcTypes(JdbcType.OTHER)
@MappedTypes(UUID.class)
public class UuidTypeHandler extends BaseTypeHandler<UUID> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException {
        ps.setObject(i, parameter, jdbcType.TYPE_CODE);
    }

    @Override
    public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return rs.getObject(columnName, UUID.class);
    }

    @Override
    public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return rs.getObject(columnIndex, UUID.class);
    }

    @Override
    public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getObject(columnIndex, UUID.class);
    }
}

But I get following exception:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'serviceId' in 'class java.util.UUID'

In other situations (when call methods directly) UuidTypeHandler works correctly and I don`t have any problems

Workaround for PostgreSQL:

 @Select("SELECT date_from, date_to, currency FROM t_rate where t_rate.service_id = #{serviceId}::uuid")
    @Results(value = {
            @Result(property = "dateFrom", column = "date_from"),
            @Result(property = "dateTo", column = "date_to"),
            @Result(property = "currency", column = "currency")
 })
    List<Rate> getAllRates(@Param("serviceId") String serviceId);

回答1:


To make it work, you need to register the type handler globally in the config (it's a kind of limitation).
In your case, adding mybatis.type-handlers-package=my.package to application.properties would be sufficient.

Once the type handler is registered globally, you can omit typeHandler in your mappers in most cases.

@Select("SELECT id, name FROM t_service s")
@Results(value = {
  @Result(column = "id", property = "id"),
  @Result(column = "name", property = "name"),
  @Result(property = "rates", column = "id",
    javaType = List.class, many = @Many(select = "getAllRates"))
})
List<Service> findAll();

@Select("SELECT date_from, date_to, currency FROM t_rate where t_rate.service_id = #{serviceId}")
@Results(value = {
  @Result(property = "dateFrom", column = "date_from"),
  @Result(property = "dateTo", column = "date_to"),
  @Result(property = "currency", column = "currency")
})
List<Rate> getAllRates(@Param("serviceId") UUID serviceId);

The type handler also can be a little bit simpler.

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

// no @MappedJdbcTypes
@MappedTypes(UUID.class)
public class UuidTypeHandler extends BaseTypeHandler<UUID> {
  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException {
    ps.setObject(i, parameter); // no 3rd arg
  }

  @Override
  public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return rs.getObject(columnName, UUID.class);
  }

  @Override
  public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    return rs.getObject(columnIndex, UUID.class);
  }

  @Override
  public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    return cs.getObject(columnIndex, UUID.class);
  }
}


来源:https://stackoverflow.com/questions/60613619/how-to-use-uuid-type-handler-with-many-annotation-in-mybatis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!