首页 > 解决方案 > Spring boot - 当我设置“用户名”变量时,JPA 匹配器不起作用

问题描述

我开始学习弹簧靴。我试了很多次都没有解决一个问题。用户类有用户名变量。我想检查用户名是否存在我们的数据库并将异常作为错误消息抛出。

 ExampleMatcher matcher = ExampleMatcher.matchingAll()
            .withMatcher("username", exact())
            .withIgnorePaths("id");

    Example<Users> example = Example.of(users, matcher);
    List<Users> find =  usersRepository.findAll(example);

    System.out.println("Find users: "+find.size());

我在匹配器上设置了用户名变量。当我运行 api 并显示日志时。你可以在下面看到:

User payload: Users{id=0, firstName='Homer', lastName='Simpsons', username='bart.skate', email='homer.sleeper@gmail.com', password='@bcd1234'}
Find users: 0
User payload: Users{id=0, firstName='Homer', lastName='Simpsons', username='bart.skate', email='homer.simpsons@gmail.com', password='@bcd1234'}
Find users: 0

第一个用户已创建。所以我尝试再次创建新用户相同的用户名。没有找到用户名。

我希望有人帮助为我解释。谢谢!

标签: springspring-bootjpa

解决方案


推荐阅读