{"id":562,"date":"2025-02-09T15:25:48","date_gmt":"2025-02-09T07:25:48","guid":{"rendered":"http:\/\/blog.heartwarming.online\/?p=562"},"modified":"2025-02-09T15:33:54","modified_gmt":"2025-02-09T07:33:54","slug":"springboot%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%9f%ba%e4%ba%8e%e7%94%a8%e6%88%b7%e7%9a%84%e5%8d%8f%e5%90%8c%e8%bf%87%e6%bb%a4%e6%8e%a8%e8%8d%90%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.heartwarming.online\/index.php\/2025\/02\/09\/springboot%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%9f%ba%e4%ba%8e%e7%94%a8%e6%88%b7%e7%9a%84%e5%8d%8f%e5%90%8c%e8%bf%87%e6%bb%a4%e6%8e%a8%e8%8d%90%e7%ae%97%e6%b3%95\/","title":{"rendered":"SpringBoot\u4e2d\u5b9e\u73b0\u57fa\u4e8e\u7528\u6237\u7684\u534f\u540c\u8fc7\u6ee4\u63a8\u8350\u7b97\u6cd5"},"content":{"rendered":"\n<p>\u5927\u4e8c\u7684\u65f6\u5019\u5c31\u60f3\u641e\u4e00\u4e0b\u63a8\u8350\u7b97\u6cd5\u4e86\uff0c\u4f46\u662f\u90a3\u65f6\u5019\u4e0d\u60f3\u5012\u817e\uff0c\u52a0\u4e0a\u6ca1\u5565\u53c2\u8003\u8d44\u6599\uff0c\u5c0f\u7834\u7ad9\u4e0a\u4e5f\u6ca1\u6709\u5565\u6559\u7a0b\uff0c\u5168\u662f\u6253\u5e7f\u544a\u63a8\u9500\u5356\u4f5c\u54c1\u7684\uff0c\u5927\u4e09\u53c8\u5728\u8003\u7814\uff0c\u6240\u4ee5\u5c31\u6ca1\u65f6\u95f4\u641e\uff0c\u73b0\u5728\u81ea\u5df1\u7684\u6bd5\u8bbe\u91cc\u8981\u52a0\uff0c\u6728\u6709\u529e\u6cd5\uff0c\u81ea\u5df1\u4e30\u8863\u8db3\u98df\uff0c\u901a\u8fc7\u67e5\u8d44\u6599\uff0c\u7406\u89e3\u522b\u4eba\u7684\u4ee3\u7801\uff0c\u81ea\u5df1\u4e5f\u7b97\u7b80\u5355\u5b9e\u73b0\u4e86\u4e00\u4e0b\uff0c\u8fd8\u6709\u66f4\u590d\u6742\u7684\u534f\u540c\u8fc7\u6ee4\u7b97\u6cd5\uff0c\u73b0\u5728\u81ea\u5df1\u8fd8\u6ca1\u6709\u7406\u89e3\uff0c\u540e\u9762\u6709\u65f6\u95f4\u518d\u7406\u89e3\u6d88\u5316\u4e00\u4e0b\ud83d\ude02\u3002\u8bdd\u4e0d\u591a\u8bf4\uff0c\u5f00\u5e72\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b80\u5355\u4ecb\u7ecd<\/h2>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u57fa\u4e8e\u7528\u6237\u7684\u534f\u540c\u8fc7\u6ee4\u63a8\u8350\u7b97\u6cd5\u662f\u57fa\u4e8e\u7528\u6237\u7684\u884c\u4e3a\u53bb\u8fdb\u884c\u63a8\u8350\uff0c\u6211\u8fd9\u91cc\u662f\u57fa\u4e8e\u7528\u6237\u8bc4\u5206\u8fd9\u4e2a\u884c\u4e3a\u53bb\u8fc7\u6ee4\u63a8\u8350\u7684\uff0c\u6211\u8fd9\u91cc\u7684\u4f8b\u5b50\u662f\uff0c\u8d34\u5427\u91cc\u9762\u6709\u5f88\u591a\u5e16\u5b50\uff0c\u7528\u6237\u53ef\u4ee5\u5bf9\u5e16\u5b50\u8fdb\u884c\u8bc4\u5206\uff0c\u6211\u7cfb\u7edf\u91cc\u9700\u8981\u5b9e\u73b0\u7ed9\u7528\u6237\u63a8\u8350\u5e16\u5b50\u3002\u9996\u5148\u80af\u5b9a\u5c31\u5f97\u7528\u4e00\u5f20\u8868\u5b58\u50a8\u7528\u6237\u5bf9\u5e16\u5b50\u7684\u8bc4\u5206\u60c5\u51b5\uff0c\u5373stick_score:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-4.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"529\" data-original=\"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-563\" style=\"width:284px;height:auto\"  sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/div><\/figure>\n\n\n\n<p>\u6574\u4e2a\u6d41\u7a0b\u5c31\u662f\uff0c\u9996\u5148\u6839\u636estick_score\u8868\u4e2d\u7684\u6570\u636e\u627e\u51fa\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\uff0c\u628a\u4ed6\u4eec\u7684id\u5b58\u4e0b\u6765\uff0c\u7136\u540e\u6839\u636e\u4ed6\u4eec\u7684id\u518d\u56de\u5230stick_score\u4e2d\u627e\u51fa\u76ee\u6807\u7528\u6237\u6ca1\u8bc4\u5206\u4f46\u662f\u76f8\u4f3c\u7528\u6237\u8bc4\u8fc7\u5206\u7684\u5e16\u5b50id\uff0c\u7136\u540e\u6839\u636e\u63a8\u8350\u6b21\u6570\u63a8\u8350\u7ed9\u76ee\u6807\u7528\u6237\uff08\u5f53\u7136\u8fd9\u91cc\u662f\u9ed8\u8ba4\u6709\u8fd9\u79cd\u60c5\u51b5\uff0c\u6ca1\u6709\u7684\u8bdd\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u9700\u6c42\u53bb\u63a8\u8350\uff0c\u4e0b\u9762\u4f1a\u8bb2\u8ff0\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">No.1 \u63a8\u8350\u6a21\u5757\u7684\u5c01\u88c5<\/h2>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u7cfb\u7edf\u8fc7\u6ee4\u63a8\u8350\u7b97\u6cd5\u6838\u5fc3\u5b9e\u73b0\u4ee3\u7801\u8fdb\u884c\u4e00\u4e2a\u5c01\u88c5\uff0c\u653e\u5728\u5de5\u5177\u7c7b\u91cc\u8fdb\u884c\u5b9e\u73b0\uff0c\u6bd4\u5982\uff0c\u5728\u6211\u8fd9\uff0c\u9700\u8981\u5c01\u88c5\u4e00\u4e2a\u5e16\u5b50\u63a8\u8350\u6a21\u5757\uff0c\u90a3\u5c31\u5728common\u7684\u5de5\u5177\u7c7b\u91cc\u5c01\u88c5\u4e00\u4e2aStickRecommend\u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-5.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"916\" data-original=\"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-5.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-565\" style=\"width:360px;height:auto\"  sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/div><\/figure>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0b\u8fd9\u4e2a\u6838\u5fc3\u4ee3\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<p>1.<strong>\u52a0\u8f7d\u7528\u6237\u8bc4\u5206\u6570\u636e\uff08<code>loadRatingsFromDB<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u8fd9\u90e8\u5206\u7684\u4f5c\u7528\u5c31\u662f\u9884\u5904\u7406\u6570\u636e\u5e93stick_score\u91cc\u9762\u7684\u6570\u636e\uff0c\u5c06\u5404\u90e8\u5206\u6570\u636e\u6620\u5c04\u6210<code>Map&lt;Integer, Map&lt;Integer, Double>><\/code>\u00a0\u6570\u636e\u7ed3\u6784\uff0c\u65b9\u4fbf\u540e\u7eed\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ \u52a0\u8f7d\u7528\u6237\u8bc4\u5206\u6570\u636e\u7684\u65b9\u6cd5\n    \/\/ \u8be5\u65b9\u6cd5\u4ece\u6570\u636e\u5e93\u4e2d\u52a0\u8f7d\u7528\u6237\u5bf9\u5404\u4e2a\u5e16\u5b50\uff08stick\uff09\u7684\u8bc4\u5206\u6570\u636e\n    \/\/ \u8fd4\u56de\u4e00\u4e2aMap\uff0c\u5176\u4e2dkey\u662f\u7528\u6237ID\uff0cvalue\u662f\u4e00\u4e2aMap\uff0c\u5b58\u50a8\u8be5\u7528\u6237\u5bf9\u5404\u4e2a\u5e16\u5b50\u7684\u8bc4\u5206\n    public static Map&lt;Integer, Map&lt;Integer, Double>> loadRatingsFromDB(Connection conn) throws SQLException {\n        Map&lt;Integer, Map&lt;Integer, Double>> userRatings = new HashMap&lt;>();\n\n        \/\/ \u4f7f\u7528Statement\u6267\u884cSQL\u67e5\u8be2\uff0c\u4ece\u6570\u636e\u5e93\u8868 \"stick_score\" \u83b7\u53d6\u7528\u6237\u8bc4\u5206\u6570\u636e\n        try (Statement stmt = conn.createStatement();\n             ResultSet rs = stmt.executeQuery(\"SELECT user_id, stick_id, score FROM stick_score\")) {\n            \/\/ \u904d\u5386\u67e5\u8be2\u7ed3\u679c\u96c6\uff0c\u5c06\u6570\u636e\u6309\u7167\u7528\u6237ID\u548c\u5e16\u5b50ID\u7ec4\u7ec7\u6210Map\n            while (rs.next()) {\n                int userId = rs.getInt(\"user_id\");  \/\/ \u83b7\u53d6\u7528\u6237ID\n                int stickId = rs.getInt(\"stick_id\");  \/\/ \u83b7\u53d6\u5e16\u5b50ID\n                double rating = rs.getDouble(\"score\");  \/\/ \u83b7\u53d6\u8bc4\u5206\n\n                \/\/ \u5c06\u8bc4\u5206\u6570\u636e\u5b58\u50a8\u5230Map\u4e2d\uff0cMap\u7684\u7ed3\u6784\u662f\uff1a\u7528\u6237ID -> (\u5e16\u5b50ID -> \u8bc4\u5206)\n                userRatings.computeIfAbsent(userId, k -> new HashMap&lt;>()).put(stickId, rating);\n            }\n        }\n        return userRatings;  \/\/ \u8fd4\u56de\u5b58\u50a8\u8bc4\u5206\u6570\u636e\u7684Map\n    }<\/code><\/pre>\n\n\n\n<p>2.<strong>\u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff08<code>cosineSimilarity<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u8fd9\u90e8\u5206\u5c31\u662f\u57fa\u4e8e\u4e24\u4e2a\u7528\u6237\u5bf9\u76f8\u540c\u5e16\u5b50\u7684\u8bc4\u5206\u8ba1\u7b97\u4ed6\u4eec\u7684\u76f8\u4f3c\u5ea6\u3002\u4f7f\u7528\u4f59\u5f26\u76f8\u4f3c\u5ea6\u516c\u5f0f\uff0c\u8861\u91cf\u4e24\u4e2a\u7528\u6237\u8bc4\u5206\u6a21\u5f0f\u4e4b\u95f4\u7684\u76f8\u4f3c\u7a0b\u5ea6\uff0c\u8d8a\u63a5\u8fd11\u8bf4\u660e\u8d8a\u76f8\u4f3c\u3002\u5927\u81f4\u7684\u8fc7\u7a0b\u5c31\u662f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1.\u8ba1\u7b97\u4e24\u4e2a\u7528\u6237\u5bf9\u5171\u540c\u5e16\u5b50\u7684\u8bc4\u5206\u7684\u70b9\u79ef\uff08<code>dotProduct<\/code>\uff09\u3002<\/p>\n\n\n\n<p>2.\u8ba1\u7b97\u6bcf\u4e2a\u7528\u6237\u7684\u8bc4\u5206\u7684\u5e73\u65b9\u548c\u7684\u5e73\u65b9\u6839\uff08<code>normA<\/code>\u00a0\u548c\u00a0<code>normB<\/code>\uff09\u3002<\/p>\n\n\n\n<p>3.\u8fd4\u56de\u70b9\u79ef\u4e0e\u4e24\u4e2a\u8bc4\u5206\u5411\u91cf\u6a21\u957f\u7684\u5546\uff0c\u5373\u5f97\u5230\u7528\u6237\u4e4b\u95f4\u7684\u4f59\u5f26\u76f8\u4f3c\u5ea6\u3002<\/p>\n\n\n\n<p>\u8bf4\u660e\uff1a\u4f59\u5f26\u76f8\u4f3c\u5ea6\u7684\u8303\u56f4\u662f [\u22121,1][\u22121,1]\uff0c\u5176\u4e2d 1 \u8868\u793a\u5b8c\u5168\u76f8\u4f3c\uff0c0 \u8868\u793a\u6ca1\u6709\u76f8\u4f3c\u5ea6\uff0c-1 \u8868\u793a\u5b8c\u5168\u4e0d\u76f8\u4f3c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ \u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\n    \/\/ \u8be5\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u7528\u6237\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u57fa\u4e8e\u4ed6\u4eec\u5bf9\u76f8\u540c\u5e16\u5b50\u7684\u8bc4\u5206\u8fdb\u884c\u8ba1\u7b97\n    \/\/ \u4f59\u5f26\u76f8\u4f3c\u5ea6\u7684\u503c\u8d8a\u5927\uff0c\u8bf4\u660e\u4e24\u4e2a\u7528\u6237\u7684\u5174\u8da3\u8d8a\u76f8\u4f3c\n    public static double cosineSimilarity(Map&lt;Integer, Double> ratings1, Map&lt;Integer, Double> ratings2) {\n        \/\/ \u83b7\u53d6\u4e24\u4e2a\u7528\u6237\u7684\u8bc4\u5206\u7684\u5171\u540c\u5e16\u5b50ID\n        Set&lt;Integer> commonBooks = new HashSet&lt;>(ratings1.keySet());\n        commonBooks.retainAll(ratings2.keySet());  \/\/ \u6c42\u4ea4\u96c6\uff0c\u627e\u5230\u5171\u540c\u8bc4\u5206\u7684\u5e16\u5b50\n\n        \/\/ \u5982\u679c\u4e24\u4e2a\u7528\u6237\u6ca1\u6709\u5171\u540c\u8bc4\u5206\u7684\u5e16\u5b50\uff0c\u76f8\u4f3c\u5ea6\u4e3a0\n        if (commonBooks.isEmpty()) return 0;\n\n        double dotProduct = 0.0;  \/\/ \u70b9\u79ef\n        double normA = 0.0;  \/\/ \u5411\u91cfA\u7684\u6a21\n        double normB = 0.0;  \/\/ \u5411\u91cfB\u7684\u6a21\n\n        \/\/ \u8ba1\u7b97\u70b9\u79ef\u548c\u5404\u81ea\u5411\u91cf\u7684\u6a21\n        for (int stickId : commonBooks) {\n            double rating1 = ratings1.get(stickId);  \/\/ \u7528\u62371\u5bf9\u8be5\u5e16\u5b50\u7684\u8bc4\u5206\n            double rating2 = ratings2.get(stickId);  \/\/ \u7528\u62372\u5bf9\u8be5\u5e16\u5b50\u7684\u8bc4\u5206\n            dotProduct += rating1 * rating2;  \/\/ \u8ba1\u7b97\u70b9\u79ef\n            normA += Math.pow(rating1, 2);  \/\/ \u8ba1\u7b97\u7528\u62371\u8bc4\u5206\u7684\u5e73\u65b9\u548c\n            normB += Math.pow(rating2, 2);  \/\/ \u8ba1\u7b97\u7528\u62372\u8bc4\u5206\u7684\u5e73\u65b9\u548c\n        }\n\n        \/\/ \u8ba1\u7b97\u5e76\u8fd4\u56de\u4f59\u5f26\u76f8\u4f3c\u5ea6\n        return dotProduct \/ (Math.sqrt(normA) * Math.sqrt(normB));\n    }<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u8865\u5145\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-6-1024x135.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"135\" data-original=\"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-6-1024x135.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-566\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ai<em>a<\/em><em>i<\/em>\u200b\u00a0\u548c\u00a0bi<em>b<\/em><em>i<\/em>\u200b\u00a0\u662f\u7528\u6237\u5bf9\u5e16\u5b50\u7684\u8bc4\u5206\u3002<\/li>\n\n\n\n<li>\u5206\u5b50\u662f\u4e24\u4e2a\u7528\u6237\u5bf9\u5171\u540c\u5e16\u5b50\u7684\u8bc4\u5206\u7684\u70b9\u79ef\uff08\u5373\u8bc4\u5206\u503c\u76f8\u4e58\u518d\u6c42\u548c\uff09\u3002<\/li>\n\n\n\n<li>\u5206\u6bcd\u662f\u4e24\u4e2a\u7528\u6237\u8bc4\u5206\u7684\u6a21\u957f\uff0c\u5373\u8bc4\u5206\u5e73\u65b9\u548c\u7684\u5e73\u65b9\u6839\u3002<\/li>\n<\/ul>\n\n\n\n<p>3.<strong>\u627e\u5230\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\uff08<code>findTopThreeSimilarUsers<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u8fd9\u91cc\u663e\u7136\u5f88\u5173\u952e\uff0c\u53ea\u6709\u627e\u5230\u4e86\u548c\u76ee\u6807\u7528\u6237\u76f8\u4f3c\u7684\u7528\u6237\uff0c\u624d\u53ef\u4ee5\u8fdb\u884c\u76f8\u5173\u63a8\u8350\uff0c\u63a8\u8350\u63a8\u8350\uff0c\u548c\u4f60\u76f8\u7c7b\u4f3c\u7684\u4eba\u505a\u8fd9\u4e2a\uff0c\u5c31\u53ef\u4ee5\u731c\u6d4b\u4f60\u4e5f\u53ef\u80fd\u505a\u8fd9\u4e2a\uff0c\u6211\u5c31\u63a8\u7ed9\u4f60\uff0c\u5176\u5b9e\u6211\u611f\u89c9\u6296\u97f3\u6709\u65f6\u5c31\u662f\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u7684\u597d\u53cb\u70b9\u8d5e\u4e86\u5565\uff0c\u8bf6\u4ed6\u5c31\u63a8\u7ed9\u4f60\u4e86\u3002\u8fd9\u91cc\u5927\u81f4\u7684\u6d41\u7a0b\u5c31\u662f\u904d\u5386\u6240\u6709\u7528\u6237\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u7528\u6237\u4e0e\u76ee\u6807\u7528\u6237\u7684\u76f8\u4f3c\u5ea6\uff0c\u627e\u5230\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\u3002\u4f7f\u7528\u4f18\u5148\u961f\u5217\uff08\u6700\u5927\u5806\uff09\u5b58\u50a8\u548c\u6392\u5e8f\u76f8\u4f3c\u5ea6\uff0c\u786e\u4fdd\u53ea\u4fdd\u7559\u6700\u76f8\u4f3c\u7684\u4e09\u4f4d\u7528\u6237\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/ \u627e\u5230\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n    \/\/ \u8be5\u65b9\u6cd5\u901a\u8fc7\u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\u6765\u627e\u5230\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n    public static List&lt;Integer> findTopThreeSimilarUsers(int targetUserId, Map&lt;Integer, Map&lt;Integer, Double>> userRatings) {\n        \/\/ \u4f7f\u7528\u4f18\u5148\u961f\u5217\uff08\u6700\u5927\u5806\uff09\u5b58\u50a8\u7528\u6237\u76f8\u4f3c\u5ea6\u548c\u7528\u6237ID\n        PriorityQueue&lt;Map.Entry&lt;Integer, Double>> pq = new PriorityQueue&lt;>(\n                (e1, e2) -> Double.compare(e1.getValue(), e2.getValue())  \/\/ \u6309\u76f8\u4f3c\u5ea6\u5347\u5e8f\u6392\u5e8f\n        );\n\n        \/\/ \u904d\u5386\u6240\u6709\u7528\u6237\uff0c\u8ba1\u7b97\u4e0e\u76ee\u6807\u7528\u6237\u7684\u76f8\u4f3c\u5ea6\n        for (Map.Entry&lt;Integer, Map&lt;Integer, Double>> entry : userRatings.entrySet()) {\n            int userId = entry.getKey();  \/\/ \u7528\u6237ID\n            if (userId == targetUserId) continue;  \/\/ \u8df3\u8fc7\u76ee\u6807\u7528\u6237\u672c\u8eab\n\n            \/\/ \u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\n            double similarity = cosineSimilarity(userRatings.get(targetUserId), entry.getValue());\n            \/\/ \u5c06\u7528\u6237\u548c\u76f8\u4f3c\u5ea6\u5b58\u5165\u4f18\u5148\u961f\u5217\n            Map.Entry&lt;Integer, Double> user = new AbstractMap.SimpleEntry&lt;>(userId, similarity);\n            pq.offer(user);\n\n            \/\/ \u5982\u679c\u961f\u5217\u4e2d\u7684\u7528\u6237\u6570\u8d85\u8fc73\u4e2a\uff0c\u79fb\u9664\u6700\u4e0d\u76f8\u4f3c\u7684\u7528\u6237\n            if (pq.size() > 3) {\n                pq.poll();\n            }\n        }\n\n        \/\/ \u4ece\u4f18\u5148\u961f\u5217\u4e2d\u63d0\u53d6\u51fa\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n        List&lt;Integer> topUsers = new ArrayList&lt;>();\n        while (!pq.isEmpty()) {\n            topUsers.add(pq.poll().getKey());  \/\/ \u63d0\u53d6\u7528\u6237ID\n        }\n        return topUsers;  \/\/ \u8fd4\u56de\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\u7684ID\n    }<\/code><\/pre>\n\n\n\n<p>4.<strong>\u57fa\u4e8e\u76f8\u4f3c\u7528\u6237\u63a8\u8350\u5e16\u5b50\uff08<code>recommendStickOnSimilarUsers<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u4e07\u4e8b\u5177\u5907\u4e86\uff0c\u53ef\u4ee5\u63a8\u8350\u4e86\u3002\u8fd9\u91cc\u5c31\u662f\u6839\u636e\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u51e0\u4e2a\u7528\u6237\u7684\u8bc4\u5206\uff0c\u63a8\u8350\u76ee\u6807\u7528\u6237\u53ef\u80fd\u611f\u5174\u8da3\u7684\u5e16\u5b50\u3002\u5982\u679c\u76f8\u4f3c\u7528\u6237\u8bc4\u5206\u8fc7\u7684\u5e16\u5b50\u76ee\u6807\u7528\u6237\u672a\u8bc4\u5206\uff0c\u5219\u63a8\u8350\u8be5\u5e16\u5b50\u3002\u6839\u636e\u63a8\u8350\u6b21\u6570\u6392\u5e8f\uff0c\u8fd4\u56de\u6700\u53d7\u6b22\u8fce\u7684\u5e16\u5b50\u3002\u5f53\u7136\u91cc\u9762\u63a8\u8350\u591a\u5c11\uff0c\u90fd\u53ef\u4ee5\u81ea\u5df1\u5fae\u8c03\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/ \u57fa\u4e8e\u76f8\u4f3c\u7528\u6237\u63a8\u8350\u5e16\u5b50\n    \/\/ \u8be5\u65b9\u6cd5\u57fa\u4e8e\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u7528\u6237\u6765\u63a8\u8350\u5e16\u5b50\n    public static List&lt;Map.Entry&lt;Integer, Integer>> recommendStickOnSimilarUsers(Map&lt;Integer, Map&lt;Integer, Double>> userRatings,\n                                                                                 List&lt;Integer> similarUsers, int targetUserId) {\n        Map&lt;Integer, Integer> stickVotes = new HashMap&lt;>();  \/\/ \u5b58\u50a8\u6bcf\u4e2a\u5e16\u5b50\u7684\u63a8\u8350\u6b21\u6570\n\n        \/\/ \u904d\u5386\u6240\u6709\u76f8\u4f3c\u7528\u6237\uff0c\u7edf\u8ba1\u4ed6\u4eec\u63a8\u8350\u8fc7\u7684\u5e16\u5b50\n        for (int userId : similarUsers) {\n            Map&lt;Integer, Double> userBooks = userRatings.getOrDefault(userId, Collections.emptyMap());\n            for (Map.Entry&lt;Integer, Double> bookRating : userBooks.entrySet()) {\n                \/\/ \u5982\u679c\u76ee\u6807\u7528\u6237\u6ca1\u6709\u8bc4\u4ef7\u8fc7\u8be5\u5e16\u5b50\uff0c\u5219\u8ba1\u5165\u63a8\u8350\u6b21\u6570\n                if (!userRatings.get(targetUserId).containsKey(bookRating.getKey())) {\n                    stickVotes.merge(bookRating.getKey(), 1, Integer::sum);  \/\/ \u589e\u52a0\u8be5\u5e16\u5b50\u7684\u63a8\u8350\u6b21\u6570\n                }\n            }\n        }\n\n        \/\/ \u5c06\u63a8\u8350\u6b21\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u9ad8\u5230\u4f4e\n        List&lt;Map.Entry&lt;Integer, Integer>> sortedsticks = new ArrayList&lt;>(stickVotes.entrySet());\n        sortedsticks.sort(Map.Entry.&lt;Integer, Integer>comparingByValue().reversed());\n\n        \/\/ \u53ea\u8fd4\u56de\u524d5\u4e2a\u63a8\u8350\u7684\u5e16\u5b50\n        List&lt;Map.Entry&lt;Integer, Integer>> recommendStick = new ArrayList&lt;>();\n        System.out.println(\"\u4e3a\u7528\u6237 \" + targetUserId + \" \u63a8\u8350\u7684\u5e16\u5b50\uff1a\");\n        for (int i = 0; i &lt; Math.min(5, sortedsticks.size()); i++) {\n            recommendStick.add(sortedsticks.get(i));  \/\/ \u83b7\u53d6\u524d5\u4e2a\u5e16\u5b50\n            System.out.println(\"\u5e16\u5b50ID: \" + sortedsticks.get(i).getKey() + \", \u88ab\u63a8\u8350\u6b21\u6570: \" + sortedsticks.get(i).getValue());\n        }\n\n        return recommendStick;  \/\/ \u8fd4\u56de\u63a8\u8350\u7684\u5e16\u5b50\u5217\u8868\n    }<\/code><\/pre>\n\n\n\n<p>5.<strong>\u5b8c\u6574\u5b9e\u73b0\u4ee3\u7801<\/strong><\/p>\n\n\n\n<p>\u5230\u8fd9\u91cc\u53ef\u4ee5\u8bf4\u5de5\u4f5c\u5b8c\u6210\u5927\u534a\u4e86\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u5199\u63a5\u53e3\u8c03\u7528\u54b1\u4eec\u5c01\u88c5\u7684\u6a21\u5757\u4e86\u3002\u6211\u8fd9\u91cc\u7ed9\u51fa\u5b8c\u6574\u7684StickRecommend\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.nchu.common.recommend;\nimport java.sql.*;\nimport java.util.*;\n\npublic class StickRecommend {\n\n    \/\/ \u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\n    public static final String DB_URL = \"jdbc:mysql:\/\/localhost:3306\/warmheart\";\n    public static final String USER = \"\u6570\u636e\u5e93\u7528\u6237\u540d\";\n    public static final String PASS = \"\u81ea\u5df1\u7684\u5bc6\u7801\";\n\n\n    \/\/ \u52a0\u8f7d\u7528\u6237\u8bc4\u5206\u6570\u636e\u7684\u65b9\u6cd5\n    \/\/ \u8be5\u65b9\u6cd5\u4ece\u6570\u636e\u5e93\u4e2d\u52a0\u8f7d\u7528\u6237\u5bf9\u5404\u4e2a\u5e16\u5b50\uff08stick\uff09\u7684\u8bc4\u5206\u6570\u636e\n    \/\/ \u8fd4\u56de\u4e00\u4e2aMap\uff0c\u5176\u4e2dkey\u662f\u7528\u6237ID\uff0cvalue\u662f\u4e00\u4e2aMap\uff0c\u5b58\u50a8\u8be5\u7528\u6237\u5bf9\u5404\u4e2a\u5e16\u5b50\u7684\u8bc4\u5206\n    public static Map&lt;Integer, Map&lt;Integer, Double>> loadRatingsFromDB(Connection conn) throws SQLException {\n        Map&lt;Integer, Map&lt;Integer, Double>> userRatings = new HashMap&lt;>();\n\n        \/\/ \u4f7f\u7528Statement\u6267\u884cSQL\u67e5\u8be2\uff0c\u4ece\u6570\u636e\u5e93\u8868 \"stick_score\" \u83b7\u53d6\u7528\u6237\u8bc4\u5206\u6570\u636e\n        try (Statement stmt = conn.createStatement();\n             ResultSet rs = stmt.executeQuery(\"SELECT user_id, stick_id, score FROM stick_score\")) {\n            \/\/ \u904d\u5386\u67e5\u8be2\u7ed3\u679c\u96c6\uff0c\u5c06\u6570\u636e\u6309\u7167\u7528\u6237ID\u548c\u5e16\u5b50ID\u7ec4\u7ec7\u6210Map\n            while (rs.next()) {\n                int userId = rs.getInt(\"user_id\");  \/\/ \u83b7\u53d6\u7528\u6237ID\n                int stickId = rs.getInt(\"stick_id\");  \/\/ \u83b7\u53d6\u5e16\u5b50ID\n                double rating = rs.getDouble(\"score\");  \/\/ \u83b7\u53d6\u8bc4\u5206\n\n                \/\/ \u5c06\u8bc4\u5206\u6570\u636e\u5b58\u50a8\u5230Map\u4e2d\uff0cMap\u7684\u7ed3\u6784\u662f\uff1a\u7528\u6237ID -> (\u5e16\u5b50ID -> \u8bc4\u5206)\n                userRatings.computeIfAbsent(userId, k -> new HashMap&lt;>()).put(stickId, rating);\n            }\n        }\n        return userRatings;  \/\/ \u8fd4\u56de\u5b58\u50a8\u8bc4\u5206\u6570\u636e\u7684Map\n    }\n\n    \/\/ \u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\n    \/\/ \u8be5\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u7528\u6237\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u57fa\u4e8e\u4ed6\u4eec\u5bf9\u76f8\u540c\u5e16\u5b50\u7684\u8bc4\u5206\u8fdb\u884c\u8ba1\u7b97\n    \/\/ \u4f59\u5f26\u76f8\u4f3c\u5ea6\u7684\u503c\u8d8a\u5927\uff0c\u8bf4\u660e\u4e24\u4e2a\u7528\u6237\u7684\u5174\u8da3\u8d8a\u76f8\u4f3c\n    public static double cosineSimilarity(Map&lt;Integer, Double> ratings1, Map&lt;Integer, Double> ratings2) {\n        \/\/ \u83b7\u53d6\u4e24\u4e2a\u7528\u6237\u7684\u8bc4\u5206\u7684\u5171\u540c\u5e16\u5b50ID\n        Set&lt;Integer> commonBooks = new HashSet&lt;>(ratings1.keySet());\n        commonBooks.retainAll(ratings2.keySet());  \/\/ \u6c42\u4ea4\u96c6\uff0c\u627e\u5230\u5171\u540c\u8bc4\u5206\u7684\u5e16\u5b50\n\n        \/\/ \u5982\u679c\u4e24\u4e2a\u7528\u6237\u6ca1\u6709\u5171\u540c\u8bc4\u5206\u7684\u5e16\u5b50\uff0c\u76f8\u4f3c\u5ea6\u4e3a0\n        if (commonBooks.isEmpty()) return 0;\n\n        double dotProduct = 0.0;  \/\/ \u70b9\u79ef\n        double normA = 0.0;  \/\/ \u5411\u91cfA\u7684\u6a21\n        double normB = 0.0;  \/\/ \u5411\u91cfB\u7684\u6a21\n\n        \/\/ \u8ba1\u7b97\u70b9\u79ef\u548c\u5404\u81ea\u5411\u91cf\u7684\u6a21\n        for (int stickId : commonBooks) {\n            double rating1 = ratings1.get(stickId);  \/\/ \u7528\u62371\u5bf9\u8be5\u5e16\u5b50\u7684\u8bc4\u5206\n            double rating2 = ratings2.get(stickId);  \/\/ \u7528\u62372\u5bf9\u8be5\u5e16\u5b50\u7684\u8bc4\u5206\n            dotProduct += rating1 * rating2;  \/\/ \u8ba1\u7b97\u70b9\u79ef\n            normA += Math.pow(rating1, 2);  \/\/ \u8ba1\u7b97\u7528\u62371\u8bc4\u5206\u7684\u5e73\u65b9\u548c\n            normB += Math.pow(rating2, 2);  \/\/ \u8ba1\u7b97\u7528\u62372\u8bc4\u5206\u7684\u5e73\u65b9\u548c\n        }\n\n        \/\/ \u8ba1\u7b97\u5e76\u8fd4\u56de\u4f59\u5f26\u76f8\u4f3c\u5ea6\n        return dotProduct \/ (Math.sqrt(normA) * Math.sqrt(normB));\n    }\n\n    \/\/ \u627e\u5230\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n    \/\/ \u8be5\u65b9\u6cd5\u901a\u8fc7\u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\u6765\u627e\u5230\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n    public static List&lt;Integer> findTopThreeSimilarUsers(int targetUserId, Map&lt;Integer, Map&lt;Integer, Double>> userRatings) {\n        \/\/ \u4f7f\u7528\u4f18\u5148\u961f\u5217\uff08\u6700\u5927\u5806\uff09\u5b58\u50a8\u7528\u6237\u76f8\u4f3c\u5ea6\u548c\u7528\u6237ID\n        PriorityQueue&lt;Map.Entry&lt;Integer, Double>> pq = new PriorityQueue&lt;>(\n                (e1, e2) -> Double.compare(e1.getValue(), e2.getValue())  \/\/ \u6309\u76f8\u4f3c\u5ea6\u5347\u5e8f\u6392\u5e8f\n        );\n\n        \/\/ \u904d\u5386\u6240\u6709\u7528\u6237\uff0c\u8ba1\u7b97\u4e0e\u76ee\u6807\u7528\u6237\u7684\u76f8\u4f3c\u5ea6\n        for (Map.Entry&lt;Integer, Map&lt;Integer, Double>> entry : userRatings.entrySet()) {\n            int userId = entry.getKey();  \/\/ \u7528\u6237ID\n            if (userId == targetUserId) continue;  \/\/ \u8df3\u8fc7\u76ee\u6807\u7528\u6237\u672c\u8eab\n\n            \/\/ \u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\n            double similarity = cosineSimilarity(userRatings.get(targetUserId), entry.getValue());\n            \/\/ \u5c06\u7528\u6237\u548c\u76f8\u4f3c\u5ea6\u5b58\u5165\u4f18\u5148\u961f\u5217\n            Map.Entry&lt;Integer, Double> user = new AbstractMap.SimpleEntry&lt;>(userId, similarity);\n            pq.offer(user);\n\n            \/\/ \u5982\u679c\u961f\u5217\u4e2d\u7684\u7528\u6237\u6570\u8d85\u8fc73\u4e2a\uff0c\u79fb\u9664\u6700\u4e0d\u76f8\u4f3c\u7684\u7528\u6237\n            if (pq.size() > 3) {\n                pq.poll();\n            }\n        }\n\n        \/\/ \u4ece\u4f18\u5148\u961f\u5217\u4e2d\u63d0\u53d6\u51fa\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n        List&lt;Integer> topUsers = new ArrayList&lt;>();\n        while (!pq.isEmpty()) {\n            topUsers.add(pq.poll().getKey());  \/\/ \u63d0\u53d6\u7528\u6237ID\n        }\n        return topUsers;  \/\/ \u8fd4\u56de\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\u7684ID\n    }\n\n    \/\/ \u57fa\u4e8e\u76f8\u4f3c\u7528\u6237\u63a8\u8350\u5e16\u5b50\n    \/\/ \u8be5\u65b9\u6cd5\u57fa\u4e8e\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u7528\u6237\u6765\u63a8\u8350\u5e16\u5b50\n    public static List&lt;Map.Entry&lt;Integer, Integer>> recommendStickOnSimilarUsers(Map&lt;Integer, Map&lt;Integer, Double>> userRatings,\n                                                                                 List&lt;Integer> similarUsers, int targetUserId) {\n        Map&lt;Integer, Integer> stickVotes = new HashMap&lt;>();  \/\/ \u5b58\u50a8\u6bcf\u4e2a\u5e16\u5b50\u7684\u63a8\u8350\u6b21\u6570\n\n        \/\/ \u904d\u5386\u6240\u6709\u76f8\u4f3c\u7528\u6237\uff0c\u7edf\u8ba1\u4ed6\u4eec\u63a8\u8350\u8fc7\u7684\u5e16\u5b50\n        for (int userId : similarUsers) {\n            Map&lt;Integer, Double> userBooks = userRatings.getOrDefault(userId, Collections.emptyMap());\n            for (Map.Entry&lt;Integer, Double> bookRating : userBooks.entrySet()) {\n                \/\/ \u5982\u679c\u76ee\u6807\u7528\u6237\u6ca1\u6709\u8bc4\u4ef7\u8fc7\u8be5\u5e16\u5b50\uff0c\u5219\u8ba1\u5165\u63a8\u8350\u6b21\u6570\n                if (!userRatings.get(targetUserId).containsKey(bookRating.getKey())) {\n                    stickVotes.merge(bookRating.getKey(), 1, Integer::sum);  \/\/ \u589e\u52a0\u8be5\u5e16\u5b50\u7684\u63a8\u8350\u6b21\u6570\n                }\n            }\n        }\n\n        \/\/ \u5c06\u63a8\u8350\u6b21\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u9ad8\u5230\u4f4e\n        List&lt;Map.Entry&lt;Integer, Integer>> sortedsticks = new ArrayList&lt;>(stickVotes.entrySet());\n        sortedsticks.sort(Map.Entry.&lt;Integer, Integer>comparingByValue().reversed());\n\n        \/\/ \u53ea\u8fd4\u56de\u524d5\u4e2a\u63a8\u8350\u7684\u5e16\u5b50\n        List&lt;Map.Entry&lt;Integer, Integer>> recommendStick = new ArrayList&lt;>();\n        System.out.println(\"\u4e3a\u7528\u6237 \" + targetUserId + \" \u63a8\u8350\u7684\u5e16\u5b50\uff1a\");\n        for (int i = 0; i &lt; Math.min(5, sortedsticks.size()); i++) {\n            recommendStick.add(sortedsticks.get(i));  \/\/ \u83b7\u53d6\u524d5\u4e2a\u5e16\u5b50\n            System.out.println(\"\u5e16\u5b50ID: \" + sortedsticks.get(i).getKey() + \", \u88ab\u63a8\u8350\u6b21\u6570: \" + sortedsticks.get(i).getValue());\n        }\n\n        return recommendStick;  \/\/ \u8fd4\u56de\u63a8\u8350\u7684\u5e16\u5b50\u5217\u8868\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">No.2 \u63a8\u8350\u63a5\u53e3\u7684\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u6211\u7684\u9879\u76ee\u662f\u57fa\u4e8eSpringBoot\u548cmybatis-plus\u6846\u67b6\uff0c\u5927\u5bb6\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u7684\u9700\u6c42\u53bb\u5199\u3002\u63a5\u4e0b\u6765\u4f9d\u6b21\u770b\u4e00\u4e0b\u5404\u4e2a\u5c42\u7684\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.controller\u5c42<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u4ee3\u7801\u7b80\u5355\uff0c\u4e0d\u591a\u8bf4\u5565\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @GetMapping(\"\/recommend\")\n    public R getRecommend(@RequestParam Integer userId){\n        List&lt;Sticks> list = sticksService.recommendSticks(userId);\n        return R.ok().data(\"stickList\",list);\n    }\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.service\u5c42<\/h3>\n\n\n\n<p>\u6211\u7684\u63a8\u8350\u7b56\u7565\u5c31\u662f\u7528\u6237\u6ca1\u6709\u8bc4\u5206\u5386\u53f2\u65f6\uff0c\u76f4\u63a5\u63a8\u8350\u4e94\u4e2a\u70b9\u8d5e\u91cf\u6700\u9ad8\u7684\u5e16\u5b50\uff1b\u800c\u5728\u6709\u8bc4\u5206\u5386\u53f2\u7684\u60c5\u51b5\u4e0b\uff0c\u5219\u5229\u7528\u534f\u540c\u8fc7\u6ee4\u7b97\u6cd5\u6839\u636e\u76f8\u4f3c\u7528\u6237\u7684\u504f\u597d\u6765\u63a8\u8350\uff0c\u4f46\u662f\u5982\u679c\u76f8\u4f3c\u7528\u6237\u8bc4\u5206\u8fc7\u7684\u5e16\u5b50\u5728\u76ee\u6807\u7528\u6237\u8fd9\u91cc\u4e5f\u90fd\u8bc4\u4ef7\u4e86\u5462\uff0c\u90a3\u6ca1\u529e\u6cd5\u518d\u76f4\u63a5\u7ed9\u4ed6\u63a8\u8350\u4e94\u4e2a\u70b9\u8d5e\u91cf\u6700\u9ad8\u8fbe\u5230\u5e16\u5b50\u4e86\ud83d\ude02\uff0c\u8fd9\u91cc\u53ef\u4ee5\u4f9d\u636e\u81ea\u5df1\u7684\u60c5\u51b5\u800c\u5b9a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\npublic interface ISticksService extends IService&lt;Sticks> {\n     \/\/\u5206\u9875\u67e5\u8be2\n     PageResult&lt;Sticks> findByPage(Integer page,Integer pageSize);\n     \/\/\u83b7\u53d6\u6700\u70ed\n     PageResult&lt;Sticks> getHot(Integer page,Integer pageSize);\n     \/\/\u83b7\u53d6\u6700\u65b0\n     PageResult&lt;Sticks> getNew(Integer page,Integer pageSize);\n     \/\/\u63a8\u8350\u8d34\u5b50\n     List&lt;Sticks> recommendSticks(Integer userId);\n}\npublic interface IStickScoreService extends IService&lt;StickScore> {\n     List&lt;Sticks> recommend(Integer userId);\n}\n\n---------------------------------------------------------------------------------------------\n\u8fd9\u91cc\u662f StickScoreService\u5b9e\u73b0\u7c7b\u7684\u90e8\u5206\u4ee3\u7801\uff1a\n@Service  \npublic class StickScoreServiceImpl extends ServiceImpl&lt;StickScoreMapper, StickScore> implements IStickScoreService {\n\n    @Autowired \n    @Lazy  \/\/ \u61d2\u52a0\u8f7d\u6ce8\u89e3\uff0c\u8868\u793a\u5728\u7b2c\u4e00\u6b21\u9700\u8981\u8be5 Bean \u65f6\u624d\u4f1a\u52a0\u8f7d\n    private ISticksService sticksService;  \n\n    @Override\n    public List&lt;Sticks> recommend(Integer userId) {\n        List&lt;Sticks> stickList = new ArrayList&lt;>();  \/\/ \u7528\u6765\u5b58\u50a8\u6700\u7ec8\u63a8\u8350\u7684\u5e16\u5b50\u5217\u8868\n        try (\n                \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\n                Connection conn = DriverManager.getConnection(StickRecommend.DB_URL, StickRecommend.USER, StickRecommend.PASS)\n        ) {\n            \/\/ \u4ece\u6570\u636e\u5e93\u52a0\u8f7d\u7528\u6237\u8bc4\u5206\u6570\u636e\n            Map&lt;Integer, Map&lt;Integer, Double>> userRatings = StickRecommend.loadRatingsFromDB(conn);\n\n            \/\/ \u627e\u5230\u4e0e\u76ee\u6807\u7528\u6237\u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\n            List&lt;Integer> similarUsers = StickRecommend.findTopThreeSimilarUsers(userId, userRatings);\n            System.out.println(\"\u4e0e\u7528\u6237 \" + userId + \" \u6700\u76f8\u4f3c\u7684\u4e09\u4e2a\u7528\u6237\u4e3a\uff1a\" + similarUsers);\n\n            \/\/ \u6839\u636e\u4e0e\u76f8\u4f3c\u7528\u6237\u7684\u8bc4\u5206\uff0c\u63a8\u8350\u76ee\u6807\u7528\u6237\u53ef\u80fd\u611f\u5174\u8da3\u7684\u5e16\u5b50\n            List&lt;Map.Entry&lt;Integer, Integer>> list = StickRecommend.recommendStickOnSimilarUsers(userRatings, similarUsers, userId);\n\n            \/\/ \u904d\u5386\u63a8\u8350\u7684\u5e16\u5b50\u5217\u8868\n            for (Map.Entry&lt;Integer, Integer> map : list) {\n                \/\/ map.getKey() \u83b7\u53d6\u5e16\u5b50 ID\n                LambdaQueryWrapper&lt;Sticks> queryWrapper = new LambdaQueryWrapper&lt;>();\n                queryWrapper.eq(Sticks::getId, map.getKey());  \/\/ \u6839\u636e\u5e16\u5b50 ID \u6784\u5efa\u67e5\u8be2\u6761\u4ef6\n                Sticks sticks = sticksService.getOne(queryWrapper);  \/\/ \u4ece\u6570\u636e\u5e93\u67e5\u8be2\u5e16\u5b50\u4fe1\u606f\n                stickList.add(sticks);  \/\/ \u5c06\u67e5\u8be2\u5230\u7684\u5e16\u5b50\u6dfb\u52a0\u5230\u63a8\u8350\u5217\u8868\u4e2d\n            }\n        } catch (SQLException e) {\n            e.printStackTrace(); \n        }\n        return stickList;\n    }\n}\n---------------------------------------------------------------------------------------------\n\u8fd9\u662fSticksService\u5b9e\u73b0\u7c7b\u7684\u90e8\u5206\u4ee3\u7801\n    @Autowired\n    @Lazy\n    private IStickScoreService stickScoreService;\n @Override\n    public List&lt;Sticks> recommendSticks(Integer userId) {\n        LambdaQueryWrapper&lt;StickScore> queryWrapper = new LambdaQueryWrapper&lt;>();\n        queryWrapper.eq(StickScore::getUserId,userId);\n        List&lt;StickScore> stickScoreList = stickScoreService.list(queryWrapper);\n\n        LambdaQueryWrapper&lt;Sticks> wrapper = new LambdaQueryWrapper&lt;>();\n        wrapper.orderBy(true,false,Sticks::getLikes);\n        wrapper.last(\"LIMIT 5\");\n        \/\/\u5982\u679c\u7528\u6237\u4ece\u672a\u8bc4\u4ef7\u8fc7,\u5219\u63a8\u8350\u5e16\u5b50\u70b9\u8d5e\u91cf\u6700\u9ad8\u76845\u4e2a\u5e16\u5b50\uff0c\u53cd\u4e4b\u8bc4\u4ef7\u8fc7\u5219\u6309\u534f\u540c\u8fc7\u6ee4\u7b97\u6cd5\u8fdb\u884c\u63a8\u8350\n        if(stickScoreList.isEmpty()) {\n            List&lt;Sticks> sticks = this.list(wrapper);\n            return sticks;\n        }else {\n            List&lt;Sticks> list = stickScoreService.recommend(userId);\n            if(list.isEmpty()){\n                \/\/\u5982\u679c\u6240\u6709\u5e16\u5b50\u90fd\u8bc4\u4ef7\u8fc7\uff0c\u5219\u63a8\u83505\u4e2a\u70b9\u8d5e\u91cf\u6700\u9ad8\u7684\u5e16\u5b50\n                List&lt;Sticks> sticks = this.list(wrapper);\n                return sticks;\n            }else {\n                return list;\n            }\n        }\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">No3.\u8fd0\u884c\u7ed3\u679c<\/h2>\n\n\n\n<p>\u5230\u8fd9\u91cc\u57fa\u672c\u5c31\u7ed3\u675f\u4e86\uff0c\u5927\u81f4\u7684\u6d41\u7a0b\u5c31\u662f\u8fd9\u6837\u4e86\uff0c\u770b\u4e00\u4e0b\u7ed3\u679c\uff0c\u6211\u7528apifox\u6d4b\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-7-1024x141.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"141\" data-original=\"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-7-1024x141.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-567\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-8-1024x634.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"634\" data-original=\"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-8-1024x634.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-568\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u662f\u53ef\u4ee5\u8fdb\u884c\u76f8\u5173\u63a8\u8350\u7684\uff0c\u57fa\u7840\u529f\u80fd\u7b97\u662f\u8fbe\u5230\u4e86\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e86\u8fd9\u91cc\u518d\u8865\u5145\u4e00\u4e0b\u6211\u8fd9\u91cc\u9047\u5230\u7684\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2025-02-08 15:34:07.935  WARN 9792 --- &#91;           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sticksController': Unsatisfied dependency expressed through field 'sticksService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sticksServiceImpl': Unsatisfied dependency expressed through field 'stickScoreService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stickScoreServiceImpl': Unsatisfied dependency expressed through field 'sticksService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sticksServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?\n2025-02-08 15:34:07.938  INFO 9792 --- &#91;           main] o.apache.catalina.core.StandardService   : Stopping service &#91;Tomcat]\n2025-02-08 15:34:07.949  INFO 9792 --- &#91;           main] ConditionEvaluationReportLoggingListener :\n\nError starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.\n2025-02-08 15:34:07.968 ERROR 9792 --- &#91;           main] o.s.b.d.LoggingFailureAnalysisReporter   :\n\nAPPLICATION FAILED TO START\n\nDescription:\n\nThe dependencies of some of the beans in the application context form a cycle:\n\nsticksController (field private com.yaojie.service.ISticksService com.yaojie.controller.SticksController.sticksService)\n\u250c\u2500\u2500\u2500\u2500\u2500\u2510\n|  sticksServiceImpl (field private com.yaojie.service.IStickScoreService com.yaojie.service.impl.SticksServiceImpl.stickScoreService)\n\u2191     \u2193\n|  stickScoreServiceImpl (field private com.yaojie.service.ISticksService com.yaojie.service.impl.StickScoreServiceImpl.sticksService)\n\u2514\u2500\u2500\u2500\u2500\u2500\u2518\n\nAction:\n\nRelying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.\n\n\u8fdb\u7a0b\u5df2\u7ed3\u675f,\u9000\u51fa\u4ee3\u78011<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u9519\u8bef\u662f\u7531\u4e8e <strong>Spring Bean \u7684\u5faa\u73af\u4f9d\u8d56<\/strong>\uff08circular dependency\uff09\u5bfc\u81f4\u7684\u3002<\/p>\n\n\n\n<p><code>SticksController<\/code>\u00a0\u9700\u8981\u00a0<code>SticksService<\/code>\u3002<\/p>\n\n\n\n<p><code>SticksServiceImpl<\/code>\u00a0\u9700\u8981\u00a0<code>StickScoreService<\/code>\u3002<\/p>\n\n\n\n<p><code>StickScoreServiceImpl<\/code>\u00a0\u9700\u8981\u00a0<code>SticksService<\/code>\u3002<\/p>\n\n\n\n<p>\u6211\u8fd9\u91cc\u662f\u4f7f\u7528 Spring \u7684\u00a0<code>@Lazy<\/code>\u00a0\u6ce8\u89e3\u3002\u901a\u8fc7\u5c06\u5176\u4e2d\u4e00\u4e2a\u4f9d\u8d56\u6ce8\u5165\u8bbe\u7f6e\u4e3a\u61d2\u52a0\u8f7d\uff08\u5ef6\u8fdf\u52a0\u8f7d\uff09\uff0c\u53ef\u4ee5\u6253\u7834\u5faa\u73af\u4f9d\u8d56\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class SticksServiceImpl implements ISticksService {\n\n    @Autowired\n    @Lazy\n    private IStickScoreService stickScoreService;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">No4.\u603b\u7ed3<\/h2>\n\n\n\n<p>\u8fd9\u91cc\u7684\u534f\u540c\u8fc7\u6ee4\u63a8\u8350\u903b\u8f91\u5b9e\u73b0\u8fd8\u662f\u4e0d\u7b97\u5f88\u590d\u6742\u7684\uff0c\u6211\u770b\u5230\u7684\u4e00\u4e2a\u97f3\u4e50\u534f\u540c\u8fc7\u6ee4\u63a8\u8350\uff0c\u91cc\u9762\u590d\u6742\u7684\u591a\u4e86\uff0c\u4e4b\u540e\u6709\u65f6\u95f4\u518d\u7814\u7a76\u7814\u7a76\uff0c\u8fd9\u6b21\u4e5f\u662f\u5b66\u5230\u4e86\u4e00\u4e9b\u4e1c\u897f\u7684\u3002\ud83d\ude01<\/p>\n\n\n\n<p>\u6211\u4e5f\u662f\u5e26\u7740\u5b66\u4e60\u7684\u76ee\u7684\u53bb\u5c1d\u8bd5\u5199\u7684\uff0c\u6240\u4ee5\u4ee3\u7801\u80af\u5b9a\u6709\u5f88\u591a\u6ca1\u5199\u597d\u7684\u5730\u65b9\uff0c\u535a\u5ba2\u4e5f\u662f\u968f\u5fc3\u5199\u7684\uff0c\u4e5f\u8bb8\u4f1a\u6709\u5f88\u591a\u9519\uff0c\u5927\u5bb6\u624b\u4e0b\u7559\u60c5\uff0c\u4e00\u8d77\u4ea4\u6d41\u4e00\u8d77\u5b66\u4e60\uff01\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u4e8c\u7684\u65f6\u5019\u5c31\u60f3\u641e\u4e00\u4e0b\u63a8\u8350\u7b97\u6cd5\u4e86\uff0c\u4f46\u662f\u90a3\u65f6\u5019\u4e0d\u60f3\u5012\u817e\uff0c\u52a0\u4e0a\u6ca1\u5565\u53c2\u8003\u8d44\u6599\uff0c\u5c0f\u7834\u7ad9\u4e0a\u4e5f\u6ca1\u6709\u5565\u6559\u7a0b\uff0c\u5168\u662f\u6253\u5e7f\u544a\u63a8\u9500\u5356\u4f5c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[18,4,1],"tags":[51,49,50],"class_list":["post-562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-back","category-blog","category-uncategorized","tag-springboot","tag-49","tag-50"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2025\/02\/image-7.png","_links":{"self":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/comments?post=562"}],"version-history":[{"count":2,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/562\/revisions\/569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/media\/567"}],"wp:attachment":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/media?parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/categories?post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/tags?post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}