当前位置:  开发笔记 > 编程语言 > 正文

按示例进行Spring数据查询

如何解决《按示例进行Spring数据查询》经验,为你挑选了1个好方法。

我有对象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 {}

依赖关系:



    4.0.0

    com.example
    my-project
    1.0
    jar

    Project Demo

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.2.RELEASE
         
    

    
        UTF-8

        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            com.h2database
            h2
        
        
            org.springframework.boot
            spring-boot-starter-data-rest
        

        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



测试:

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 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数据jpa查询

有什么建议吗?



1> Monzurul Haq..:

将匹配器更新为:

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

你没有得到任何东西,因为matcher正在查询所有的字段匹配但是你只对mcc感兴趣,你需要告诉忽略其他字段.

推荐阅读
臭小子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有