温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中怎么获取MongoDB连接

发布时间:2021-07-16 17:08:28 来源:亿速云 阅读:251 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关Java中怎么获取MongoDB连接,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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

<dependency>  <groupId>org.mongodb</groupId>  <artifactId>mongodb-driver</artifactId>  <version>3.4.1</version></dependency><dependency>  <groupId>org.mongodb</groupId>  <artifactId>bson</artifactId>  <version>3.4.1</version></dependency><dependency>  <groupId>org.springframework.data</groupId>  <artifactId>spring-data-mongodb</artifactId>  <version>1.7.0.RELEASE</version></dependency>  <dependency>  <groupId>org.mongodb</groupId>  <artifactId>mongo-java-driver</artifactId>  <version>3.9.1</version></dependency>

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

List<ServerAddress> addrs = new ArrayList<ServerAddress>();addrs.add(new ServerAddress(dataSource.getSourceIp(), Integer.valueOf(dataSource.getSourcePort())));List<MongoCredential> credentials = new ArrayList<MongoCredential>();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<ServerAddress> addrs = new ArrayList<ServerAddress>(); addrs.add(serverAddress); //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码 MongoCredential credential = MongoCredential.createScramSha1Credential("root", "admin", "123456Ab".toCharArray()); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); 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<ServerAddress> addrs = new ArrayList<ServerAddress>();     addrs.add(serverAddress);          //MongoCredential.createScramSha1Credential(username,source,password)三个参数分别为 用户名 数据库名称 密码     MongoCredential credential = MongoCredential.createScramSha1Credential(username, databaseName, password.toCharArray());     List<MongoCredential> credentials = new ArrayList<MongoCredential>();     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<String,String>(){{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<ShopDO> 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<ShopDO> list = mongoTemplate.find(query,ShopDO.class,"col");    System.out.println("mongoDB按条件ID查询数据成功,集合为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<ShopDO> 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<ShopDO> 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<ShopDO> 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<ShopDO> 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());  }}

关于Java中怎么获取MongoDB连接就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI