我尝试使用Mongo DB集合填充Java POJO类,但在尝试从Pojo类获取数据时得到null
这里,CSVRow是POJO类名
类构造函数
MongoDatabase database;
MongoDbUtils() {
String uri = "mongodb://localhost:27017/?maxPoolSize=20&w=majority";
//This registry is required for your Mongo document to POJO conversion
ConnectionString connectionString = new ConnectionString(uri);
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings clientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(clientSettings);
database = mongoClient.getDatabase("StudyPriceDataTest");
}
从POJO检索数据的函数
public MongoCollection<CsvRow> RetriveAllDocumentsInCSVRows(String collectionName) {
System.out.println(collectionName);
MongoCollection<CsvRow> collection = database.getCollection(collectionName, CsvRow.class);
List<CsvRow> csvRows = collection.find(new Document(), CsvRow.class).into(new ArrayList<CsvRow>());
for(CsvRow doc:csvRows) {
System.out.println("vinsds="+doc.getVin()); **<-- here I am getting null**
}
return collection;
}
我尝试了上面提到的代码,希望从POJO获得数据
1条答案
按热度按时间pjngdqdw1#
我们可以用