Start MongoDB by reading the configuration file

Source: Internet
Author: User
Tags get ip mongodb ticket

In the actual project, the MongoDB database is often used for caching, MongoDB concurrency is relatively high, so for fast storage, read information has many advantages. In the project, the database is accessed directly for the first data request, and the information obtained is usually deposited into mongodb at this time, and the next request will no longer access the database but will be added and removed directly in MongoDB.

The usual mongodb, which we typically use in the form of configuration files, is especially advantageous for later deliveries. This is generally the following steps:

1. Write the configuration file mongodb.properties

#mongodb的ip和端口号ipandport =127.0.0.1:27017# Database name Dbname=mongodbtest

2. Class and method for writing the IP and port number of MongoDB Getmongodbipandport.java

Package Org.fkit.b2118.util;import Java.io.ioexception;import Java.io.inputstream;import java.util.Properties; public class Getmongodbipandport {        //IP with port number private String ipandport;        Database name private String dbname;        Construction method public void Getmongodbipandport () {InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("Mongodb.properties"); Properties P = new properties (); try {p.load (inputstream); this.ipandport = P.getproperty ("Ipandport");// Gets the value of the configuration file This.dbname = P.getproperty ("dbname");//Gets the value of the configuration file} catch (IOException e) {e.printstacktrace ();}} Setter,getter method 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. Write the DAO layer of the database, and Mongodb.java the database by line operation.

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 = Null;public static Dbcollection CoLlection;public static MongoDb db; @Autowired @qualifier ("Ticketservice") public ticketservice Ticketservice;public B2118ticketmapper ticketmapper;public dbobject dbs = new Basicdbobject ();/* Create a database collection connection */public void Createcon Nection (String name) throws Unknownhostexception, mongoexception{
Use configuration files and their methods to create links getmongodbipandport getmongodbipandport= new Getmongodbipandport (); Getmongodbipandport.getmongodbipandport ();//Construction method String ipandport= getmongodbipandport.getipandport ();// Get IP with portstring dbname = Getmongodbipandport.getdbname ();//Get database name//Create a Mongo Database connection object Mongo con = new Mongo (ipandport) ;//Create a connection to the related database db db = Con.getdb (dbname);d b.createcollection (name, DBS);}
/* Open a Database collection 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);d atabase = Connection.getdb (dbname); collection= Database.getcollection (name);}
/* Close a collection of databases connection */

public static void CloseConnection (String name) {connection.close ();} /* Query Data */public list<b2118ticket> Queryticket () throws Unknownhostexception, Mongoexception {//instantiate try { OpenConnection ("Ticket");} catch (Unknownhostexception e) {e.printstacktrace ();} catch (Mongoexception e) {e.printstacktrace ();} List<b2118ticket> b2118ticketlist=new arraylist<b2118ticket> ();D bcursor 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. Get set Dbcollection coll = database.getcollection (collname);D bobject DBS = new Basicdbobject (); int count = Coll.remove (DBS). GETN (); return count;}}

  

Start MongoDB by reading the configuration file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.