小白式模糊查询(新手记录)
前端搜索框
<div class="nav navbar-nav navbar-right btn-group">
<form action="keywordSearch.do" method="get">
<input name="webpage" type="hidden" value="admin/secondLevel/timelong">
<input name="keyword" type="text" class="search-input" placeholder="请输入关键字">
<input class="search-btn" value="搜索" type="submit">
</form>
</div>
后端获取数据 vlounteerController
/**
* vlounteerController
* 模糊查询项目
* 给定网页、关键字 返回网页,并传递List<volunteer>
* @param webpage
* @param keyword
* @return modelAndView("webpage","listVolunteer",volunteerVO)
*/
@RequestMapping("keywordSearch.do")
public ModelAndView fex12(String webpage,String keyword){
ModelAndView modelAndView = new ModelAndView(webpage);
//关键字分割
String keywordSplit = KeyWordUtil.keywordSplit(keyword);
//模糊查询标题
List<VolunteerVO> volunteerVOS = volunteerService.dimSelectVolSynopsis(keywordSplit);
//模糊查询组织
List<VolunteerVO> volunteerVOS1 = volunteerService.dimSelectVolTisname(keywordSplit);
//模糊查询地点
List<VolunteerVO> volunteerVOS2 = volunteerService.dimSelectVolSite(keyword);
//将三个list集合装在一个里
volunteerVOS.addAll(volunteerVOS1);
volunteerVOS.addAll(volunteerVOS2);
//利用hashset集合去重
ArrayList<VolunteerVO> volunteerVO = new ArrayList<>(new HashSet<>(volunteerVOS));
//将volunteerVO装进modelandview中
modelAndView.addObject("listVolunteer",volunteerVO);
return modelAndView;
}
vlounteerController —引用—> 工具类 KeyWordUtil
public class KeyWordUtil {
/**
*
* @param keyWord -->全文索引
* @return str -->全|全文|全文索|全文索引|文索引|索引|引
*/
public static String keywordSplit(String keyWord) {
String s = CharacterUtils.filterIllegal(keyWord);
if (CharacterUtils.checkNameChese(s)) {//去除除数字,英文,汉字 之外的内容
//纯汉字
System.out.println(combine(s));
return combine(s);
} else if (CharacterUtils.isNumeric(s) || CharacterUtils.strIsEnglish(s)) {
//纯数字
//纯字母
System.out.println(s);
return s;
} else {
//都有
String filterEnglish = "";
String seperateWord = "";
String number = "";
try {
filterEnglish = combine(CharacterUtils.filterEnglish(s));//获取中文
} catch (StringIndexOutOfBoundsException A) {
System.out.println("KeyWordUtil.keywordSplit 提示: 该字符串中无中文 s=" + s);
}
try {
seperateWord = CharacterUtils.getSeperateWord(s);//获取英文
} catch (StringIndexOutOfBoundsException A) {
System.out.println("KeyWordUtil.keywordSplit 提示: 该字符串中无英文 s=" + s);
}
try {
number = CharacterUtils.getNumber(s);//获取数字
} catch (StringIndexOutOfBoundsException A) {
System.out.println("KeyWordUtil.keywordSplit 提示: 该字符串中无数字 s=" + s);
}
String str;
if (!("".equals(filterEnglish) || "".equals(seperateWord) || "".equals(number))) {
str = filterEnglish + "|" + seperateWord + "|" + number;
} else if (!("".equals(filterEnglish) || "".equals(seperateWord))) {
str = filterEnglish + "|" + seperateWord;
} else if (!("".equals(filterEnglish) || "".equals(number))) {
str = filterEnglish + "|" + number;
} else if (!("".equals(seperateWord) || "".equals(number))) {
str = seperateWord + "|" + number;
} else {
str = "不";
}
return str;
}
}
//遍历字符可能性
public static String combine(String string) {
StringBuilder sbd = new StringBuilder();
for (int i = 0; i <= string.length(); i++) {
for (int j = i + 1; j <= string.length(); j++) {
String substring = string.substring(i, j);
sbd.append(substring);
if (!((i == string.length() - 1) && (j == string.length()))) {
sbd.append("|");
}
}
}
return sbd.toString();
}
}
工具类 KeyWordUtil —引用—> 字符处理工具类 CharacterUtils
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharacterUtils {
/**
* 校验一个字符是否是汉字
* @param c 被校验的字符
* @return true代表是汉字
*/
public static boolean isChineseChar(char c) {
try {
return String.valueOf(c).getBytes("UTF-8").length > 1;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
}
}
/**
* 验证字符串内容是否包含下列非法字符<br>
* `~!#%^&*=+\\|{};:'\",<>/?○●★☆☉♀♂※¤╬の〆
*
* @param content 字符串内容
* @return 't'代表不包含非法字符,otherwise代表包含非法字符。
*/
public static char validateLegalString(String content) {
String illegal = "`~!#%^&*=+\\|{};:'\",<>/?○●★☆☉♀♂※¤╬の〆";
char isLegalChar = 't';
Code1:
for (int i = 0; i < content.length(); i++) {//L1定义代码块L1
for (int j = 0; j < illegal.length(); j++) {
if (content.charAt(i) == illegal.charAt(j)) {
isLegalChar = content.charAt(i);
break Code1;
}
}
}
return isLegalChar;
}
/**
* 验证是否是汉字或者0-9、a-z、A-Z
*
* @param c 被验证的char
* @return true代表符合条件
*/
public static boolean isRightChar(char c) {
return isChinese(c) || isWord(c);
}
/**
* 校验某个字符是否是a-z、A-Z、_、0-9
*
* @param c 被校验的字符
* @return true代表符合条件
*/
public static boolean isWord(char c) {
String regEx = "[\\w]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher("" + c);
return m.matches();
}
/**
* 判定输入的是否是汉字
*
* @param c 被校验的字符
* @return true代表是汉字
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 校验String是否全是中文
*
* @param name 被校验的字符串
* @return true代表全是汉字
*/
public static boolean checkNameChese(String name) {
boolean res = true;
char[] cTemp = name.toCharArray();//转换为数组
for (int i = 0; i < name.length(); i++) {
if (!isChinese(cTemp[i])) {//逐个判断是否为中文
res = false;
break;
}
}
return res;
}
/**
* 获取字符串中的中文
*
* @param str 任意字符串
* @return strz 过滤掉英文后的字符串
*/
public static String filterEnglish(String str) {
return str.replaceAll("[^\\u4e00-\\u9fa5]", "");
}
/**
* 去除数字,英文,汉字 之外的内容
*
* @param str
* @return
*/
public static String filterIllegal(String str) {
return str.replaceAll("[^a-zA-Z0-9\\u4E00-\\u9FA5]", ""); //去除数字,英文,汉字 之外的内容
}
/**
* 判断字符串是否为正整数
*
* @param string
* @return
*/
public static boolean isNumeric(String string) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(string).matches();
}
/**
* 判断字符串为存纯英文
* @param word
* @return
*/
public static boolean strIsEnglish(String word) {
boolean sign = true; // 初始化标志为为'true'
for (int i = 0; i < word.length(); i++) {
if (!(word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') && !(word.charAt(i) >= 'a' && word.charAt(i) <= 'z')) {
return false;
}
}
return true;
} //正则 String str ="abssdf"; str.matches("^[a-zA-Z]*");
/**
* 获取字符串中的单词
*
* @param str
* @return
*/
public static String getSeperateWord(String str) {
String pattern = "[a-zA-Z]+('?[a-zA-Z])?";
StringBuilder sbd = new StringBuilder();
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
while(m.find()) {
sbd.append(m.group()).append("|");
}
return sbd.deleteCharAt(sbd.length() - 1).toString();
}
/**
* 获取字符串中的数字
* @param str
* @return
*/
public static String getNumber(String str){
String regex = "\\d+";
StringBuilder sbd = new StringBuilder();
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(str);
while(m.find()) {
sbd.append(m.group()).append("|");
}
return sbd.deleteCharAt(sbd.length() - 1).toString();
}
}
我们通过工具类就能得到
@param keyWord --> 全文索引
@return str --> 全|全文|全文索|全文索引|文索引|索引|引
使用 KeyWordUtil.keywordSplit(String keyWord)得到的字符串
DAO层代码
@Override
public List<VolunteerVO> dimSelectVolSynopsis(String keyword) {
String sql="select * from T_VOLUNTEER where regexp_like(synopsis,?)";
//将String keyWord传入 ?(占位符) 中就可得到t_volunteer表中Synopsis字段所有模糊查询结果
List<VolunteerVO> query = jdbcTemplate.query(sql, //指定SQL语句
new VolunteerRowMapper()//方法如下封装List<VlunteerVO>
, keyword);//占位符设参
return query;
}
class VolunteerRowMapper implements RowMapper<VolunteerVO>{
@Override
public VolunteerVO mapRow(ResultSet resultSet, int i) throws SQLException {
VolunteerVO volunteer=new VolunteerVO();
volunteer.setVolid(resultSet.getInt("volid"));
volunteer.setPlate(resultSet.getString("plate"));
volunteer.setSite(resultSet.getString("site"));
volunteer.setRelease(resultSet.getDate("release"));
volunteer.setVolimage(resultSet.getString("volimage"));
volunteer.setStarttime(resultSet.getDate("starttime"));
volunteer.setEndtime(resultSet.getDate("endtime"));
volunteer.setFre(resultSet.getInt("fre"));
volunteer.setContacts(resultSet.getString("contacts"));
volunteer.setVoltel(resultSet.getString("voltel"));
volunteer.setVolsite(resultSet.getString("volsite"));
volunteer.setSynopsis(resultSet.getString("synopsis"));
volunteer.setContent(resultSet.getString("content"));
volunteer.setClaim(resultSet.getString("claim"));
volunteer.setNbpeople(resultSet.getInt("nbpeople"));
volunteer.setStatus(resultSet.getString("status"));
return volunteer;
}
}
来源:CSDN
作者:迷途小白菜
链接:https://blog.csdn.net/weixin_45494831/article/details/103650855