Java中获取MongoDB连接的方法详解

网友投稿 382 2022-12-25


Java中获取MongoDB连接的方法详解

首先是所需jar包,Maven中的配置如下:

org.mongodb

mongodb-driver

3.4.1

org.mongodb

bson

3.4.1

org.springframework.data

spring-data-mongodb

1.7.0.RELEASE

org.mongodb

mongo-java-driver

3.9.1

获取连接的代码如下(本中用的是模板类):

List addrs = new ArrayList();

addrs.add(new ServerAddress(dataSource.getSourceIp(), Integer.valueOf(dataSource.getSourcePort())));

List credentials = new ArrayList();

credentials.add(MongoCredential.createScramSha1Credential(dataSource.getUsername(), dataSource.getSourceName(), dataSource.getPassword().toCharArray()));

MongoClient mongoClient = new MongoClient(addrs, credentials);

MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, dataSource.getSourceName());

MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);

mongoTemplate.getCollectionNames();

或者用如下方式获取:

ServerAddress serverAddress = new ServerAddress("192.168.16.121",27017);

List addrs = new ArrayList();

addrs.add(serverAddress);

//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码

MongoCredential credential = MongoCredential.createScramSha1Credential("root", "admin", "123456Ab".toCharArray());

List credentials = new ArrayList();

credentials.add(credential);

//通过连接认证获取MongoDB连接

MongoClient mongoClient = new MongoClient(addrs,credentials);

//连接到数据库

MongoDatabase mongoDatabase = mongoClient.getDatabase("admin");

mongoDatabase.getCollection("test").find().iterator();

// 关闭数据库连接

mongoClient.close();

附带简单增删改查的例子:

package test;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import org.junit.Test;

import org.springframework.data.domain.Sort;

import org.springframework.data.mongodb.MongoDbFactory;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.mongodb.core.query.Update;

import com.mongodb.MongoClient;

import com.mongodb.MongoCredential;

import com.mongodb.ServerAddress;

import com.mongodb.WriteResult;

import bean.Book;

import bean.Good;

import bean.ShopDO;

import net.sf.json.JSONObject;

public class MongoTemplateTest {

public static MongoTemplate mongoTemplate = getMongoTemplate();

public static void main(String[] args) {

System.out.println(mongoTemplate.getCollectionNames());

}

public static MongoTemplate getMongoTemplate(){

String host = "192.168.16.121";

int port = 27017;

String databaseName = "test";

String username = "root";

String password = "123456Ab";

//ServerAddress(host,port)两个参数分别为 IP地址 端口号

ServerAddress serverAddress = new ServerAddress(host,port);

List addrs = new ArrayList();

http:// addrs.add(serverAddress);

//MongoCredential.createScramSha1Credential(username,source,password)三个参数分别为 用户名 数据库名称 密码

MongoCredential credential = MongoCredential.createScramSha1Credential(username, databaseName, password.toCharArray());

List credentials = new ArrayList();

credentials.add(credential);

//通过连接认证获取MongoDB连接

MongoClient mongoClient = new MongoClient(addrs,credentials);

MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, databaseName);

MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);

return mongoTemplate;

}

/**

* 插入数据

*/

@Test

public void save(){

ShopDO shop1 = new ShopDO(100L,"菜鸟教程");

ShopDO shop2 = new ShopDO(101L,"有道笔记");

mongoTemplate.save(shop1,"col");

mongoTemplate.save(shop2,"col");

System.out.println("mongoDB插入数据成功,集合为col,文档为:"+mongoTemplate.getCollection("col"));

}

@Test

public void save1(){

Book book = new Book(22L,"英语","32.5");

mongoTemplate.save(book,"col");

}

@Test

public void save2(){

Good good = new Good(new HashMap(){{put("id","1");put("name","动物");}});

mongoTemplate.save(good,"col");

}

@Test

public void save3(){

mongoTemplate.save(JSONObject.fromObject("{\"这样\":\"we\",\"好吧\":\"hai\"}"),"col");

System.out.println("mongoDB插入数据成功,集合为col,文档为:"+mongoTemplate.getCollection("col"));

}

/**

* 查询所有

*/

@Test

public void findAll(){

List list = mongoTemplate.findAll(ShopDO.class,"col");

System.out.println("mongoDB查询数据成功,集合为col,文档为:");

for (ShopDO shopDO:list){

System.out.println(shopDO.getNo()+"/"+shopDO.getName());

}

}

/**

* 单条件查询

*/

@Test

public void simpleQuery(){

Query query = Query.query(Criteria.where("no").is(100L));

List list = mongoTemplate.find(query,ShopDO.class,"col");

System.out.println("mongoDB按条件IwyYjPesrmD查询数据成功,集合为col,文档为:");

for (ShopDO shopDO:list){

System.out.println(shopDO.getNo()+"/"+shopDO.getName());

}

}

/**

* 多条件查询

*/

@Test

public void muchQuery(){

Criteria criteria = new Criteria();

//or是条件或查询,and是条件与查询

criteria.orOperator(

Criteria.where("no").is(100),

Criteria.where("name").is("菜鸟教程"));

Query query = new Query(criteria); //组合查询放入query

Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC,"no")); //结果集进行排序

query.with(sort);

List list = mongoTemplate.find(query,ShopDO.class,"col");

System.out.println("mongoDB组合查询数据成功,集合为col,文档为:");

for (ShopDO shopDO:list){

System.out.println(shopDO.getNo()+"/"+shopDO.getName());

}

}

/**

* 分页查询

*/

@Test

public void LimitQuery(){

Query query = new Query();

query.skip(1).limit(3);

List list = mongoTemplate.find(query,ShopDO.class,"col");

System.out.println("mongoDB分页查询下标为1开始总共3行数据,集合为col,文档为:");

for (ShopDO shopDO:list){

System.out.println(shopDO.getNo()+"/"+shopDO.getName());

}

}

/**

* 模糊查询

*/

@Test

public void LikeQuery(){

Query query = new Query(Criteria.where("name").regex("菜鸟"));

List list = mongoTemplate.find(query,ShopDO.class,"col");

System.out.println("mongoDB查询名称叫菜鸟的数据成功,集合为col,文档为:");

for (ShopDO shopDO:list){

System.out.println(shopDO.getNo()+"/"+shopDO.getName());

}

Update update = new Update();

update.set("name","菜鸟教程old");

WriteResult wr = mongoTemplate.updateMulti(query,update,"col");

System.out.println("mongoDB更新数据成功,集合为col,行数为:" + wr.getN());

}

/**

* 更新

*/

@Test

public void update(){

Query query = new Query(Criteria.where("no").is(100));

List list = mongoTemplate.find(query,ShopDO.class,"col");

System.out.println("mongoDB查询no为100的数据成功,集合为col,文档为:");

for (ShopDO shopDO:list){

System.out.println(shopDO.getNo()+"/"+shopDO.getName());

}

Update update = new Update();

update.set("name","菜鸟教程new");

WriteResult wr = mongoTemplate.updateFirst(query,update,"col");

System.out.println("mongoDB更新数据成功,集合为col,行数为:" + wr.getN());

}

/**

* 删除

*/

@Test

public void delete(){

Query query = new Query(Criteria.where("no").is(1));

WriteResult result = mongoTemplate.remove(query, "col");

System.out.println("mongoDB删除数据成功,集合为col,行数为:" + result.getN() + "删除的ID为"+result.getUpsertedId());

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:自动化api测试工具(app接口自动化测试工具)
下一篇:Springboot引入多个yml方法(多种方案)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~