SpringAI基于Mysql jdbc方式存储对话记忆

SpringAI基于Mysql jdbc方式存储对话记忆 版本SpringAI1.1.6SpringBoot3.5.14JdbcChatMemoryRepositoryJdbcChatMemoryRepository是一个内置实现使用 JDBC 将消息存储在关系数据库中。下面所有配置完成后应用会在启动时会读取内置建表语句schema-mysql.sql自动创建一张名为spring_ai_chat_memory的表用于存储对话记录IDEA中双击shift搜索schema-mysql.sql就能看到建表语句。使用引入依赖!-- 引入jdbc --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-chat-memory-repository-jdbc/artifactId/dependency!-- 引入mysql --dependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependency修改配置使用的是阿里云百炼平台中的大模型配置如下spring.ai.chat.memory.repository.jdbc.initialize-schema有三个选项embedded默认值只能用于嵌入式数据库如 H2、HSQL、Derby 等always自动使用内置sql创建表never不初始化手动创建自定义表spring.ai.chat.memory.repository.jdbc.platform的值是你的数据库类型当initialize-schemaalways时springai会根据platform使用对应的schema-{platform}.sql文件建表spring.ai.openai.chat.api-keyspring.ai.openai.chat.base-urlhttps://dashscope.aliyuncs.com/compatible-mode spring.ai.openai.chat.options.modeldeepseek-v4-flash spring.ai.chat.memory.repository.jdbc.initialize-schemaalways spring.ai.chat.memory.repository.jdbc.platformmysql spring.datasource.urljdbc:mysql://127.0.0.1:3306/ai_demo spring.datasource.usernameroot spring.datasource.password1111spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver代码packagecom.cry.chatmemorymysql.controller;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;importorg.springframework.ai.chat.memory.ChatMemory;importorg.springframework.ai.chat.memory.MessageWindowChatMemory;importorg.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;importorg.springframework.ai.chat.messages.Message;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;RestControllerpublicclassChatMemoryMysqlController{privateChatClientchatClient;privateJdbcChatMemoryRepositorychatMemoryRepository;publicChatMemoryMysqlController(ChatClient.Builderbuilder,JdbcChatMemoryRepositorychatMemoryRepository){this.chatMemoryRepositorychatMemoryRepository;// 创建聊天存储器ChatMemorychatMemoryMessageWindowChatMemory.builder().chatMemoryRepository(chatMemoryRepository).maxMessages(5)// 数据库中同一id最多保存5条.build();this.chatClientbuilder.defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())// 使用聊天存储器.build();}GetMapping(/chat)publicStringchat(RequestParam(valuemessage,defaultValue你是谁)Stringmessage,RequestParam(valuechatId,defaultValue1)StringchatId){returnchatClient.prompt(message)// 设置会话id.advisors(advisorSpec-advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId)).call().content();}GetMapping(/chat/list)publicListStringchatList(){returnchatMemoryRepository.findConversationIds();}GetMapping(/chat/history)publicListMessagechatHistory(RequestParam(valuechatId,defaultValue1)StringchatId){returnchatMemoryRepository.findByConversationId(chatId);}}验证http://localhost:8080/chat?message我是cccchatId1http://localhost:8080/chat?message我是谁chatId1