当前位置:Gxlcms >
数据库问题 >
不使用spring的情况下用java原生代码操作mongodb数据库的两种方式
不使用spring的情况下用java原生代码操作mongodb数据库的两种方式
时间:2021-07-01 10:21:17
帮助过:2人阅读
主要原因实际上是和mongodb本身无关的,因为他们改的是配置文件的密码,而实际上这个密码在代码中根本就没有使用,他们在代码中已经把用户验证信息写死。
在协助他们解决这个问题的时候,我看到他们代码中在和mongodb数据库交互时使用了已经不被建议使用的方法,于是便抽时间尝试了一下另一种被建议的方式实现各功能。
当然了,生产环境中用的是mongodb集群,验证时的写法和单机时会略有不同,我这里就只拿单机试验。
使用原生的Java代码操作mongodb数据库,就不需要和spring集成的那些jar包,只用到了mongodb-java-driver3.0.3.jar,代码如下,一些需要注意的地方也都写在注释中:
[java] view
plain
copy
-
package monAndMysql;
-
-
import java.util.ArrayList;
-
import java.util.Date;
-
import java.util.List;
-
import java.util.Set;
-
import org.bson.BsonDocument;
-
import org.bson.BsonString;
-
import org.bson.Document;
-
import org.bson.conversions.Bson;
-
import com.mongodb.BasicDBObject;
-
import com.mongodb.DB;
-
import com.mongodb.DBCollection;
-
import com.mongodb.DBCursor;
-
import com.mongodb.DBObject;
-
import com.mongodb.MongoClient;
-
import com.mongodb.MongoCredential;
-
import com.mongodb.ServerAddress;
-
import com.mongodb.client.FindIterable;
-
import com.mongodb.client.MongoCollection;
-
import com.mongodb.client.MongoCursor;
-
import com.mongodb.client.MongoDatabase;
-
import com.mongodb.client.model.Filters;
-
-
-
-
-
-
-
-
public class MonAndMysqlTest {
-
-
public static void main(String[] args) {
-
mongodbTest();
-
}
-
-
public static void mongodbTest() {
-
ServerAddress sa = new ServerAddress("192.168.0.7", 27017);
-
List<MongoCredential> mongoCredentialList = newArrayList<MongoCredential>();
-
-
mongoCredentialList.add(MongoCredential.createMongoCRCredential(
-
"admin", "admin", "123456".toCharArray()));
-
MongoClient client = new MongoClient(sa, mongoCredentialList);
-
-
-
DB mongoDB = client.getDB("mongoTest1");
-
DBCollection collection1 = mongoDB.getCollection("userTest1");
-
-
DBObject document1 = new BasicDBObject();
-
document1.put("name", "mongoTest1");
-
document1.put("createTime", new Date().getTime());
-
-
collection1.insert(document1);
-
-
DBCursor cursor1 = collection1.find();
-
System.out.println("第一种方式插入数据的结果:");
-
while (cursor1.hasNext()) {
-
DBObject object = cursor1.next();
-
Set<String> keySet = object.keySet();
-
for (String key : keySet) {
-
System.out.println(key + ":" + object.get(key));
-
}
-
}
-
-
DBObject query = new BasicDBObject();
-
DBObject update = new BasicDBObject();
-
query.put("name", "mongoTest1");
-
update.put("$set", new BasicDBObject("name", "update1"));
-
collection1.update(query, update);
-
System.out
-
.println("--------------------------------------------------------------------------------------");
-
System.out.println("第一种方式修改数据的结果:");
-
DBCursor cursor11 = collection1.find();
-
while (cursor11.hasNext()) {
-
DBObject object = cursor11.next();
-
Set<String> keySet = object.keySet();
-
for (String key : keySet) {
-
System.out.println(key + ":" + object.get(key));
-
}
-
}
-
-
DBObject query1 = new BasicDBObject();
-
query1.put("name", "update1");
-
collection1.remove(query1);
-
System.out
-
.println("--------------------------------------------------------------------------------------");
-
System.out.println("第一种方式删除数据的结果:");
-
DBCursor cursor12 = collection1.find();
-
while (cursor12.hasNext()) {
-
DBObject object = cursor12.next();
-
Set<String> keySet = object.keySet();
-
for (String key : keySet) {
-
System.out.println(key + ":" + object.get(key));
-
}
-
}
-
-
System.out
-
.println("****************************************************************************");
-
-
MongoDatabase database = client.getDatabase("m