在使用ExampleMatcher时,有时候会碰到,查询时不同字段的查询方式不同,例如,想要对姓名实现前后模糊查询,同时对学号精确查询时。
示范代码:
Student student = new Student() ;
student.setStudentName(studentName);
student.setStudentCode(studentCode);
ExampleMatcher exampleMatcher = ExampleMatcher.matching()
//对name前后模糊查询
.withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
//对code精确查询
.withMatcher("code",ExampleMatcher.GenericPropertyMatchers.exact()) ;
studentPage = studentRepository.findAll(Example.of(student,exampleMatcher),PageRequest.of(page - 1, size)) ;
下面记录一下ExampleMatcher.GenericPropertyMatchers(通用属性匹配器)的查询方式
| 方法名 | 作用 |
|---|---|
| ignoreCase | 与字符串不区分大小写的匹配 |
| caseSensitive | 与字符串区分大小写的匹配 |
| contains | 与字符串模糊匹配,%{str}% |
| endsWith | 与字符串模糊匹配,%{str} |
| startsWith | 与字符串模糊匹配,{str}% |
| exact | 与字符串精确匹配 |
| storeDefaultMatching | 默认匹配模式 |
| regex | 将字符串视为正则表达式进行匹配 |
下面有一则更为详细的博客:https://blog.csdn.net/weixin_44216706/article/details/106496298
版权声明:本文为weixin_43481812原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。