ExampleMatcher 针对不同字段实现不同方式查询

在使用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版权协议,转载请附上原文出处链接和本声明。