关键字搜索

思路:

        思路1:SQL中多个or筛选

        思路2:Java中stream流anyMatch筛选

mysql 

-- keyword: 关键字
-- column1,column2,column3: 需要筛选的字段
select * 
from user 
where 
column1 rlike/regexp #{keyword} or
column2 rlike/regexp #{keyword} or
column3 rlike/regexp #{keyword}

postgresql

-- keyword: 关键字
-- column1,column2,column3: 需要筛选的字段
-- ~*表示忽略大小写
select * 
from user 
where 
column1 ~* #{keyword} or
column2 ~* #{keyword} or
column3 ~* #{keyword}

Java

List list = new ArrayList<>();
list.add(new Student("s1", "123"));
list.add(new Student("b1", "s123"));
String keyword = "s";
// 筛选name,password字段,可自己选择
list = list.stream()
      .filter(s -> Stream.of(s.getName(), s.getPassword()).anyMatch(field -> StringUtils.isNotEmpty(field) && field.toLowerCase(Locale.ROOT).
contains(keyword.toLowerCase(Locale.ROOT)))).collect(Collectors.toList());

你可能感兴趣的:(java,postgresql,mysql)