MongoDB(三)——java集成mongodb

代码下载地址:https://github.com/tazhigang/big-data-github.git

一、项目结构

MongoDB(三)——java集成mongodb_第1张图片

二、pom.xml

  1. mongodb-parent----->pom.xml
    	
    	
    		4.0.0
    
    		com.ittzg.mongodb
    		mongodb-parent
    		pom
    		1.0-SNAPSHOT
    
    		
    			mongodb-client
    			springboot-mongodb
    		
    		
    			
    			
    				com.alibaba
    				fastjson
    				1.2.58
    			
    		
    		
    			
    				
    					org.apache.maven.plugins
    					maven-compiler-plugin
    					
    						1.8
    						1.8
    						UTF-8
    					
    				
    			
    		
    	
    
  2. mongodb-client----->pom.xml
    	
    	
    		
    			mongodb-parent
    			com.ittzg.mongodb
    			1.0-SNAPSHOT
    		
    		4.0.0
    
    		mongodb-client
    
    		
    			
    			
    				org.mongodb
    				mongodb-driver
    				3.11.0-beta4
    			
    			
    				junit
    				junit
    				4.12
    			
    		
    	
    

三、代码

  1. 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: [email protected]
    	 * @author: ittzg
    	 * @date: 2019/7/11 23:13
    	 */
    	public class MDBClientUtil {
    		MongoDatabase db = null;
    		MongoCollection 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 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 documents2 = usersDB.find();
    			for (Document document : documents2) {
    				System.out.println(document.toJson());
    			}
    		}
    	}
    
  2. User.java
    	package com.ittzg.mongodb;
    
    	import org.bson.types.ObjectId;
    
    	/**
    	 * @email: [email protected]
    	 * @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 + '\'' +
    					'}';
    		}
    	}
    

转载于:https://my.oschina.net/ittzg/blog/3073838

你可能感兴趣的:(MongoDB(三)——java集成mongodb)