结组作业8

今天又写了一部分代码,用于对数据库内容的增加和查询

 1 package org.test.tongyuzhe.Controller;
 2 
 3 
 4 import org.springframework.web.bind.annotation.RequestBody;
 5 import org.test.tongyuzhe.Pojo.Chat;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.web.bind.annotation.PostMapping;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import org.springframework.web.bind.annotation.RestController;
11 import org.test.tongyuzhe.Pojo.Result;
12 import org.test.tongyuzhe.Service.ChatService;
13 
14 import java.util.List;
15 
16 /**
17  * <p>
18  *  前端控制器
19  * </p>
24 @RestController
25 @RequestMapping("/message")
26 public class ChatController1 {
27     @Autowired
28     ChatService chatService;
29     @PostMapping("/getMessage")
30     public Result<Object> getMessage(Integer userId)
31     {
32         List<Chat> list=chatService.getMessage(userId);
33         return Result.success(list);
34     }
35     @PostMapping("/insertMessage")
36     public Result<Object> insertMessage(@RequestBody Chat chat){
37         chatService.insertMessage(chat);
38         return Result.success();
39     }
40 }
 1 package org.test.tongyuzhe.Mapper;
 2 
 3 import org.apache.ibatis.annotations.Insert;
 4 import org.apache.ibatis.annotations.Mapper;
 5 import org.apache.ibatis.annotations.Select;
 6 import org.test.tongyuzhe.Pojo.Chat;
 7 
 8 import java.util.List;
 9 
10 @Mapper
11 public interface ChatMapper {
12     @Select("select * from chat where userId=#{userId}")
13     List<Chat> getMessage(Integer userId);
14     @Insert("insert into chat(userId, request, response, time) VALUES (#{userId},#{request},#{response},#{time})")
15     void insertMessage(Chat chat);
16 }
 1 package org.test.tongyuzhe.Service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 import org.test.tongyuzhe.Mapper.ChatMapper;
 6 import org.test.tongyuzhe.Pojo.Chat;
 7 
 8 import java.util.List;
 9 
10 @Service
11 public class ChatService {
12     @Autowired
13     ChatMapper chatMapper;
14 
15     /**
16      * 通过userId来获取当前用户的历史问问题记录
17      * @param userId 用户id
18      * @return 问题和回答列表信息
19      */
20     public List<Chat> getMessage(Integer userId) {
21         List<Chat> list=chatMapper.getMessage(userId);
22         return list;
23     }
24 
25     /**
26      * 添加message(也就是保存用户提出的问题和答案等信息)
27      * @param chat
28      */
29     public void insertMessage(Chat chat) {
30        chatMapper.insertMessage(chat);
31     }
32 }

 

posted @ 2024-05-11 16:54  连师傅只会helloword  阅读(1)  评论(0编辑  收藏  举报