代码下载地址:https://github.com/tazhigang/big-data-github.git
一、项目结构
二、pom.xml
- mongodb-parent----->pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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.ittzg.mongodb</groupId> <artifactId>mongodb-parent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>mongodb-client</module> <module>springboot-mongodb</module> </modules> <dependencies> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
- mongodb-client----->pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>mongodb-parent</artifactId> <groupId>com.ittzg.mongodb</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mongodb-client</artifactId> <dependencies> <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.11.0-beta4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
三、代码
- MDBClientUtil.java
package com.ittzg.mongodb; import com.alibaba.fastjson.JSON; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import org.bson.Document; import org.junit.Before; import org.junit.Test; /** * @email: tazhigang095@163.com * @author: ittzg * @date: 2019/7/11 23:13 */ public class MDBClientUtil { MongoDatabase db = null; MongoCollection<Document> usersDB =null; @Before public void init(){ //连接mongodb服务器 MongoClient mongoDBClient = new MongoClient("127.0.0.1", 27017); //建立客户端连接 db = mongoDBClient.getDatabase("test"); //获取数据库test usersDB = db.getCollection("users"); //获取users集合的操作 } @Test public void testInsert(){ Document doc = new Document("name", "汤姆"); doc.append("age",18); doc.append("shuxin","老鼠"); usersDB.insertOne(doc); //插入一条数据 } /** * 添加一个对象 */ @Test public void testInserObj(){ User user = new User("杰瑞", 21, "猫"); String userJsonStr = JSON.toJSONString(user); //将对象转为json格式字符串 Document doc = Document.parse(userJsonStr); //解析成文档对象 usersDB.insertOne(doc); } /** * 查询一个对象 */ @Test public void testFind(){ Document doc = usersDB.find().first();//查出users中的第一个文档 User user = JSON.parseObject(doc.toJson(), User.class);//将其反序列化成对象 System.out.println(user); } /** * 删除一个文档 */ @Test public void testDelete(){ usersDB.deleteOne(Filters.eq("name","杰瑞")); } /** * 更新一个文档 */ @Test public void testUpdate(){ FindIterable<Document> documents = usersDB.find(); for (Document document : documents) { System.out.println(document.toJson()); } usersDB.updateOne(Filters.eq("name","杰瑞"), new Document("$set" , new Document("like","play2")) ); System.out.println("==============="); FindIterable<Document> documents2 = usersDB.find(); for (Document document : documents2) { System.out.println(document.toJson()); } } }
- User.java
package com.ittzg.mongodb; import org.bson.types.ObjectId; /** * @email: tazhigang095@163.com * @author: ittzg * @date: 2019/7/11 23:26 */ public class User { private ObjectId _id; private String name; private Integer age; private String shuxin; public User(String name, Integer age, String shuxin) { this.name = name; this.age = age; this.shuxin = shuxin; } public User() { } public ObjectId get_id() { return _id; } public void set_id(ObjectId _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getShuxin() { return shuxin; } public void setShuxin(String shuxin) { this.shuxin = shuxin; } @Override public String toString() { return "User{" + "_id=" + _id + ", name='" + name + '\'' + ", age=" + age + ", shuxin='" + shuxin + '\'' + '}'; } }