Bootstrap

MybatisPlus自定义TypeHandler

1. 自定义TypeHandler

下面是一个自定义的用于处理Set<Long>类型的类型处理器
用于处理:Java实体类属性Set<Long> [1, 2, 3, 4] <–> 数据库表字段varchar(255) 1,2,3,4

public class SetTypeHandler extends BaseTypeHandler<Set<Long>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Set<Long> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, convertSetToString(parameter));
    }

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

    @Override
    public Set<Long> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return convertStringToSet(rs.getString(columnIndex));
    }

    @Override
    public Set<Long> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return convertStringToSet(cs.getString(columnIndex));
    }

    private String convertSetToString(Set<Long> longSet) {
        if (longSet == null || longSet.isEmpty())
            return null;
        StringBuilder sb = new StringBuilder();
        for (Long item : longSet) {
            sb.append(item.toString()).append(",");
        }
        System.out.println(sb.deleteCharAt(sb.length() - 1).toString());
        return sb.deleteCharAt(sb.length() - 1).toString(); // Remove trailing comma
    }

    private Set<Long> convertStringToSet(String value) {
        Set<Long> resultSet = new HashSet<>();
        if (value != null && !value.isEmpty()) {
            String[] tokens = value.split(",");
            for (String token : tokens) {
                resultSet.add(Long.parseLong(token));
            }
        }
        return resultSet;
    }
}

2. 在配置类注册自定义的类型处理器

@Bean
public ConfigurationCustomizer mybatisConfigurationCustomizer() {
    return new ConfigurationCustomizer() {
        @Override
        public void customize(MybatisConfiguration configuration) {
            configuration.getTypeHandlerRegistry().register(Set.class, SetTypeHandler.class);
        }
    };
}
@Slf4j
@Configuration
public class MybatisConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        // 分页
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setMaxLimit(1000L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(MybatisConfiguration configuration) {
                configuration.getTypeHandlerRegistry().register(Set.class, SetTypeHandler.class);
            }
        };
    }
}

3. 使用方法

3.1 PO

@TableField(typeHandler = SetTypeHandler.class)
@ApiModelProperty(value = "标签列表")
@TableField(typeHandler = SetTypeHandler.class)
private Set<Long> labels;

3.2 Mapper XML

#{labels, typeHandler=com.lichun.handler.SetTypeHandler, mode=IN, jdbcType=VARCHAR})}
<select id="queryProfileList" resultType="com.lichun.domain.vo.HelpPostProfileVO" statementType="CALLABLE">
    {call query_help_post_profile_list(
            #{deliveryCycleFloor, mode=IN, jdbcType=INTEGER},
            #{deliveryCycleCeiling, mode=IN, jdbcType=INTEGER},
            #{rewardAmountFloor, mode=IN, jdbcType=DOUBLE},
            #{rewardAmountCeiling, mode=IN, jdbcType=DOUBLE},
            #{createTimeFloor, mode=IN, jdbcType=DATE},
            #{createTimeCeiling, mode=IN, jdbcType=DATE},
            #{labels, typeHandler=com.lichun.handler.SetTypeHandler, mode=IN, jdbcType=VARCHAR})}
</select>
;