springboot怎么连接mongodb的

 我来答
育知同创教育
2017-04-10 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
springboot连接mongodb方法:
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myproject</name>
<url>http://maven.apache.org</url>

<!-- 要继承的项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-MongoDB</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

package com.example.myproject.dao;

import Java.util.List;

import com.mongodb.DBObject;

public interface UserDao {

long count();

List<DBObject> find();

}

package com.example.myproject.dao.impl;

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

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class UserDaoImpl implements UserDao{

@Autowired
MongoClient client;

DBCollection dbCollection;

//初始化方法
@PostConstruct
public void init(){

DB db = client.getDB("test");

dbCollection = db.getCollection("users");

}

public long count() {

return dbCollection.count();
}

public List<DBObject> find(){

DBCursor it = dbCollection.find();

List<DBObject> result=new ArrayList<DBObject>();

while(it.hasNext()){

DBObject object = it.next();

result.add(object);

}

return result;
}

}

测试类:
@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<DBObject> find = userDao.find();

for(int i=0;i<find.size();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);
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式