通过读取配置文件,启动mongodb

  在实际的项目中,经常利用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; } }

  

posted @ 2017-10-07 14:24  怡安  阅读(1723)  评论(0编辑  收藏  举报