spring 整合morhpia 访问mongodb 集群

spring 3.0.1,  mongo-2.7.3.jar ,morphia-0.99.jar 

 

 

配置如下:

 

<?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:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">


    <task:annotation-driven/>
    <bean id="mongoURI" class="com.mongodb.MongoURI">
        <constructor-arg value="mongodb://xxx.xxx.xx.xx:40000,yyy.yyy.yy.yy:40000,zzz.zzz.zz.zz:40000"/>
    </bean>

    <bean id="mongo" class="com.mongodb.Mongo">
        <constructor-arg ref="mongoURI"/>
    </bean>

    <bean id="morphia" class="com.google.code.morphia.Morphia">
    </bean>

    <bean name="datastore" class="com.google.code.morphia.DatastoreImpl">
        <constructor-arg index="0" ref="morphia"></constructor-arg>
        <constructor-arg index="1" ref="mongo"></constructor-arg>
        <constructor-arg index="2" value="mydb"></constructor-arg>
    </bean>
</beans>

 

 

在dao 层 注解 datastore 直接使用就行了。

 

@Repository("mongoArticleDao")
public class MongoArticleDaoImpl implements MongoArticleDao {
    @Autowired
    private Datastore datastore;


    @Override
    public void insertArticle(MongoArticle mongoArticle) {
        datastore.save(mongoArticle);
    }

}

 

初学,如有问题欢迎大家指正

你可能感兴趣的:(mongodb)