spring-data-mongo查询数组示例

时间:2015-08-30 15:03:37

标签: spring-data spring-data-mongodb

我是spring-data-mongo的新手。我有以下文件Food和我想使用Querying Arrys查询。我跟随食物收藏

> db.food.find()
{ "_id" : 1, "fruit" : [  "apple",  "banana",  "peach" ] }
{ "_id" : 2, "fruit" : [  "apple",  "kumquat",  "orange" ] }
{ "_id" : 3, "fruit" : [  "cherry",  "banana",  "apple" ] }
>

我正在使用以下查询我得到了正确的数据。 现在我希望使用Spring-data-mongo

> db.food.find({fruit : {$all : ["apple","banana"]}})
{ "_id" : 1, "fruit" : [  "apple",  "banana",  "peach" ] }
{ "_id" : 3, "fruit" : [  "cherry",  "banana",  "apple" ] }

到目前为止请参考我的发展: Food.java

package com.mkyong.model;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document
public class Food {
    @Id
    private Double id;
    @Field
    private List<String> fruits;

    public Double getId() {
        return id;
    }
    public void setId(Double id) {
        this.id = id;
    }
    public List<String> getFruits() {
        return fruits;
    }
    public void setFruits(List<String> fruits) {
        this.fruits = fruits;
    }
    @Override
    public String toString() {
        return "Food [id=" + id + ", fruits=" + fruits + "]";
    }
}

FoodRepository:

package com.mkyong.reposiroty;

import org.springframework.data.repository.CrudRepository;

import com.mkyong.model.Food;

public interface FoodRepository extends CrudRepository<Food, String>{

}

SpringMongoConfig.java

package com.mkyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {

    @Override
    public String getDatabaseName() {
        return "test";
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient("127.0.0.1");
    }
}

SpringConfig.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mongo:mongo host="127.0.0.1" port="27017" />
    <mongo:db-factory dbname="test" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
</beans>

App.java

package com.mkyong.core;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.mkyong.config.SpringMongoConfig;
import com.mkyong.model.Food;

public class App {

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
        MongoOperations mongoOperations = (MongoOperations) ctx.getBean("mongoTemplate");

        Query q = new Query(Criteria.where("fruit").all("apple","banana"));
        List<Food> foods = mongoOperations.find(q, Food.class);

        for (Food food : foods) {
            System.out.println("-------------------");
            System.out.println("ID : "+ food.getId());
            System.out.println("Fruits : "+ food.getFruits());
        }
    }
}

主要方法的结果,它没有任何东西。水果显示为空。有没有专家可以帮我从Fruits Array / List中提取数据?如果您需要任何其他信息,请告诉我?

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
-------------------
ID : 1.0
Fruits : null
-------------------
ID : 3.0
Fruits : null

1 个答案:

答案 0 :(得分:0)

是的,我能够解决错误。需要进行以下更改:

将Food类中的数据类型更改为private String [] fruit;

@Document
public class Food {
    @Id
    private Double id;
    @Field
    private String[] fruit;

    public Double getId() {
        return id;
    }
    public void setId(Double id) {
        this.id = id;
    }
    public String[] getFruit() {
        return fruit;
    }
    public void setFruit(String[] fruit) {
        this.fruit = fruit;
    }
}

以下是输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Query : Query: { "fruit" : { "$all" : [ "apple" , "banana"]}}, Fields: null, Sort: null
-------------------
ID : 1.0
Fruits : apple,banana,peach
-------------------
ID : 3.0
Fruits : cherry,banana,apple