zoukankan      html  css  js  c++  java
  • 第二部分:Spring中配置mongodb

    一、需要引用的jar包

    1、spring-data-mongodb-1.9.4.RELEASE.jar

    2、spring-data-commons-1.12.11.RELEASE.jar

    3、mongo-java-driver-2.11.0.jar

    注意:我用的spring4.3.6,使用过程中一定要注意版本问题

    二、配置文件

    applicationContext.xml文件

    <!--导入配置文件方式二-->
    <context:property-placeholder location="classpath:mongo.properties"/>
    
    <!--mongodb数据库配置-->
    <mongo:mongo host="${MONGO.HOST}" port="${MONGO.PORT}"/>
    <!--<mongo:mongo host="192.168.111.129" port="27017"/>-->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo"/>
    <constructor-arg name="databaseName" value="school"/>
    </bean>

    mongo.properties文件

    MONGO.HOST = 192.168.0.xxx(自己的IP地址)
    MONGO.PORT = 27017

    四、程序中调用

    public void test(){
        DB db = mongoTemplate.getDb();
      //mongodb是在程序中做认证,这点与关系型数据库有些不同
        db.authenticate("admin", "xxxxxx".toCharArray());
        DBObject beijingUniversity = db.getCollection("beijing_university").findOne();
        System.out.println(beijingUniversity);
        Map map = beijingUniversity.toMap();
        System.out.println(map);
    }
  • 相关阅读:
    代码之美
    一点对互联网的浅薄理解
    angularjs填写表单
    一种通用数据采集的schema定义形式
    maven常用命令
    find which process occupy the PORT
    Mac上安装boost开放环境
    codeforces 581B Luxurious Houses
    codeforces 599A Patrick and Shopping
    codeforces 597A Divisibility
  • 原文地址:https://www.cnblogs.com/guoyinli/p/9159145.html
Copyright © 2011-2022 走看看