MongoDB表结构设计代码实现以及连接实现

1、以备查看

表结构设计代码:

package cn.uestc.warningTest.warningTest;
import org.bson.Document;
public class TableDesign {
	private String time;//时间
    
	private String phoneNum;//手机号
    private String type;//类型
    private String local;// 位置
    private String out;//漫出
    private String in;//漫入
    private String context;//内容
    public TableDesign(String phoneNum,String type,String context){
    	super();
    	
    	this.phoneNum = phoneNum;
    	this.type = type;
    	this.context = context;
    }
     public Document getDoc(){
    	  return new Document("手机号",phoneNum).
    			  append("预警类型",type).
    			  append("详细内容", context);
      }
     public String getTime() {
 		return time;
 	}
 	public void setTime(String time) {
 		this.time = time;
 	}
 	public String getPhoneNum() {
 		return phoneNum;
 	}
 	public void setPhoneNum(String phoneNum) {
 		this.phoneNum = phoneNum;
 	}
 	public String getType() {
 		return type;
 	}
 	public void setType(String type) {
 		this.type = type;
 	}
 	public String getLocal() {
 		return local;
 	}
 	public void setLocal(String local) {
 		this.local = local;
 	}
 	public String getOut() {
 		return out;
 	}
 	public void setOut(String out) {
 		this.out = out;
 	}
 	public String getIn() {
 		return in;
 	}
 	public void setIn(String in) {
 		this.in = in;
 	}
 	public String getContext() {
 		return context;
 	}
 	public void setContext(String context) {
 		this.context = context;
 	}
}

2、MongoDB连接:

package cn.uestc.warningTest.warningTest;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.connection.Connection;

public class mongodbConnection {
	//mongodb数据库连接
	String url ;
	String dbase;
	String collect;
	Connection connection;
	String number;
	String IMSI;
	MongoCollection col;
	public mongodbConnection(String url,String dbase){
		this.url= url;
		this.dbase = dbase;
	
	}
	public MongoCollection getConnection(String collect){
		MongoClient client = new MongoClient(url,27019);	
		MongoDatabase db = client.getDatabase(dbase);
		col =db.getCollection(collect) ;
		System.out.println("成功");
		return col;
		
	}
	public  MongoCursor query(String number, String simi,MongoCollection col){
	    BasicDBObject query = new BasicDBObject();
		query.put("手机号", number);
		BasicDBObject query1 = new BasicDBObject();
		query1.put("SIMI号", simi);
		List orQueryList = new ArrayList();  
        orQueryList.add(query);  
        orQueryList.add(query1);  
        BasicDBObject orQuery = new BasicDBObject("$or", orQueryList);  
		FindIterable findIterable = col.find(orQuery);
		MongoCursor mongoCursor = findIterable.iterator();
		return mongoCursor;
	}
	/*public static void main(String args[]){
		mongodbConnection a = new mongodbConnection("192.168.0.75","warning","warn");
	    a.getConnection();
	}*/
	
}

3、优化上面2的连接,用连接池实现:(单例模式)

package cn.uestc.warningTest.warningTest;

import java.rmi.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

public class ConnectionPool {

	private static MongoClient mongoClient = null;
	private static final ConnectionPool pool = new ConnectionPool();
	private ConnectionPool(){
		if(mongoClient == null){
			MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
			buide.connectionsPerHost(100);// 与目标数据库可以建立的最大链接数
            buide.connectTimeout(1000 * 60 * 20);// 与数据库建立链接的超时时间
            buide.maxWaitTime(100 * 60 * 5);// 一个线程成功获取到一个可用数据库之前的最大等待时间
            buide.threadsAllowedToBlockForConnectionMultiplier(100);
            buide.maxConnectionIdleTime(0);
            buide.maxConnectionLifeTime(0);
            buide.socketTimeout(0);
            MongoClientOptions myOptions = buide.build();
            try{
            	mongoClient = new MongoClient(new ServerAddress("192.168.0.75",27019),myOptions);
            }catch(Exception e){
            	e.printStackTrace();
            }
		}
	}
	public static ConnectionPool getConnectionPool(){
		return pool;
	}
	public MongoDatabase getDB(String dbName){
		return mongoClient.getDatabase(dbName);
	}
	public static MongoCollection getCollection(String dbName,String collectionName){
		MongoDatabase db = mongoClient.getDatabase(dbName);
		return db.getCollection(collectionName);
	}
	
    public static MongoCursor query(String number, String simi,String dbName,String collectName){
    	MongoDatabase db = mongoClient.getDatabase(dbName);
    	MongoCollection col =db.getCollection(collectName) ;
    	BasicDBObject query = new BasicDBObject();
		query.put("手机号", number);
		BasicDBObject query1 = new BasicDBObject();
		query1.put("SIMI号", simi);
		List orQueryList = new ArrayList();  
        orQueryList.add(query);  
        orQueryList.add(query1);  
        BasicDBObject orQuery = new BasicDBObject("$or", orQueryList);  
		FindIterable findIterable = col.find(orQuery);
		MongoCursor mongoCursor = findIterable.iterator();
		return mongoCursor;
	}

}

你可能感兴趣的:(MongoDB表结构设计代码实现以及连接实现)