`
xxjkcyt
  • 浏览: 5710 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

springboot MangoTemplate保存自定义枚举类型值

 
阅读更多

/**

 * MongoDB配置

 */

@Configuration

public class MongoDBConfig

{

    private MappingMongoConverter converter;

 

    /*

     * 系统内部使用的类型转换器都在这里设置

     */

    @Autowired

    public void setMappingMongoConverter(MappingMongoConverter converter)

    {

        // 设置mongodb不保存 _class字段

        converter.setTypeMapper(new DefaultMongoTypeMapper(null));

        GenericConversionService conversionService = GenericConversionService.class.cast(

            converter.getConversionService());

 

        // 这里是设置自定义枚举类型转换器

        conversionService.addConverter(new Int2OrderStatusConverter());

        conversionService.addConverter(new OrderStatus2IntConverter());

        conversionService.addConverter(new Int2PayTypeConverter());

        conversionService.addConverter(new PayType2IntConverter());

        conversionService.addConverter(new Int2TradeTypeConverter());

        conversionService.addConverter(new TradeType2IntConverter());

 

        DirectFieldAccessor bw = (DirectFieldAccessor)PropertyAccessorFactory.forDirectFieldAccess(

            converter);

        CustomConversions conversions = (CustomConversions)bw.getPropertyValue(

            "conversions");

 

        // 这里是设置保存文档时,将Java Bean属性映射映射到文档字段的策略

        Map<Class<?>, CacheValue<Class<?>>> rawWriteTargetTypes = (Map<Class<?>, CacheValue<Class<?>>>)PropertyAccessorFactory.forDirectFieldAccess(

            conversions).getPropertyValue("rawWriteTargetTypes");

        rawWriteTargetTypes.put(OrderStatus.class,

            CacheValue.ofNullable(Integer.class));

        rawWriteTargetTypes.put(PayType.class,

            CacheValue.ofNullable(Integer.class));

        rawWriteTargetTypes.put(TradeType.class,

            CacheValue.ofNullable(Integer.class));

 

        // 这是是设置读取文档,并将文档字段转换为Java Bean属性的映射策略

        Map<ConvertiblePair, CacheValue<Class<?>>> customReadTargetTypes = (Map<ConvertiblePair, CacheValue<Class<?>>>)PropertyAccessorFactory.forDirectFieldAccess(

            conversions).getPropertyValue("customReadTargetTypes");;

        customReadTargetTypes.put(

            new ConvertiblePair(Integer.class, OrderStatus.class),

            CacheValue.ofNullable(OrderStatus.class));

        customReadTargetTypes.put(

            new ConvertiblePair(Integer.class, PayType.class),

            CacheValue.ofNullable(PayType.class));

        customReadTargetTypes.put(

            new ConvertiblePair(Integer.class, TradeType.class),

            CacheValue.ofNullable(TradeType.class));

    }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics