首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

mongodb运用

2012-07-20 
mongodb使用mongodb使用 1. 下载 mongo-2.6.3.jar 2. 新建 java项目 3. 按Spring方式封装查询 直接上代码

mongodb使用
mongodb使用
1. 下载 mongo-2.6.3.jar
2. 新建 java项目
3. 按Spring方式封装查询 直接上代码



Java代码 
1.package com.mytest;  
2. 
3.import java.net.UnknownHostException;  
4. 
5.import com.mongodb.DB;  
6.import com.mongodb.DBCollection;  
7.import com.mongodb.Mongo;  
8.import com.mongodb.MongoException;  
9. 
10./** 
11. *  
12. *  
13. * @author lw 
14. * @created 2011-6-27 下午04:26:40 
15. * @version 1.0.0 
16. * @date 2011-6-27 下午04:26:40 
17. */ 
18. 
19.public class DBTemplate {  
20.    private static String MONGODB_SERVER = "192.168.42.212";  
21.    private static int SERVER_PORT = 27017;  
22.    private static String MONGODB_DBNAME = "test";  
23. 
24.    public final Object execute(MsgCallback action, String collection) {  
25.        DB db = getConn();  
26.        DBCollection dbColl = db.getCollection(collection);  
27.        Object result = action.doExecute(dbColl);  
28.        closeDb(db);  
29.        closeCollection(dbColl);  
30. 
31.        return result;  
32.    }  
33. 
34.    private DB getConn() {  
35.        return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME);  
36.    }  
37. 
38.    private DB getConn(String server, int port, String dbName) {  
39.        Mongo m = null;  
40.        try {  
41.            m = new Mongo(server, port);  
42.        } catch (UnknownHostException e) {  
43.            e.printStackTrace();  
44.        } catch (MongoException e) {  
45.            e.printStackTrace();  
46.        }  
47.        return m.getDB(dbName);  
48.    }  
49. 
50.    private void closeDb(DB db) {  
51.        if (db != null) {  
52.            db = null;  
53.        }  
54.    }  
55. 
56.    private void closeCollection(DBCollection col) {  
57.        if (col != null) {  
58.            col = null;  
59.        }  
60.    }  
61.} 
package com.mytest;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
*
*
* @author lw
* @created 2011-6-27 下午04:26:40
* @version 1.0.0
* @date 2011-6-27 下午04:26:40
*/

public class DBTemplate {
private static String MONGODB_SERVER = "192.168.42.212";
private static int SERVER_PORT = 27017;
private static String MONGODB_DBNAME = "test";

public final Object execute(MsgCallback action, String collection) {
DB db = getConn();
DBCollection dbColl = db.getCollection(collection);
Object result = action.doExecute(dbColl);
closeDb(db);
closeCollection(dbColl);

return result;
}

private DB getConn() {
return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME);
}

private DB getConn(String server, int port, String dbName) {
Mongo m = null;
try {
m = new Mongo(server, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
return m.getDB(dbName);
}

private void closeDb(DB db) {
if (db != null) {
db = null;
}
}

private void closeCollection(DBCollection col) {
if (col != null) {
col = null;
}
}
}




Java代码 
1.package com.mytest;  
2. 
3.import com.mongodb.DBCollection;  
4. 
5./** 
6. *  
7. * 功能描述:  
8. * 
9. * @author lw 
10. * @created 2011-6-28 下午01:57:44 
11. * @version 1.0.0 
12. * @date 2011-6-28 下午01:57:44 
13. */ 
14. 
15.public interface MsgCallback {  
16.    Object doExecute(DBCollection dbCollection);  
17.} 
package com.mytest;

import com.mongodb.DBCollection;

/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午01:57:44
* @version 1.0.0
* @date 2011-6-28 下午01:57:44
*/

public interface MsgCallback {
Object doExecute(DBCollection dbCollection);
}




Java代码 
1.package com.mytest;  
2. 
3.import java.util.List;  
4.import java.util.Map;  
5. 
6.import com.mongodb.BasicDBObject;  
7.import com.mongodb.DBObject;  
8. 
9./** 
10. *  
11. * 功能描述: 
12. *  
13. * @author lw 
14. * @created 2011-6-28 下午02:13:33 
15. * @version 1.0.0 
16. * @date 2011-6-28 下午02:13:33 
17. */ 
18. 
19.public interface SQLTemplate {  
20. 
21.    int insert(String collection, BasicDBObject dbObj);  
22. 
23.    int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);  
24. 
25.    int delete(String collection, BasicDBObject dbObj);  
26. 
27.    Object selectOne(String collection, BasicDBObject dbObj);  
28. 
29.    Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);  
30. 
31.    List<DBObject> selectList(String collection, final BasicDBObject dbObj);  
32. 
33.} 
package com.mytest;

import java.util.List;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:13:33
* @version 1.0.0
* @date 2011-6-28 下午02:13:33
*/

public interface SQLTemplate {

int insert(String collection, BasicDBObject dbObj);

int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);

int delete(String collection, BasicDBObject dbObj);

Object selectOne(String collection, BasicDBObject dbObj);

Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);

List<DBObject> selectList(String collection, final BasicDBObject dbObj);

}


Java代码 
1.package com.mytest;  
2. 
3.import java.util.List;  
4.import java.util.Map;  
5. 
6.import com.mongodb.BasicDBObject;  
7.import com.mongodb.DBObject;  
8. 
9./** 
10. *  
11. * 功能描述: 
12. *  
13. * @author lw 
14. * @created 2011-6-28 下午02:13:33 
15. * @version 1.0.0 
16. * @date 2011-6-28 下午02:13:33 
17. */ 
18. 
19.public interface SQLTemplate {  
20. 
21.    int insert(String collection, BasicDBObject dbObj);  
22. 
23.    int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);  
24. 
25.    int delete(String collection, BasicDBObject dbObj);  
26. 
27.    Object selectOne(String collection, BasicDBObject dbObj);  
28. 
29.    Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);  
30. 
31.    List<DBObject> selectList(String collection, final BasicDBObject dbObj);  
32. 
33.} 
package com.mytest;

import java.util.List;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:13:33
* @version 1.0.0
* @date 2011-6-28 下午02:13:33
*/

public interface SQLTemplate {

int insert(String collection, BasicDBObject dbObj);

int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);

int delete(String collection, BasicDBObject dbObj);

Object selectOne(String collection, BasicDBObject dbObj);

Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);

List<DBObject> selectList(String collection, final BasicDBObject dbObj);

}


Java代码 
1.package com.mytest;  
2. 
3.import java.util.List;  
4.import java.util.Map;  
5. 
6.import com.mongodb.BasicDBObject;  
7.import com.mongodb.DBCollection;  
8.import com.mongodb.DBObject;  
9. 
10./** 
11. *  
12. * 功能描述: 
13. *  
14. * @author lw 
15. * @created 2011-6-28 下午02:21:43 
16. * @version 1.0.0 
17. * @date 2011-6-28 下午02:21:43 
18. */ 
19. 
20.public class SQLDao implements SQLTemplate {  
21.    //可改成 Spring 注入  
22.    DBTemplate dbTemp = new DBTemplate();  
23. 
24.    public int insert(String collection, final BasicDBObject dbObj) {  
25.        return (Integer) dbTemp.execute(new MsgCallback() {  
26.            public Object doExecute(DBCollection dbCollection) {  
27.                return dbCollection.insert(dbObj).getN();  
28.            }  
29.        }, collection);  
30.    }  
31. 
32.    public int update(String collection, final BasicDBObject oldObj,  
33.            final BasicDBObject newObj) {  
34.        return (Integer) dbTemp.execute(new MsgCallback() {  
35.            public Object doExecute(DBCollection dbCollection) {  
36.                return dbCollection.update(oldObj, newObj).getN();  
37.            }  
38.        }, collection);  
39.    }  
40. 
41.    public int delete(String collection, final BasicDBObject dbObj) {  
42.        return (Integer) dbTemp.execute(new MsgCallback() {  
43.            public Object doExecute(DBCollection dbCollection) {  
44.                return dbCollection.remove(dbObj).getN();  
45.            }  
46.        }, collection);  
47.    }  
48. 
49.    public Object selectOne(String collection, final BasicDBObject dbObj) {  
50.        return dbTemp.execute(new MsgCallback() {  
51.            public Object doExecute(DBCollection dbCollection) {  
52.                return dbCollection.findOne(dbObj);  
53.            }  
54.        }, collection);  
55.    }  
56. 
57.    @SuppressWarnings("unchecked")  
58.    public Map<String, DBObject> selectMap(String collection,  
59.            final BasicDBObject dbObj) {  
60.        return ((DBObject) selectOne(collection, dbObj)).toMap();  
61.    }  
62. 
63.    @SuppressWarnings("unchecked")  
64.    public List<DBObject> selectList(String collection,  
65.            final BasicDBObject dbObj) {  
66.        return (List<DBObject>) dbTemp.execute(new MsgCallback() {  
67.            public Object doExecute(DBCollection dbCollection) {  
68.                return dbCollection.find(dbObj).toArray();  
69.            }  
70.        }, collection);  
71.    }  
72.} 
package com.mytest;

import java.util.List;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:21:43
* @version 1.0.0
* @date 2011-6-28 下午02:21:43
*/

public class SQLDao implements SQLTemplate {
//可改成 Spring 注入
DBTemplate dbTemp = new DBTemplate();

public int insert(String collection, final BasicDBObject dbObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.insert(dbObj).getN();
}
}, collection);
}

public int update(String collection, final BasicDBObject oldObj,
final BasicDBObject newObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.update(oldObj, newObj).getN();
}
}, collection);
}

public int delete(String collection, final BasicDBObject dbObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.remove(dbObj).getN();
}
}, collection);
}

public Object selectOne(String collection, final BasicDBObject dbObj) {
return dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.findOne(dbObj);
}
}, collection);
}

@SuppressWarnings("unchecked")
public Map<String, DBObject> selectMap(String collection,
final BasicDBObject dbObj) {
return ((DBObject) selectOne(collection, dbObj)).toMap();
}

@SuppressWarnings("unchecked")
public List<DBObject> selectList(String collection,
final BasicDBObject dbObj) {
return (List<DBObject>) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.find(dbObj).toArray();
}
}, collection);
}
}



Java代码 
1.package com.mytest;  
2. 
3.import java.util.ArrayList;  
4.import java.util.List;  
5. 
6.import com.mongodb.BasicDBObject;  
7.import com.mongodb.DBObject;  
8.import com.mongodb.util.JSON;  
9. 
10./** 
11. *  
12. * 功能描述: 
13. *  
14. * @author lw 
15. * @created 2011-6-28 下午03:31:51 
16. * @version 1.0.0 
17. * @date 2011-6-28 下午03:31:51 
18. */ 
19. 
20.public class TestSQLDao extends SQLDao {  
21. 
22.    /** 
23.     * 功能描述: 
24.     *  
25.     * @param args 
26.     */ 
27. 
28.    public static void main(String[] args) {  
29.        TestSQLDao test = new TestSQLDao();  
30. 
31.        BasicDBObject obj = new BasicDBObject();  
32.        // obj.put("id", 6);  
33.        obj.put("name", "pluto");  
34.        // BasicDBObject newObj = new BasicDBObject("$set", new  
35.        // BasicDBObject("name", "gogo"));  
36.        List<DBObject> list = new ArrayList<DBObject>();  
37.        list = test.selectList("students", obj);  
38. 
39.        for (DBObject db : list) {  
40.            System.out  
41.                    .println("-----------------------------------------------------");  
42.            System.out.println(JSON.serialize(db));  
43.            System.out  
44.                    .println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");  
45.            System.out.println(db.toMap());  
46.            System.out  
47.                    .println("-----------------------------------------------------");  
48.        }  
49.    }  
50. 
51.    public int insert(String collection, BasicDBObject dbObj) {  
52.        return super.insert(collection, dbObj);  
53.    }  
54. 
55.    public List<DBObject> selectList(String collection, BasicDBObject dbObj) {  
56.        return super.selectList(collection, dbObj);  
57.    }  
58. 
59.} 

热点排行