简体   繁体   English

按示例进行Spring数据查询

[英]Spring data query by Example

I have object FilterData. 我有对象FilterData。

@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class FilterData implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id;

    @Size(max = 20)
    private String imsi;

    @Size(max = 10)
    private String mcc;

    @Size(max = 10)
    private String mnc;

    public FilterData(String imsi, String mcc, String mnc){
        this.imsi = imsi;
        this.mcc = mcc;
        this.mnc = mnc;
    }
}

repository: 库:

@RepositoryRestResource
public interface FilterDataRepository extends JpaRepository<FilterData, Long> {}

dependencies: 依赖关系:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>Project Demo</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Testing: 测试:

package com.example;

import com.example.orm.FilterData;
import com.example.repository.FilterDataRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public CommandLineRunner demo(FilterDataRepository filterDataRepository){
        return (args) -> {

            FilterData fd1 = new FilterData("12345", "219", "01");
            FilterData fd2 = new FilterData("12345", "219", "02");
            FilterData fd3 = new FilterData("56789", "218", "10");
            FilterData fd4 = new FilterData("56789", "222", "02");
            FilterData fd5 = new FilterData("10010", "443", "44");

            filterDataRepository.save(fd1);
            filterDataRepository.save(fd2);
            filterDataRepository.save(fd3);
            filterDataRepository.save(fd4);
            filterDataRepository.save(fd5);


            FilterData fd = new FilterData();
            fd.setMcc("218");

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withIncludeNullValues();

            Example<FilterData> ex = Example.of(fd, matcher);
            System.out.println(filterDataRepository.findAll()); // prints all

            System.out.println(filterDataRepository.count(ex)); // 0
            System.out.println(filterDataRepository.findOne(ex)); // null
            System.out.println(filterDataRepository.findAll(ex)); // [] empty
        };
    }
}

When I try “findAll()” I get all objects, but when I try query from example I can't get anything. 当我尝试“findAll()”时,我得到了所有对象,但是当我尝试从示例中查询时,我无法得到任何东西。

When I tried i was following spring data jpa query by example 当我尝试时,我通过示例跟随spring数据jpa查询

Any suggestion? 有什么建议吗?

Update your matcher to this: 将匹配器更新为:

ExampleMatcher matcher = ExampleMatcher.matching().withIncludeNullValues().withIgnorePaths("id","imsi", "mnc");

You are not getting anything because matcher is querying with all field match but as you are interested to only by mcc, you need to tell to ignore other fields. 你没有得到任何东西,因为matcher正在查询所有的字段匹配但是你只对mcc感兴趣,你需要告诉忽略其他字段。

The issue is due to the repository looking for the JPA entity ID, in this case it is looking for ID = 0. Also, the un-set values are null and need to be ignored in the matcher. 问题是由于存储库正在查找JPA实体ID,在这种情况下,它正在寻找ID = 0.此外,未设置的值为null,需要在匹配器中忽略。

You only need to ignore the identity field 您只需要忽略标识字段

ExampleMatcher.matching().withIgnoreNullValues()
    .withIgnorePaths("id");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM