java 中模糊查询

Python070

java 中模糊查询,第1张

可以通过拼where条件的方式模糊查询;

String where = “1=1”;

if(StringUtils.isBlank(custId)){

where = where+" CUSTID = '"+custID+"'"

}

if(StringUtils.isBlank(custname)){

where = where+" CUSTNAME = '"+custname+"'"

}

。。。。。。。

这只是一种模糊查询的方法,适用于按不确定的条件进行查询

可以使用正则表达式实现, 可以字符串本身的方法实现,请看示例:

import java.util.regex.Pattern

/**

 * @author Arvin

 * @time 2016/11/8 21:38

 */

public class Main {

    public static void main(String[] args) {

        String keyword = ".(你好)"

        String contentOne = "hello .(你好)asd" // LIKE 匹配

        String contentTwo = "你好" // LIKE 不匹配

        // 方法一: 利用正则表达式

        // 构造正则表达式

        Pattern regex = Pattern.compile(keyword)

        System.out.println(regex.matcher(contentOne).find()) // true

        System.out.println(regex.matcher(contentTwo).find()) // false

        // 方法二:利用String的contain方法

        System.out.println(contentOne.contains(keyword)) // true

        System.out.println(contentTwo.contains(keyword)) // false

        // 方法三:利用indexOf方法, 找得到说明包含

        System.out.println(contentOne.indexOf(keyword) > -1) // true

        System.out.println(contentTwo.indexOf(keyword) > -1) // false

    }

}