4. 用户信息查询功能设计与实现 4.1 功能描述 根据用户ID查询用户的基本信息,用于显示欢迎语等。 4.2 设计思路 前端从本地存储读取用户ID。…
- User
- 用户信息查询功能设计与实现 4.1 功能描述 根据用户ID查询用户的基本信息,用于显示欢迎语等。
4.2 设计思路 前端从本地存储读取用户ID。 发送GET请求带参数ID查询用户信息。 返回用户名,后端返回“未知用户”提示防止用户未登录或数据缺失。 4.3 处理流程
前端获取 userId → GET /user/info?id=userId → 后端查找用户 → 返回用户名或“未知用户” → 前端显示用户名 4.4 核心代码 后端 UserController.getUserInfo
@GetMapping("/info") public Map<String, Object> getUserInfo(@RequestParam Integer id) { User user = userService.getById(id); Map<String, Object> result = new HashMap<>(); if (user != null) { result.put("name", user.getUsername()); } else { result.put("name", "未知用户"); } return result; } 后端 UserService.getById
public User getById(Integer id) { return userMapper.findById(id); } 前端获取用户名示例 (Vue + axios)
const fetchUserInfo = async () => { if (!userId || isNaN(userId)) { username.value = '未登录'; return; } try { const response = await axios.get('http://localhost:8080/user/info', { params: { id: userId }}); username.value = response.data.name || '未知用户'; } catch (e) { username.value = '未知用户'; } }参考上面的文段,根据我给的资料,写给我。我的资料:package com.example.springboot.controller;
import com.example.springboot.entity.Notice; import com.example.springboot.service.NoticeService; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.*;
import java.util.Date; import java.util.List;
@RestController @RequestMapping("/notices") public class NoticeController {
private final NoticeService noticeService; public NoticeController(NoticeService noticeService) { this.noticeService = noticeService; } /** * 新增公告 */ @PostMapping public Notice create(@RequestBody Notice notice) { return noticeService.createNotice(notice); } /** * 修改公告 */ @PutMapping("/{id}") public Notice update(@PathVariable Long id, @RequestBody Notice notice) { notice.setId(id); return noticeService.updateNotice(notice); } /** * 删除公告 */ @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { noticeService.deleteNotice(id); } /** * 根据ID获取公告详情 */ @GetMapping("/{id}") public Notice get(@PathVariable Long id) { return noticeService.getNoticeById(id); } /** * 查询公告列表,支持模糊查询title和content,精确匹配发布时间(createTime) * */ @GetMapping public List<Notice> search( @RequestParam(required = false) String title, @RequestParam(required = false) String content, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime ) { return noticeService.searchNotices(title, content, createTime); }} package com.example.springboot.entity;
import java.util.Date;
public class Notice {
private Long id; private String title; private String content; private Date createTime; private Date updateTime; // Getter & Setter public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } - Assistant