mongodbtemplate配置

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    <mongo:mongo-client id="mongo" host="127.0.0.1" port="27017" credentials="user:pass@test">
        <mongo:client-options write-concern="NORMAL" />
    </mongo:mongo-client>

     <mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongo"/>
    
<!-- 添加这部分可以入库时屏蔽掉_class字段-->
    <bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

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

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

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

将此文件包含进xml中。

<import resource="classpath*:${mongoFileName}.xml"/>

posted @ 2016-03-09 18:01  無限大  阅读(5489)  评论(0编辑  收藏  举报