• MongoDB插入数据,除去Class key


    本文来源于:

    来源:CSDN
    原文:https://blog.csdn.net/ClementAD/article/details/50849680

    去除class字段的本质为设置
    DefaultMongoTypeMapper
    typeKey为空
    
    

    xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- 让Spring通过自动扫描来查询和管理Bean -->
    <context:component-scan base-package="SpringMogo.mongo" />

    <!-- 使Spring关注Annotation -->
    <context:annotation-config/>

    <!-- Default bean name is 'mongo' -->
    <mongo:mongo host="localhost" port="27017" id="mongo" />

    <mongo:db-factory dbname="chaoba" mongo-ref="mongo" />

    <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey"><null/></constructor-arg>
    </bean>

    <bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"></bean>

    <bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <property name="typeMapper" ref="defaultMongoTypeMapper"></property>
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mongoMappingContext" />
    </bean>

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mongoConverter" ref="mappingMongoConverter"/>
    </bean>

    <mongo:repositories base-package="SpringMogo.mongo"></mongo:repositories>

    </beans>

    代码配置
    @Configuration
    public class MongoConfigLog extends AbstractMongoConfiguration{
     
        @Value("${mongo.ip}")
        private String MONGO_IP;
        @Value("${mongo.port}")
        private int MONGO_PORT;
     
        @Override
        protected String getDatabaseName() {
            return "dbName";
        }
     
        @Override
        public Mongo mongo() throws Exception {
            return new MongoClient(MONGO_IP, MONGO_PORT);
        }
     
        //都只是为了去除“_class”字段
        @Bean
        @Override
        public MappingMongoConverter mappingMongoConverter() throws Exception {
            MappingMongoConverter mmc = super.mappingMongoConverter();
            mmc.setTypeMapper(new DefaultMongoTypeMapper(null));
            return mmc;
        }
    }
    一万年太久,只争朝夕!
  • 相关阅读:
    昆石VOS3000_2.1.4.0完整安装包及安装脚本
    KVPhone,VOS官方的SIP软电话电脑客户端
    昆石VOS2009 VOS3000无漏洞去后门电脑管理客户端大全
    2017年最新(4月20日)手机号码归属地数据库分享
    2017年最新VOS2009/VOS3000最新手机号段导入文件(手机归属地数据)
    Android:onNewIntent()
    三星S4使用体验(Markdown版)
    apple公司的潮起潮落——浪潮之巅
    microsoft的罗马帝国——浪潮之巅
    我的iOS开发之路
  • 原文地址:https://www.cnblogs.com/chaoba/p/10058781.html
Copyright © 2020-2023  润新知