zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    一万年太久,只争朝夕!
  • 相关阅读:
    mac 系统下删除目录的所有.svn文件
    java DES加密解密文件
    也许,未来需要重新规划
    android选择图片或拍照图片上传到服务器(包括上传参数)
    iOS DES ECB模式对称加密解密
    iOS开发中防止键盘挡住UITextField解决方案
    xCode 4.X 免证书真机公布及调试
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    java DES ECB模式对称加密解密
    解决error: failed to launch"/private/var/mobile/Applications/XX" timed out waiting for app to launch
  • 原文地址:https://www.cnblogs.com/chaoba/p/10058781.html
Copyright © 2011-2022 走看看