zoukankan      html  css  js  c++  java
  • [Spring Data MongoDB]学习笔记--_id和类型映射

    _id字段的映射:

    MongoDB要求所有的document都要有一个_id的字段。

    如果我们在使用中没有传入_id字段,它会自己创建一个ObjectId.

    { "_id" : ObjectId("53e0ff0b0364cb4a98ce3bfd"), "_class" : "org.springframework.data.mongodb.examples.hello.domain.Person", "name" : "John", "age" : 39, "accounts" : [ { "_id" : null, "accountNumber" : "1234-59873-893-1", "accountType" : "SAVINGS", "balance" : 123.45 } ] }

    MongoMappingConverter按照下面的规则将java class对应到_id字段:

    1. 被标记为@Id (org.springframework.data.annotation.Id) 的字段或者属性。

    2. 没有标记,但是名字是id的字段或者属性(类型为string或者BigInteger)。

    类型映射

    如果一个object中包含另一个object,默认存放在_class字段下,例如

    public class Sample {
      Contact value;
    }
    
    public abstract class Contact { … }
    
    public class Person extends Contact { … }
    
    Sample sample = new Sample();
    sample.value = new Person();
    
    mongoTemplate.save(sample);
    
    { "_class" : "com.acme.Sample",
      "value" : { "_class" : "com.acme.Person" }
    }

    通过使用@TypeAlias可以把在_class下面存放的值变成固定的值,下面的例子为pers。

    @TypeAlias("pers")
    class Person {
    
    }

    定义自己的MongoTypeMapper

    1. 使用java config

    class CustomMongoTypeMapper extends DefaultMongoTypeMapper {
      //implement custom type mapping here
    }
    @Configuration
    class SampleMongoConfiguration extends AbstractMongoConfiguration {
    
        @Override
        protected String getDatabaseName() {
            return "database";
        }
    
        @Override
        public Mongo mongo() throws Exception {
            return new Mongo();
        }
    
        @Bean
        @Override
        public MappingMongoConverter mappingMongoConverter() throws Exception {
            MappingMongoConverter mmc = super.mappingMongoConverter();
            mmc.setTypeMapper(customTypeMapper());
            return mmc;
        }
    
        @Bean
        public MongoTypeMapper customTypeMapper() {
            return new CustomMongoTypeMapper();
        }
    }

    2. 使用xml

    <mongo:mapping-converter type-mapper-ref="customMongoTypeMapper"/>
    
    <bean name="customMongoTypeMapper" class="com.bubu.mongo.CustomMongoTypeMapper"/>
  • 相关阅读:
    (转)MySQL中MyISAM引擎与InnoDB引擎性能简单测试
    (转)数据库水平切分的实现原理解析
    ECShop笔记(品牌类)
    ECShop笔记(通用类)
    (转)InnoDB的性能(zz)
    ECSHOP笔记(商品类 三)
    ECShop笔记(积分类)
    ECShop笔记(商品类)
    ECShop笔记(二)
    phpcms 点击排行榜的改进
  • 原文地址:https://www.cnblogs.com/lemonbar/p/3893552.html
Copyright © 2011-2022 走看看