这篇文章将为大家详细讲解有关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(); 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按条件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 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()); }}
关于Java中怎么获取MongoDB连接就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
文章名称:Java中怎么获取MongoDB连接
网站URL:
http://cxhlcq.com/article/ghehpg.html