乐于分享
好东西不私藏

Java版AI小助手:拥有上下文记忆 + MySQL Function Calling,从0到1完整代码实现

Java版AI小助手:拥有上下文记忆 + MySQL Function Calling,从0到1完整代码实现
之前两篇都是 Python 搭建的,这篇使用 Java 从 0 到 1 搭建聊天小助手。话不多说,正式开始。
一、编写前端的聊天界面

1、创建一个按钮,点击按钮可以弹出聊天对话框,代码如下

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
<!-- AI Agent 按钮 --><divid="aiAgentBtn"class="ai-agent-btn"><span>AI小助手</span></div><!-- AI Agent 聊天窗口 --><divid="aiAgentPanel"class="ai-agent-panel"style="display: none;"><divclass="ai-agent-header"><span>AI小助手</span><divclass="ai-header-actions"><buttonclass="ai-agent-clear"onclick="clearAiAgentHistory()"title="清除历史">🗑️</button><buttonclass="ai-agent-close"onclick="closeAiAgent()">&times;</button></div></div><divclass="ai-agent-body"id="aiAgentMessages"><divclass="ai-message">您好!我是AI小助手。我可以:<br>1. 查询系统AI资讯<br>2. 查看数据报表<br>3. 其他问题解答<br><br>请告诉我您需要什么帮助?</div></div><divclass="ai-agent-footer"><inputtype="text"id="aiAgentInput"placeholder="请输入您的问题..."onkeydown="handleAiAgentEnter(event)"oninput="autoResizeInput(this)"><buttonclass="send-btn"onclick="sendAiAgentMessage()">发送</button></div></div>

2、页面的css 样式如下

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
<style>/* AI Agent 按钮样式 */.ai-agent-btn {position: fixed;right: -65px;bottom20px;height40px;backgroundlinear-gradient(135deg#667eea0%#764ba2100%);border-radius20px0020px;display: flex;align-items: center;justify-content: flex-start;cursor: pointer;box-shadow04px15pxrgba(1021262340.4);z-index9999;padding015px025px;gap8px;transition: right 0.3s ease-out, border-radius 0.3s ease-out;width110px;    }.ai-agent-btn:hover {right: -10px;    }.ai-agent-btn.expanded {right20px;border-radius20px;    }.ai-agent-btn.expanded:hover {transformscale(1.05);    }.ai-agent-btni {color#fff;font-size20px;    }.ai-agent-btnspan {color#fff;font-size14px;font-weight500;    }/* AI Agent 聊天面板样式 */.ai-agent-panel {position: fixed;right30px;bottom50px;width25vw;height75vh;background#fff;border-radius12px;box-shadow05px20pxrgba(0,0,0,0.15);z-index9998;display: flex;flex-direction: column;overflow: hidden;    }.ai-agent-header {backgroundlinear-gradient(135deg#667eea0%#764ba2100%);color#fff;padding15px;display: flex;justify-content: space-between;align-items: center;font-weight: bold;height50px;    }.ai-header-actions {display: flex;gap8px;align-items: center;    }.ai-agent-clear {background: none;border: none;color#fff;font-size16px;cursor: pointer;padding4px;opacity0.8;transition: opacity 0.2s;    }.ai-agent-clear:hover {opacity1;transformscale(1.1);    }.ai-agent-close {background: none;border: none;color#fff;font-size24px;cursor: pointer;line-height1;    }.ai-agent-body {flex1;padding15px;padding-bottom70px;overflow-y: auto;background#f5f7fa;box-sizing: border-box;    }.ai-agent-body.ai-message,.ai-agent-body.user-message {padding10px14px;border-radius8px;margin-bottom10px;max-width85%;word-wrap: break-word;    }/* Avatar support for chat messages (no HTML changes required) */.ai-agent-body.ai-message { position: relative; padding-left60px; }.ai-agent-body.ai-message::before {content"AI";position: absolute;left12px;top6px;width28pxheight28pxborder-radius50%;display: inline-flex; align-items: center; justify-content: center;background#666color#ffffont-size12px;    }.ai-agent-body.user-message { padding-right60pxposition: relative; }.ai-agent-body.user-message::before {content"我";position: absolute;right12px;top6px;width28pxheight28pxborder-radius50%;display: inline-flex; align-items: center; justify-content: center;background#894feacolor#ffffont-size12px;    }.ai-message {background#e8e8e8;color#333;align-self: flex-start;white-space: pre-wrap;    }.ai-messagetable {border-collapse: collapse;width100%;margin10px0;font-size13px;    }.ai-messagetableth,.ai-messagetabletd {border1px solid #ddd;padding8px;text-align: left;    }.ai-messagetableth {background#667eea;color#fff;    }.ai-messagetabletr:nth-child(even) {background#f9f9f9;    }.user-message {background#667eea;color#fff;margin-left: auto;    }.ai-agent-footer {padding12px;border-top1px solid #eee;display: flex;flex-direction: row;align-items: flex-end;gap10px;    }.ai-agent-footerinput {flex1;padding8px16px;border1px solid #ddd;border-radius8px;outline: none;resize: none;min-height30px;text-overflow: ellipsis;word-wrap: break-word;word-break: break-all;white-space: pre-wrap;    }.ai-agent-footerinput:focus {border-color#667eea;    }.ai-agent-footer.send-btn {padding8px16px;min-height37px;background#667eea;color#fff;border: none;border-radius20px;cursor: pointer;flex-shrink0;    }.ai-agent-footer.send-btn:hover {background#764ba2;    }</style>

3、点击事件js 如下

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
<script language="JavaScript">var aiAgentTimer = null;varAI_AGENT_TIMEOUT = 60000;functionresetAiAgentTimer() {if (aiAgentTimer) {clearTimeout(aiAgentTimer);        }        aiAgentTimer = setTimeout(function() {            $('#aiAgentBtn').removeClass('expanded');        }, AI_AGENT_TIMEOUT);    }    $(document).ready(function() {// AI Agent 按钮点击事件        $('#aiAgentBtn').click(function() {            $(this).hide();            $('#aiAgentPanel').show();        });// 监听AI面板内的交互,重置计时器        $('#aiAgentPanel').on('click keydown'function() {resetAiAgentTimer();        });        $('#aiAgentInput').on('input'function() {resetAiAgentTimer();        });    });// 关闭 AI Agent 面板functioncloseAiAgent() {        $('#aiAgentPanel').hide();        $('#aiAgentBtn').show().addClass('expanded');resetAiAgentTimer();    }// 清除 AI Agent 历史记录functionclearAiAgentHistory() {        js.layer.confirm('确定要清除对话历史吗?'function() {            $.post("${ctx}/ai/chat/clear"function(data) {if(data==='success') {// 清空消息区域,只保留欢迎语   $('#aiAgentMessages').html('<div class="ai-message">您好!我是AI小助手。我可以:<br>1. 查询项目信息<br>2. 查看数据报表<br>3. 其他问题解答<br><br>请告诉我您需要什么帮助?</div>');   js.layer.msg('历史记录已清除',{icon:1,time:1500});   }else {   js.layer.msg('清除失败',{icon:2,time:1500});                }            }).fail(function() {   js.layer.msg('请求失败',{icon:2,time:1500});            });        });    }// 处理回车发送消息functionhandleAiAgentEnter(event) {if (event.key === 'Enter' && !event.shiftKey) {            event.preventDefault();sendAiAgentMessage();        }    }// 输入框自动伸开,最多3行functionautoResizeInput(input) {        input.style.height = 'auto';var scrollHeight = input.scrollHeight;var maxHeight = 72// 约3行高度        input.style.height = Math.min(scrollHeight, maxHeight) + 'px';    }// 发送消息functionsendAiAgentMessage() {var input = $('#aiAgentInput');var message = input.val().trim();if (!message) return;// 添加用户消息        $('#aiAgentMessages').append('<div class="user-message">' + message + '</div>');        input.val('');// 滚动到底部        $('#aiAgentMessages').scrollTop($('#aiAgentMessages')[0].scrollHeight);// 显示加载中        $('#aiAgentMessages').append('<div class="ai-message">正在思考...</div>');// 调用后端 AI 接口(同步)        $.post("${ctx}/ai/chat/send", {message: message}, function(data) {            $('#aiAgentMessages .ai-message').last().remove();// var html = marked.parse(data);   $('#aiAgentMessages').append('<div class="ai-message">'+data+'</div>');            $('#aiAgentMessages').scrollTop($('#aiAgentMessages')[0].scrollHeight);        }).fail(function(xhr) {            $('#aiAgentMessages .ai-message').last().remove();            $('#aiAgentMessages').append('<div class="ai-message">请求失败,请重试</div>');        });    }</script>

4、前端AI 弹窗界面运行的效果如下,效果还不错吧~

二、和AI聊天,并让它能记住上下文内容

1、在controller获取用户输入,service 层执行AI 聊天功能

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
/**   * 聊天   */  @PostMapping(value = "send")@ResponseBodypublic String send(@RequestParam String message,     HttpServletRequestrequest,HttpServletResponseresponse){      logger.debug("用户输入的内容={}", message);  try {   return aiChatService.chat(message);      } catch (Exception e) {          logger.error("AI聊天异常", e);  return"error: " + e.getMessage();      }  }

2、service 是核心

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
/**   * AI聊天   */  public String chat(String message) {      List<JSONObject> messages = newArrayList<>();  JSONObjectuserJSON=newJSONObject();      userJSON.put("role""user");      userJSON.put("content", message);      messages.add(userJSON);  try {  return chatWithoutTools(messages);      } catch (Exception e) {          logger.error("AI聊天异常", e);  return"error: " + e.getMessage();      }  }/**   * 发送http请求,与通过ollama与大模型对话   */ private String chatWithoutTools(List<JSONObject> messages)throws Exception {  JSONObjectrequestBody=newJSONObject();  // MODEL :ollama上运行的大模型名称 类似:qwen3.5:27b     requestBody.put("model", MODEL);      requestBody.put("messages", messages);      requestBody.put("stream"false);      logger.debug("Request body: {}", requestBody.toJSONString());  // OLLAMA_BASE_URL:类似 http://localhost:11434URLurl=newURL(OLLAMA_BASE_URL + "/api/chat");  // 发送http请求HttpURLConnectionconn= (HttpURLConnection) url.openConnection();      conn.setRequestMethod("POST");      conn.setRequestProperty("Content-Type""application/json");      conn.setDoOutput(true);  // 连接超时时间 1min     conn.setConnectTimeout(60*1000);  // 读取超时时间 5min    conn.setReadTimeout(5*60*1000);  // 处理返回数据try (OutputStreamos= conn.getOutputStream()) {  byte[] input = requestBody.toString().getBytes(StandardCharsets.UTF_8);          os.write(input, 0, input.length);      }  intresponseCode= conn.getResponseCode();  if (responseCode != 200) {  StringBuildererrorMsg=newStringBuilder();  try (BufferedReaderbr=newBufferedReader(  newInputStreamReader(conn.getErrorStream(),StandardCharsets.UTF_8))){              String line;  while((line=br.readLine())!=null){                  errorMsg.append(line);              }          }  thrownewException("Ollama API error "+responseCode+": "+errorMsg);      }  // 处理结果,返回的前端展示  StringBuilderresponse=newStringBuilder();  try (BufferedReaderbr=newBufferedReader(  newInputStreamReader(conn.getInputStream(),StandardCharsets.UTF_8))){          String line;  while((line=br.readLine())!=null){              response.append(line);          }      }  JSONObjectjsonResponse= JSON.parseObject(response.toString());  JSONObjectassistantMessage= jsonResponse.getJSONObject("message");  return assistantMessage.getString("content");  }

3、运行代码,开始聊天,结果发现它没有上下文记忆

4、很明显,它记不住上下文。为了隔离不同用户的聊天内容,我们使用SessionId来区别

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
// 在controller请求中try {  StringsessionId= getOrCreateSessionId(request);      logger.debug("sessionId={}", sessionId);  return aiChatService.chat(sessionId,message);  catch (Exception e) {  return"error: " + e.getMessage();  }/**   * 用户的会话隔离   * @param request   * @return */  private String getOrCreateSessionId(HttpServletRequest request) {  HttpSessionsession= request.getSession();  return"ai_" + session.getId();  }

5、在Service中,每次请求前,先获取当前SessionId下的会话消息集合,再将用户最新输入问题添加到消息集合的最后,AI 回复后,最后将回复内容也添加到集合中。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
/**   * 根据sessionId 获取会话记录 * @param sessionId * @return */  privatesynchronized List<JSONObject> getSessionMessages(String sessionId) {  // 清除过期的session消息    cleanupExpiredSessions();  SessionContextcontext= sessionContexts.computeIfAbsent(sessionId, k -> {  SessionContextnewContext=newSessionContext();  return newContext;      });      context.updateAccessTime();  return context.messages;  }/**   * 聊天 */  public String chat(String sessionId, String message) {  // 根据sessionId获取会话记录;    List<JSONObject> messages = getSessionMessages(sessionId);  JSONObjectuserJSON=newJSONObject();      userJSON.put("role""user");      userJSON.put("content", message);  // 将当前用户发送的消息加入到集合中    addMessageToSession(sessionId, userJSON);  try {  Stringresult= chatWithoutTools(messages);  JSONObjectassistantMsg=newJSONObject();          assistantMsg.put("role""assistant");          assistantMsg.put("content", result);  // 将ai 回复的问题也添加到session消息集合中;        addMessageToSession(sessionId, assistantMsg);  return result;      } catch (Exception e) {          logger.error("AI聊天异常", e);  return"error: " + e.getMessage();      }  }

6、这样下来,session消息集合的内容会越来越多,上下文窗口会被撑爆,所以需要加上过期时间处理,同时会话列表也只记录最新的10条或者20条内容即可。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
// 上下文存储的最大的消息数 20privatestaticfinalintMAX_HISTORY_MESSAGES=20;  // session 过期时间30min  privatestaticfinallongSESSION_TIMEOUT_MS=30 * 60 * 1000;/**   * 清除过期的会话消息   */  privatevoidcleanupExpiredSessions() {      sessionContexts.entrySet().removeIf(entry -> entry.getValue().isExpired());  }// 存储消息内容的map,key是sessionId, value 是SessionContext ,存储会话的消息集合; privatefinal Map<String, SessionContext> sessionContexts = newConcurrentHashMap<>();  // 创建静态类SessionContext  privatestaticclassSessionContext {      List<JSONObject> messages = newArrayList<>();  longlastAccessTime= System.currentTimeMillis();  voidupdateAccessTime() {  this.lastAccessTime = System.currentTimeMillis();      }    booleanisExpired() {  return System.currentTimeMillis() - lastAccessTime > SESSION_TIMEOUT_MS;      }  }/**   * 将ai 回复的内容加上到session   */  privatevoidaddMessageToSession(String sessionId, JSONObject message) {      List<JSONObject> messages = getSessionMessages(sessionId);      messages.add(message);  // 当消息条数大于我们设置的20条的时候,不再添加到消息集合中;while (messages.size() > MAX_HISTORY_MESSAGES + 1) {  if (messages.size() > 1) {              messages.remove(1);          }      }  }

7、这样通过sessionId 和集合方式,就实现了让AI 记住了上下文。本质上也很简单:通过程序记录每次用户输入的问题和AI回复的内容,当用户新问题的时候,将之前记录的问过的内容和新的问题拼接到一起,发送给AI 

三、通过Function Calling 连接MySQL数据库

1、定义tools 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
/**   * 获取工具定义   * 手写的JSON 告诉Ollama 我们有一个查询项目的工具;   **/private JSONArray getToolsDefinition() {  JSONArraytools=newJSONArray();  JSONObjectqueryProject=newJSONObject();      queryProject.put("type""function");  JSONObjectfunction=newJSONObject();  // tools 名称是 query_articlefunction.put("name", "query_article");      function.put("description""根据关键词查询文章的标题,内容和摘要信息");  JSONObjectparameters=newJSONObject();      parameters.put("type""object");  JSONObjectproperties=newJSONObject();  JSONObjectkeywordProp=newJSONObject();      keywordProp.put("type""string");      keywordProp.put("description""查询关键词,如标题、摘要等");      properties.put("keyword", keywordProp);      parameters.put("properties", properties);  JSONArrayrequiredArr=newJSONArray();      requiredArr.add("keyword");      parameters.put("required", requiredArr);      function.put("parameters", parameters);      queryProject.put("function", function);      tools.add(queryProject);  return tools;  }

2、添加一个系统提示词

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
/**   * 创建一个AI 系统提示词,每次用户的问题都会加上这个,是给AI看的,让他遵守这些规则; */private JSONObject createSystemMessage() {  JSONObjectsystemMsg=newJSONObject();      systemMsg.put("role""system");      systemMsg.put("content""你是一个智能助手。当用户询问AI资讯时,必须调用query_article函数查询数据库。禁止向数据库添加或者修改数据。");  return systemMsg;  }/**   * 使用的时候 */privatesynchronized List<JSONObject> getSessionMessages(String sessionId) {      cleanupExpiredSessions();  SessionContextcontext= sessionContexts.computeIfAbsent(sessionId, k -> {  SessionContextnewContext=newSessionContext();  //系统提示词,加到这里;        newContext.messages.add(createSystemMessage());  return newContext;      });      context.updateAccessTime();  return context.messages;  }

3、使用Function Calling 来调用MySQL数据库

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
/*** 注意:之前方法名是 chatWithoutTools,这里改成了 chatWithTools* 请求中只是加上了一个属性 requestBody.put("tools", getToolsDefinition());  **/private String chatWithTools(List<JSONObject> messages)throws Exception {  JSONObjectrequestBody=newJSONObject();      requestBody.put("model", MODEL);      requestBody.put("messages", messages);  // 上面定义的工具调用方法     requestBody.put("tools", getToolsDefinition());      requestBody.put("stream"false);      logger.debug("Request body: {}", requestBody.toJSONString());  URLurl=newURL(OLLAMA_BASE_URL + "/api/chat");  HttpURLConnectionconn= (HttpURLConnection) url.openConnection();      conn.setRequestMethod("POST");      conn.setRequestProperty("Content-Type""application/json");      conn.setDoOutput(true);      conn.setConnectTimeout(60000);      conn.setReadTimeout(300000);  // 发送请求  try (OutputStreamos= conn.getOutputStream()) {  byte[] input = requestBody.toString().getBytes(StandardCharsets.UTF_8);          os.write(input, 0, input.length);      }  intresponseCode= conn.getResponseCode();      logger.debug("Ollama response code: {}", responseCode);  if (responseCode != 200) {  StringBuildererrorMsg=newStringBuilder();  try (BufferedReaderbr=newBufferedReader(  newInputStreamReader(conn.getErrorStream(),StandardCharsets.UTF_8))){              String line;  while((line=br.readLine())!=null){                  errorMsg.append(line);              }          }          logger.error("Ollama API error: {}", errorMsg);  thrownewException("Ollama API error "+responseCode+": "+errorMsg);      }  StringBuilderresponse=newStringBuilder();  try (BufferedReaderbr=newBufferedReader(  newInputStreamReader(conn.getInputStream(),StandardCharsets.UTF_8))){          String line;  while((line=br.readLine())!=null){              response.append(line);          }      }      logger.debug("Ollama response: {}", response.toString());  JSONObjectjsonResponse= JSON.parseObject(response.toString());  JSONObjectassistantMessage= jsonResponse.getJSONObject("message");  // 根据返回的结果,看看是否需要Function Calling ; tool_calls 是ollama 返回的一个属性,如果AI 根据系统提示词和用户输入判断需要方法调用,它会返回一个tool_calls属性的数组;JSONArraytoolCalls= assistantMessage.getJSONArray("tool_calls");  if (toolCalls != null && !toolCalls.isEmpty()) {  JSONObjectassistantMsgWithTools=newJSONObject();          assistantMsgWithTools.put("role""assistant");          assistantMsgWithTools.put("content", assistantMessage.getString("content"));          assistantMsgWithTools.put("tool_calls", toolCalls);          messages.add(assistantMsgWithTools);  for(inti=0;i<toolCalls.size();i++){  JSONObjecttoolCall=toolCalls.getJSONObject(i);  StringfunctionName=toolCall.getJSONObject("function").getString("name");  JSONObjectarguments=JSON.parseObject(toolCall.getJSONObject("function").getString("arguments"));  // 执行MySQL ,取查询数据库,返回的结果,交给AI;StringtoolResult=executeFunction(functionName,arguments);  JSONObjecttoolMsg=newJSONObject();              toolMsg.put("role""tool");              toolMsg.put("tool_call_id", toolCall.getString("id"));              toolMsg.put("content", toolResult);              messages.add(toolMsg);          }  // 再次发起AI请求,将MySQL 获取的内容交给AI 处理后返给前端用户;return chatWithTools(messages);      }  return assistantMessage.getString("content");  }

4、从数据库查找

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
/**   * 执行函数调用 */  private String executeFunction(String functionName, JSONObject arguments) {  Stringkeyword= arguments.getString("keyword");  if (!validateKeyword(keyword)) {          logger.warn("无效的查询参数: {}", keyword);  return"错误:查询参数无效";      }      logger.info("AI执行查询操作 - 函数: {}, 关键词: {}", functionName, keyword);  try {  return executeReadOnlyQuery(keyword);      } catch (Exception e) {          logger.error("查询执行异常", e);  return"查询异常: " + e.getMessage();      }  }/**   * 从数据库查找   * @param func   * @param keyword   * @return */  private String executeReadOnlyQuery(String keyword) {  // 数据库对应的实体类 Articlequery=newArticle();  // mybatis sql        query.sqlMap().getWhere().disableAutoAddStatusWhere();          query.sqlMap().getWhere()     .or("title",com.jeesite.common.mybatis.mapper.query.QueryType.LIKE,keyword)     .or("description",com.jeesite.common.mybatis.mapper.query.QueryType.LIKE,keyword);          query.setPage(newcom.jeesite.common.entity.Page<>(1, MAX_RESULTS));          List<Article> list = articleService.findList(query);  if(list==null||list.isEmpty()){  return"未找到匹配的文章信息";          }  StringBuildersb=newStringBuilder();          sb.append("标题 | 描述\n");          sb.append("--- | ---\n");  for (Article article : list) {     sb.append(escapeOutput(article.getTitle()!=null?article.getTitle():"-"))     .append(" | ")     .append(escapeOutput(article.getDescription()!=null?article.getDescription():"-"))     .append("\n");          }  return sb.toString();  }

5、运行后当用户输入AI 资讯相关内容时,AI 将会查询数据库,返回结果

6、从控制台日志也能看清整个过程:第一次请求时添加了系统提示词,返回的数据有tool_calls 属性,表示需要执行Function Calling ,去查询MySQL数据库,获取到数据库中两条数据后,返送第二次请求,然后AI处理后,返回给用户

四、小结
  1. 上面就是纯java原生来实现的一个Ai 聊天小助手,代码基本上也是完整的。相信看到这里对于Ai 上下文,提示词、Function Calling 的理解会更深一点了吧。
  2. Function Calling 和MCP 有什么区别呢?我这里是Function Calling 的实现,我在请求中告诉Ollama ,我有这些工具函数,然后大模型返回tool_calls ,我解析tool_calls, 手动去调用函数,然后将结果返回AI。
  3. MCP 呢?之前pyhon编写的内容已经说明了。它是让AI 通过统一的接口去发现和调用远程工具,不需要我去手动拼接、解析和处理。mcp服务帮我完成了这些工具。

今天内容就到此了,纯原生的写法还是比较费时间的,我也正在学习使用Langchain4j 框架,发现确实简单很多,也在尝试实现更多复杂一些的功能,还有安全方面的考虑。感谢关注,下次再见。

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-06-11 10:13:43 HTTP/1.1 GET : https://www.yeyulingfeng.com/a/619416.html
  2. 运行时间 : 0.232203s [ 吞吐率:4.31req/s ] 内存消耗:4,864.76kb 文件加载:145
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=ea2cbeff8083e884904d98d5297bac7d
  1. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/composer/autoload_static.php ( 6.05 KB )
  7. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/ralouphie/getallheaders/src/getallheaders.php ( 1.60 KB )
  10. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  11. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  12. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  13. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  14. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  15. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  16. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  17. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  18. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  19. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/guzzlehttp/guzzle/src/functions_include.php ( 0.16 KB )
  21. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/guzzlehttp/guzzle/src/functions.php ( 5.54 KB )
  22. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  23. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  24. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  25. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/provider.php ( 0.19 KB )
  26. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  27. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  28. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  29. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/common.php ( 0.03 KB )
  30. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  32. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/alipay.php ( 3.59 KB )
  33. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  34. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/app.php ( 0.95 KB )
  35. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/cache.php ( 0.78 KB )
  36. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/console.php ( 0.23 KB )
  37. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/cookie.php ( 0.56 KB )
  38. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/database.php ( 2.48 KB )
  39. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/filesystem.php ( 0.61 KB )
  40. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/lang.php ( 0.91 KB )
  41. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/log.php ( 1.35 KB )
  42. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/middleware.php ( 0.19 KB )
  43. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/route.php ( 1.89 KB )
  44. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/session.php ( 0.57 KB )
  45. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/trace.php ( 0.34 KB )
  46. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/config/view.php ( 0.82 KB )
  47. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/event.php ( 0.25 KB )
  48. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  49. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/service.php ( 0.13 KB )
  50. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/AppService.php ( 0.26 KB )
  51. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  52. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  53. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  54. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  55. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  56. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/services.php ( 0.14 KB )
  57. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  58. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  59. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  60. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  61. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  62. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  63. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  64. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  65. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  66. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  67. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  68. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  69. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  70. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  71. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  72. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  73. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  74. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  75. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  76. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  77. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  78. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  79. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  80. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  81. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  82. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  83. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  84. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  85. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  86. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  87. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/Request.php ( 0.09 KB )
  88. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  89. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/middleware.php ( 0.25 KB )
  90. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  91. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  92. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  93. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  94. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  95. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  96. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  97. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  98. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  99. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  100. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  101. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  102. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  103. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/route/app.php ( 3.94 KB )
  104. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  105. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  106. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/controller/Index.php ( 9.87 KB )
  108. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/BaseController.php ( 2.05 KB )
  109. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  110. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  111. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  112. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  113. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  114. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  115. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  116. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  117. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  118. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  119. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  120. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  121. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  122. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  123. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  124. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  125. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  126. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  127. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  128. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  129. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  130. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  131. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  132. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  133. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  134. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  135. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/app/controller/Es.php ( 3.30 KB )
  136. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  137. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  138. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  139. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  140. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  141. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  142. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  143. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  144. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/runtime/temp/c935550e3e8a3a4c27dd94e439343fdf.php ( 31.50 KB )
  145. /yingpanguazai/ssd/ssd1/www/wwww.yeyulingfeng.com/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.001294s ] mysql:host=127.0.0.1;port=3306;dbname=wenku;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.002832s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000776s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000767s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.002450s ]
  6. SELECT * FROM `set` [ RunTime:0.000567s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.002081s ]
  8. SELECT * FROM `article` WHERE `id` = 619416 LIMIT 1 [ RunTime:0.001774s ]
  9. UPDATE `article` SET `lasttime` = 1781144023 WHERE `id` = 619416 [ RunTime:0.001828s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 64 LIMIT 1 [ RunTime:0.000801s ]
  11. SELECT * FROM `article` WHERE `id` < 619416 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.001289s ]
  12. SELECT * FROM `article` WHERE `id` > 619416 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.001402s ]
  13. SELECT * FROM `article` WHERE `id` < 619416 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.002574s ]
  14. SELECT * FROM `article` WHERE `id` < 619416 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.002637s ]
  15. SELECT * FROM `article` WHERE `id` < 619416 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.002419s ]
0.236193s