废话不多说,直接上代码。
1、pom.xml 文件
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
2、新建IKAnalyzer.cfg.xml文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">local.dic;</entry>
<!--用户可以在这里配置自己的扩展停止词字典 -->
<entry key="ext_stopwords">stop.dic;</entry>
</properties>
3、新建local.dic 配置自己的扩展字典 和 stop.dic文件 配置自己的扩展停止词字典 内容举个例子
local.dic
正能量
微商
stop.dic
的
好
了
4、controller
public ReturnObject query(@RequestBody Map<String, Object> params) throws IOException {
logger.info("queryLoveWord: params data ===> " + JSON.toJSONString(params));
if (params.containsKey("keywords")) {
String keywords = MyUtil.removeDuplicateLetters(params.get("keywords").toString());
List<String> fvs = new ArrayList<>();
if(keywords.length() > 1){
fvs = MyUtil.cutWord(keywords);
}
fvs.add(0, params.get("keywords").toString());
fvs = MyUtil.removeDuplicateKeepOrder(fvs);
params.put("fvs", fvs);
}
PageInfo<LoveWord> p = loveWordService.query(params);
return new ReturnObject(ReturnCode.SUCCESS, p);
}
5、util
// 整合IKAnalyzer中文分词
public static List<String> cutWord(String msg) throws IOException {
StringReader sr = new StringReader(msg);
IKSegmenter ik = new IKSegmenter(sr, true);
Lexeme lex = null;
List<String> list = new ArrayList<>();
while((lex=ik.next()) != null){
list.add(lex.getLexemeText());
}
return list;
}
// 字符串去重并且顺序不变
public static String removeDuplicateLetters(String str){
if(StringUtils.isEmpty(str)){
return "";
}
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length(); i++){
char everyword = str.charAt(i);
int firstIndex = str.indexOf(everyword);
int lastIndex = str.lastIndexOf(everyword);
if(lastIndex == firstIndex || firstIndex == i){
sb.append(everyword);
}
}
return sb.toString();
}
/**
* 删除List中重复元素,并保持顺序
* @param list 待去重的list
* @return 去重后的list
*/
public static <T> List<T> removeDuplicateKeepOrder(List<T> list){
Set<T> set = new HashSet<T>();
List<T> newList = new ArrayList<>();
for (T element : list) {
//set能添加进去就代表不是重复的元素
if (set.add(element)){
newList.add(element);
}
}
list.clear();
list.addAll(newList);
return list;
}
6、mybatis
<!-- 获取列表 -->
<select id="query" parameterType="java.util.Map" resultType="com.hi.hailiaowenan.affair.bean.LoveWord">
select id, f_id as fId, f_id_name as fIdName, state,
create_time as createTime, update_time as updateTime
from
<include refid="table_name"></include>
<trim prefix="WHERE" prefixOverrides="AND | OR">
<!-- 上架状态 -->
<if test="state != null and state != '' or state == 0">
and state = #{state}
</if>
<!-- 审核状态 -->
<if test="active != null and active != '' or active == 0">
and active = #{active}
</if>
<if test="content != null and content != ''">
<!-- bind写法:预防SQL注入 匹配词语内容匹配 -->
<bind name="pattern" value="'%'+content+'%'" />
and content like #{pattern}
</if>
<if test="fIdName != null and fIdName != ''">
<!-- bind写法:预防SQL注入 匹配词语标题匹配 -->
<bind name="pattern" value="'%'+fIdName+'%'" />
and f_id_name like #{pattern}
</if>
<if test="fvs != null and fvs.size > 0">
and (
<foreach collection="fvs" open="(" close=")" separator="or" item="item">
content like CONCAT('%',#{item},'%') or f_id_name like CONCAT('%',#{item},'%') or title like CONCAT('%',#{item},'%')
</foreach>
)
</if>
</trim>
</select>
参考文章——
扫描二维码关注公众号,回复:
12900468 查看本文章
https://blog.csdn.net/tfstone/article/details/105785441
https://blog.csdn.net/chengyuqiang/article/details/102792839
拓展文章——IK Analyzer 热更新自定义词典
https://blog.csdn.net/litianxiang_kaola/article/details/103086088