diff --git a/pom.xml b/pom.xml index aea2df30be2aaf1244e22a89408a37982fd9cec3..4bf3d66f9ec40e49e113e1c9987513946f27e19f 100644 --- a/pom.xml +++ b/pom.xml @@ -187,6 +187,11 @@ poi-ooxml 4.1.2 + + com.hankcs + hanlp + portable-1.8.3 + diff --git a/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java b/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java index 1f95cb80340523fd0be240cdf0c7076fd6327a98..c4f8d6efb5f650993fe544b613134aeb0cb7c2a0 100644 --- a/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java +++ b/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java @@ -1,8 +1,13 @@ package com.kwan.springbootkwan.filter; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import javax.servlet.*; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -14,6 +19,7 @@ import java.io.IOException; * @version : 2.2.0 * @date : 2023/7/9 22:54 */ +@Order(1) @Component public class CorsFilter implements Filter { @@ -25,7 +31,6 @@ public class CorsFilter implements Filter { httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); - chain.doFilter(request, response); } } diff --git a/src/main/java/com/kwan/springbootkwan/filter/MyFilter.java b/src/main/java/com/kwan/springbootkwan/filter/MyFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..74c4a13ae05fa1536f5535da90e65122224e6b8d --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/filter/MyFilter.java @@ -0,0 +1,31 @@ +package com.kwan.springbootkwan.filter; + +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import java.io.IOException; + + +/** + * 添加过滤器 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/8/26 11:06 + */ +@Component +@Order(2) // 设置过滤器的优先级为1,值越小,优先级越高 +public class MyFilter implements Filter { + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + // 过滤器逻辑 + chain.doFilter(request, response); + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java b/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..9d7ac0bc8bb4345c799b32db63765ca691917d13 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java @@ -0,0 +1,49 @@ +package com.kwan.springbootkwan.utils; + +import com.hankcs.hanlp.dictionary.py.Pinyin; +import com.hankcs.hanlp.dictionary.py.PinyinDictionary; +import org.apache.commons.lang3.StringUtils; +import org.springframework.util.CollectionUtils; + +import java.util.List; + + +/** + * 拼音工具类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/8/26 11:16 + */ +public class PinyinUtil { + + /** + * 获取中文完整拼音 + * + * @param chineseStr + * @return + */ + public static String getPinyin(String chineseStr) { + List pinyins = PinyinDictionary.convertToPinyin(chineseStr); + StringBuilder stringBuilder = new StringBuilder(); + for (Pinyin pinyin : pinyins) { + stringBuilder.append(pinyin.getPinyinWithoutTone()); + } + return stringBuilder.toString(); + } + + + /** + * 获取中文拼音首字母 + * + * @param chineseStr + * @return + */ + public static String getInitial(String chineseStr) { + List pinyins = PinyinDictionary.convertToPinyin(chineseStr); + if (CollectionUtils.isEmpty(pinyins)) { + return StringUtils.EMPTY; + } + return String.valueOf(pinyins.get(0).getPinyinWithoutTone().charAt(0)); + } +} diff --git a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java index b721ae18423f3ac25291a597ba964be757d9e83c..c202ae88f3d2ab789e269ced742eaaaebc1b1c8a 100644 --- a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java +++ b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java @@ -5,7 +5,7 @@ import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.mapper.UserMapper; import com.kwan.springbootkwan.service.ISendMsgHandle; import com.kwan.springbootkwan.utils.RedisIdWorker; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -129,7 +129,7 @@ public class UserServiceImplTest { } latch.await(); long end = System.currentTimeMillis(); - System.out.println("times = " + (end- begin)); + System.out.println("times = " + (end - begin)); } diff --git a/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java b/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java new file mode 100644 index 0000000000000000000000000000000000000000..45268601323e73d6feda8abb63f1dcea30029dae --- /dev/null +++ b/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java @@ -0,0 +1,19 @@ +package com.kwan.springbootkwan.utils; + +import com.kwan.springbootkwan.SpringBootKwanApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration(classes = SpringBootKwanApplication.class) +public class PinyinUtilTest { + + @Test + public void testName() { + assert "ceshi".equals(PinyinUtil.getPinyin("测试")); + } +} \ No newline at end of file