spring使用mysql数据库实现关键字别字、拼音、拼音首字母、拼音所有首字母组合搜索


1、实现思路

前端传入的文字、拼音、别字、拼音首字母、拼音所有首字母组合传入到后台,通过后台接口转成拼
音,然后通过转换后的拼音结合sql语句查询匹配。

2、后台实现

pom配置:

<!--中文转拼音-->
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.0</version>
</dependency>

汉字转拼音实现类

package com.gis.utils;/**
 * Created by Administrator on 2024/5/8.
 */

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @ClassName PinyinUtils
 * @Description
 * @Author Administrator
 * @Date 2024/5/8 15:42
 * @Version V1.0
 */
public class PinyinUtils {

    /**
     * 汉字转拼音
     *
     * @param chinese   汉字
     * @return  拼音
     */
    public static String changeSpell(String chinese) {
        if (StringUtils.isEmpty(chinese)) {
            return chinese;
        }
        //将汉字参数去除空格后转化为数组
        char[] chineseArr = chinese.trim().toCharArray();
        //定义一个字符串
        StringBuilder spell = new StringBuilder();
        //输出格式
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        /**
         * 输出大小写设置
         * LOWERCASE:输出小写
         * UPPERCASE:输出大写
         */
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        /**
         * 输出音标设置
         * WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常)
         * WITH_TONE_NUMBER:1-4数字表示音标
         * WITHOUT_TONE:没有音标
         */
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        /**
         * 特殊音标ü设置
         * WITH_V:用v表示ü
         * WITH_U_AND_COLON:用"u:"表示ü
         * WITH_U_UNICODE:直接用ü
         */
        // format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        try {
            for (int i = 0; i < chineseArr.length; i++) {
                //判断是否是汉字
                if (Character.toString(chineseArr[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    //如果是多音字,返回多个拼音的数组
                    String[] pys = PinyinHelper.toHanyuPinyinStringArray(chineseArr[i], format);
                    //只取数组中的第一个
                    spell.append(pys[0]);
                } else {
                    //如果不是汉字直接拼接到spell中
                    spell.append(chineseArr[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return spell.toString();
    }
}

controller

 @GetMapping("/search")
public ResponseEntity<Object> search(String key){
	// 文字转拼音
   key = PinyinUtils.changeSpell(key);
   return new ResponseEntity<>(this.searchService.search(key))
}

3、sql查询语句

select name from tablename where to_pinyin(name) like '%key%' or fristPinyin(name) = 'key' or pinyin(name) = 'key'

4、to_pinyin、fristPinyin、pinyin为自定义函数

从下面三个本人博客获取
to_pinyin https://blog.csdn.net/qq_28419035/article/details/140456866
fristPinyin https://blog.csdn.net/qq_28419035/article/details/140544573
pinyin https://blog.csdn.net/qq_28419035/article/details/140544573

创作不易,希望能帮到您,发财的小手点点关注!

  • 64
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gis分享者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值