如何从java中的getter和setter方法获取值并插入到Mongodb中。我使用下面的代码插入到mongodb中
DBCollection coll = db.getCollection("mycol");
BasicDBObject doc = new BasicDBObject("id","Hello" ).
append("description", "database").
append("likes", 100).
append("url", "http://http://www.flowersofindia.net/").
append("by", "Rose");
coll.insert(doc);
上面给出的是一些硬代码值。但是我需要从我编写的 setter 方法中获取值。这是我的 getter 和 setter 类。
public class Encapsulation {
private String id;
private String product_name;
private String product_url;
private String product_image;
private String product_price;
private String product_src;
private String country;
private String date;
private String Category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_url() {
return product_url;
}
public void setProduct_url(String product_url) {
this.product_url = product_url;
}
public String getProduct_image() {
return product_image;
}
public void setProduct_image(String product_image) {
this.product_image = product_image;
}
public String getProduct_price() {
return product_price;
}
public void setProduct_price(String product_price) {
this.product_price = product_price;
}
public String getProduct_src() {
return product_src;
}
public void setProduct_src(String product_src) {
this.product_src = product_src;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
}
.有人可以帮忙吗? 任何帮助将不胜感激。我是这个环境的新手
下面是使用 setter 和 getter 插入 mongodb 的代码。
请注意
1) 我使用了相同的
Encapsulation
类以及 getter 和 setter。
2)我使用
mongo-java-driver-2.12.2.jar
作为mongo java驱动程序。
3) mongodb 正在端口 27017 运行。
代码:
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
public class MongoInsert {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Encapsulation bean=new Encapsulation();
// Setting values
bean.setId("value1");
bean.setProduct_name("value2");
bean.setProduct_image("value3");
bean.setProduct_price("value4");
bean.setProduct_src("value5");
bean.setProduct_url("value6");
bean.setDate("value7");
bean.setCountry("value8");
bean.setCategory("value9");
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("Test");
DBCollection coll = db.getCollection("mycol");
//getting values and assigning to mongo field
BasicDBObject doc = new BasicDBObject("id", bean.getId()).
append("Product_name", bean.getProduct_name()).
append("Product_image", bean.getProduct_image()).
append("Product_price", bean.getProduct_price()).
append("Product_src", bean.getProduct_src()).
append("Product_url", bean.getProduct_url()).
append("Date", bean.getDate()).
append("Country", bean.getCountry()).
append("Category", bean.getCategory());
coll.insert(doc);
}
}
希望有帮助。
编辑:昆德拉不再维护。
您想要将 POJO 与 MongoDB 数据库映射。您可以使用 ORM 来实现此目的。使用Kundera,您可以通过SQL方式轻松实现CRUD和查询操作。 Kundera 是一个符合 JPA 2.1 规范的 NoSQL 数据存储对象数据存储映射库。您可以在下面提到的链接中找到有关昆德拉的更多信息。
https://github.com/impetus-opensource/Kundera/wiki
希望有帮助...:)