1. 程式人生 > >通過讀取配置文件,啟動mongodb

通過讀取配置文件,啟動mongodb

star 個數 sun 數據請求 delete except bte domain 優點

  在實際的項目中,經常利用mongodb數據庫做緩存,mongodb的並發性比較高,所以對於快速存儲、讀取信息有很多優點。在項目中對於第一次的數據請求會直接訪問數據庫,而對於獲得的信息通常都會在此時刻存入到mongodb中,下一次請求就不會再訪問數據庫而是直接在mongodb中進行增刪改查。

  而通常的mongodb我們一般利用配置文件的形式來設置,這對於後期的交付也特別有利。一般就是以下幾個步驟:

  1.寫配置文件mongodb.properties

#mongodb的ip和端口號
ipandport=127.0.0.1:27017
#數據庫名字
dbname=mongodbTest

  2.寫得到mongodb的ip和端口號的類及方法GetMongoDBIpAndPort.java

package org.fkit.B2118.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class GetMongoDBIpAndPort {
        //ip與端口號
	private String ipAndPort;
        //數據庫名字
	private String dbname;
        //構造方法
	public void getMongoDBIpAndPort(){
		
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mongodb.properties");
				Properties p = new Properties();
				try {
					p.load(inputStream);
					this.ipAndPort = p.getProperty("ipandport");//獲取配置文件的值
					this.dbname = p.getProperty("dbname");//獲取配置文件的值
				} catch (IOException e) {
					e.printStackTrace();
				}
	}
	
//setter,getter方法
	public String getIpAndPort() {
		return ipAndPort;
	}
	public void setIpAndPort(String ipAndPort) {
		this.ipAndPort = ipAndPort;
	}
	public String getDbname() {
		return dbname;
	}
	public void setDbname(String dbname) {
		this.dbname = dbname;
	}


}        

  3.寫數據庫的dao層,對數據庫經行操作MongoDb.java

package org.fkit.B2118.dao;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.fkit.B2118.controller.BuyTicketController;
import org.fkit.B2118.domain.B2118ticket;
import org.fkit.B2118.mapper.B2118ticketMapper;
import org.fkit.B2118.service.TicketService;
import org.fkit.B2118.service.TicketServiceImpl;
import org.fkit.B2118.util.GetMongoDBIpAndPort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.sun.swing.internal.plaf.basic.resources.basic;


public class MongoDb {
	
	public static Mongo connection = null;
	public static DB database = null;
	public static DBCollection collection;

	public static MongoDb db ;
	
	@Autowired
	@Qualifier("ticketService")
	public TicketService ticketService;
	public B2118ticketMapper ticketMapper;
	public DBObject dbs = new BasicDBObject();
	/*
	 * 創建一個數據庫集合Connection
	 */
	public void createConnection(String name)  throws UnknownHostException, MongoException{
          //利用配置文件及其方法來創建鏈接 GetMongoDBIpAndPort getMongoDBIpAndPort= new GetMongoDBIpAndPort(); getMongoDBIpAndPort.getMongoDBIpAndPort();//構造方法 String ipandport= getMongoDBIpAndPort.getIpAndPort();//得到ip與port String dbname = getMongoDBIpAndPort.getDbname();//得到數據庫名字 //建立一個Mongo的數據庫連接對象 Mongo con = new Mongo(ipandport); //創建相關數據庫的連接 DB db = con.getDB(dbname); db.createCollection(name, dbs); }
     /*
	 * 打開一個數據庫集合Connection
	 */
	public static void openConnection(String name) throws UnknownHostException, MongoException{
		GetMongoDBIpAndPort getMongoDBIpAndPort= new GetMongoDBIpAndPort();
		getMongoDBIpAndPort.getMongoDBIpAndPort();
		String ipandport= getMongoDBIpAndPort.getIpAndPort();
		String dbname = getMongoDBIpAndPort.getDbname();
		System.out.println(ipandport);
		connection = new Mongo(ipandport);
		database = connection.getDB(dbname);
		collection=database.getCollection(name);
	}
	
     /*
	 * 關閉一個數據庫集合Connection
	 */

public static void closeConnection(String name) { connection.close(); } /* * 查詢數據 */ public List<B2118ticket> queryTicket() throws UnknownHostException, MongoException { //實例化 try { openConnection("ticket"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } List<B2118ticket> b2118ticketlist=new ArrayList<B2118ticket>(); DBCursor cur = collection.find(); while (cur.hasNext()) { DBObject object = cur.next(); B2118ticket b2118ticket = new B2118ticket(); b2118ticket.setTid(String.valueOf(object.get("tid"))); b2118ticket.setTrain(String.valueOf(object.get("train"))); b2118ticket.setTnum(String.valueOf(object.get("tnum"))); b2118ticket.setTtime((Date) object.get("ttime")); b2118ticket.setTtotle(String.valueOf(object.get("ttotle"))); b2118ticket.setStart(String.valueOf(object.get("start"))); b2118ticket.setStarttime(String.valueOf(object.get("starttime"))); b2118ticket.setEnd(String.valueOf(object.get("end"))); b2118ticket.setEndtime(String.valueOf(object.get("endtime"))); b2118ticket.setPrice(String.valueOf(object.get("price"))); b2118ticketlist.add(b2118ticket); } System.out.println(b2118ticketlist); closeConnection("ticket"); return b2118ticketlist; } public static int deleteAll(String collName){ //1.得到集合 DBCollection coll = database.getCollection(collName); DBObject dbs = new BasicDBObject(); int count = coll.remove(dbs).getN(); return count; } }

  

通過讀取配置文件,啟動mongodb