spring boot 通过配置文件连接mongodb

配置文件:在src/main/resource目录下:

application.properties 内容为:


spring.data.mongodb.host=localhost

spring.data.mongodb.port=27017

spring.data.mongodb.database=test


package com.example.myproject.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Repository;

import com.example.myproject.dao.UserDao;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@Repository
public class UserDaoImpl implements UserDao{
    
    @Autowired
    MongoOperations oper;
    
    public long count() {
        
        DBCollection dbCollection = oper.getCollection("users");
        
        return dbCollection.count();
    }
    
    public List find(){
        
        DBCollection dbCollection = oper.getCollection("users");
        
        DBCursor it = dbCollection.find();
        
        List result=new ArrayList();
        
        while(it.hasNext()){
            
            DBObject object = it.next();
            
            result.add(object);
            
        }
        
        return result;
    }

}


测试类:

package com.example.myproject;

import java.net.UnknownHostException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.myproject.dao.impl.UserDaoImpl;
import com.example.myproject.po.User;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

////@SpringBootApplication
//@Configuration
//@EnableAutoConfiguration
//@ComponentScan
//public class Example {
//
////    @Autowired
////    ConnectionSettings connection;
//    
//    @RequestMapping("/")
//    String home() {
//        
////        System.out.println(connection.getName() + "--" + connection.getAge());
//        
//        return "Hello World!";
//    }
//
//    public static void main(String[] args) throws Exception {
//        SpringApplication.run(Example.class, args);
//    }
//
//}



@RestController
@EnableAutoConfiguration
@ComponentScan
public class Example {
    
    @Autowired
    ConnectionSettings connection;
    
    @Autowired
    UserDaoImpl userDao;
    
    
    private Logger logger = Logger.getLogger(Example.class);

    @RequestMapping("/")
    String home() throws UnknownHostException {
        
        long count = userDao.count();
        
        System.out.println(count);
        
        List find = userDao.find();
        
        for(int i=0;i             
            DBObject DBObject = find.get(i);
            
            System.out.println(DBObject);
        }
        
        logger.info(connection.getName() + "--" + connection.getAge());
        
        return connection.getName() + connection.getAge() + connection.getSex();
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

你可能感兴趣的:(spring-boot)