我有对象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;
}
}
存储库:
@RepositoryRestResource
public interface FilterDataRepository extends JpaRepository<FilterData, Long> {}
依赖项:
<?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>
测试:
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
};
}
}
当我尝试“findAll()”时,我得到了所有对象,但是当我尝试从示例中查询时,我无法得到任何东西。
当我尝试时,我正在关注spring data jpa query by example
有什么建议吗?
答案 0 :(得分:8)
将匹配器更新为:
ExampleMatcher matcher = ExampleMatcher.matching().withIncludeNullValues().withIgnorePaths("id","imsi", "mnc");
你没有得到任何东西,因为matcher正在查询所有的字段匹配,但由于你只对mcc感兴趣,你需要告诉忽略其他字段。
答案 1 :(得分:2)
问题是由于存储库正在查找JPA实体ID,在这种情况下,它正在寻找ID = 0.此外,未设置的值为null,需要在匹配器中忽略。
您只需忽略身份字段
ExampleMatcher.matching().withIgnoreNullValues()
.withIgnorePaths("id");