引起:org.springframework.data.mapping.Property

我是新手Redis并使用Spring Boot + Spring Data Redis示例。在这个例子中,我使用了QueryByExampleRedisExecutor<T>on repository 方法并使用ExampleAPI 来执行自定义查询。


以下是 Redis NoSQL DB 中存在的 KEYS。


redis 127.0.0.1:6379> KEYS *

 1) "country:76c78bcc-bb2a-41b3-a1fc-3dbb3042edd6:idx"

 2) "country:76c78bcc-bb2a-41b3-a1fc-3dbb3042edd6"

 3) "user:lastName:Kerr"

 4) "user"

 5) "user:role.roleName:API"

 6) "country:countryName:India"

 7) "Student:name:John Doe"

 8) "user:8252a4b3-22a1-4e6c-99fc-04fed93a21a5:idx"

 9) "user:8252a4b3-22a1-4e6c-99fc-04fed93a21a5"

10) "user:middleName:Lima"

11) "Student"

12) "user:middleName:Mike"

13) "user:firstName:John"

14) "country:countryCode:+91"

15) "user:role.roleName:ADMIN"

16) "user:lastName:Wixson"

17) "Student:1:idx"

18) "user:46ca0606-38a5-4d0c-8cea-38e2c4fa5bfa:idx"

19) "Student:2:idx"

20) "Student:1"

21) "user:firstName:Matt"

22) "user:46ca0606-38a5-4d0c-8cea-38e2c4fa5bfa"

23) "Student:2"

24) "country"

25) "Student:name:Michael Harford"

26) "country:84fc82f2-1c21-4d3e-9429-f1ee1039ceed:idx"

27) "country:84fc82f2-1c21-4d3e-9429-f1ee1039ceed"

redis 127.0.0.1:6379>

存储库方法 -


Example<User> example = Example.of(User.builder().firstName("Mike").build());

long count = userRepository.count();

System.out.println(count);

当我执行下面的方法时,我期望只得到一个结果,但我得到了两个结果。为什么 ?


此处显示的更多信息:考虑重新访问上面的条目或在您的配置中定义类型为“org.springframework.data.redis.core.RedisTemplate”的 bean


我们怎么能看到Redis snippet日志上的?我需要设置什么配置?


public interface UserRepository extends CrudRepository<User, String>, QueryByExampleExecutor<User>{

    List<User> findByFirstName(String firstName);


    List<User> findByFirstNameAndLastName(String firstName, String lastName);


    @Query("SELECT u FROM User u WHERE u.middleName LIKE :middleName ")

    List<User> findCustomByMiddleName(@Param("middleName") String middleName); 


    List<User> findByRole_RoleName(String roleName);

}


海绵宝宝撒
浏览 296回答 2
2回答

噜噜哒

您正在构建一个Example,然后通过不使用它而丢弃它。您正在调用userRepository.count();它基本上计算所有记录。您应该使用count带有Example.这个方法在QueryByExampleExecutor你的界面上,也UserRepository应该扩展。那么你可以简单地做。Example<User> example = Example.of(User.builder().firstName("Mike").build());long count = userRepository.count(example);System.out.println(count);你应该得到你期望的结果。

犯罪嫌疑人X

我遇到了类似的问题。解决方案是将 spring-data-redis 和 spring-data-common 包升级到相同版本。如果您使用的是 maven:&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.data</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-data-redis</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.4.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.data</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-data-commons</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.4.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java