码迷,mamicode.com
首页 > 数据库 > 详细

使用spring-data-mongodb操作mongodb

时间:2016-06-18 19:53:40      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

mongodb.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:mongo="http://www.springframework.org/schema/data/mongo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:property-placeholder location="classpath*:mongodb.properties"  />

    <!--username="${mongo.username}"-->
    <!--password="${database.password}"-->
    <mongo:db-factory id="mongoDbFactory"
                      host="${mongo.host}"
                      port="${mongo.port}"
                      dbname="${mongo.dbname}"
    />
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>
</beans>

mongodb.properties

mongo.host=127.0.0.1
mongo.port=27017
mongo.dbname=test
#mongo.username=
#database.password=

操作mongodb的测试方法

@Resource
MongoOperations mongoOperations;

@Test
public void testMongodb(){

    User user = new User();
    user.setName("testMongodb");
    user.setAge(12);
    mongoOperations.save(user, "collectionName");
    User userGetFromMdb = mongoOperations.findOne(new Query(Criteria.where("name").is("testMongodb")), User.class, "collectionName");
    System.out.println(userGetFromMdb);
}

 

使用spring-data-mongodb操作mongodb

标签:

原文地址:http://www.cnblogs.com/ytpblog/p/5596646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!