使用JPA框架时,用ExampleMatcher动态查询,但是根据打印出的sql语句来看,没有实现模糊查询的功能,最后发现时ExampleMatcher的写法有问题,下边时官网的写法:
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstname", match -> match.endsWith())
.withMatcher("firstname", match -> match.startsWith());
}
但是因为用了条件判断,生成matcher时,分开了来写的
ExampleMatcher matcher = ExampleMatcher.matching();
matcher.withMatcher(columnName, GenericPropertyMatchers.startsWith());
导致了不生效,因为matcher.withMatcher会返回一个新的matcher,不是开始实例化的了,所以换成以下:
ExampleMatcher matcher = ExampleMatcher.matching();
matcher = matcher.withMatcher(columnName, GenericPropertyMatchers.startsWith());