• 创建我的空间和发帖功能


    建表 -> 实体类 -> Mapper -> Service -> Controller

    pojo

    1. package com.kill9.backend.pojo;
    2. import com.baomidou.mybatisplus.annotation.IdType;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.fasterxml.jackson.annotation.JsonFormat;
    5. import lombok.AllArgsConstructor;
    6. import lombok.Data;
    7. import lombok.NoArgsConstructor;
    8. import java.util.Date;
    9. @Data
    10. @NoArgsConstructor
    11. @AllArgsConstructor
    12. public class Activity {
    13. @TableId(type = IdType.AUTO)
    14. private Integer id;//在pojo里最好用Integer,否则会报警告
    15. private Integer userId;//pojo里要用驼峰命名法和数据库的下划线对应
    16. private String photo;
    17. private String content;
    18. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "Asia/Shanghai")
    19. private Date createtime;
    20. }

     mapper

    1. package com.kill9.backend.mapper;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import org.apache.ibatis.annotations.Mapper;
    4. @Mapper
    5. public interface ActivityMapper extends BaseMapper<ActivityMapper> {
    6. }

    service

    1. package com.kill9.backend.service.impl.user.activity;
    2. import com.kill9.backend.mapper.ActivityMapper;
    3. import com.kill9.backend.pojo.Activity;
    4. import com.kill9.backend.pojo.User;
    5. import com.kill9.backend.service.impl.utils.UserDetailsImpl;
    6. import com.kill9.backend.service.user.activity.AddActivityService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    9. import org.springframework.security.core.context.SecurityContextHolder;
    10. import org.springframework.stereotype.Service;
    11. import java.util.Date;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. @Service
    15. public class AddActivityServiceImpl implements AddActivityService {
    16. @Autowired
    17. private ActivityMapper activityMapper;
    18. @Override
    19. public Map<String, String> add(Map<String, String> data) {
    20. UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    21. UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
    22. User user = loginUser.getUser();
    23. Map<String,String> map = new HashMap<>();
    24. String content = data.get("content");
    25. if (content == null || content.length() == 0) {
    26. map.put("error_message", "内容不能为空");
    27. return map;
    28. }
    29. Date date = new Date();
    30. Activity activity = new Activity(null,user.getId(),user.getPhoto(),content,date);
    31. activityMapper.insert(activity);
    32. map.put("error_message","success");
    33. return map;
    34. }
    35. }

    controller

    1. package com.kill9.backend.controller.user;
    2. import com.kill9.backend.service.user.activity.AddActivityService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.PostMapping;
    5. import org.springframework.web.bind.annotation.RequestParam;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import java.util.Map;
    8. @RestController
    9. public class AddActivityController {
    10. @Autowired
    11. private AddActivityService addActivityService;
    12. @PostMapping("/user/activity/add")
    13. public Map add(@RequestParam Map data){
    14. return addActivityService.add(data);
    15. }
    16. }

    实现前端个人页面展示

    navbar

    1. <template>
    2. <nav class="navbar navbar-expand-lg navbar-light bg-light">
    3. <div class="container">
    4. <a class="navbar-brand">MySpacea>
    5. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
    6. <span class="navbar-toggler-icon">span>
    7. button>
    8. <div class="collapse navbar-collapse" id="navbarText">
    9. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
    10. <li class="nav-item">
    11. <a class="nav-link active" aria-current="page">Homea>
    12. li>
    13. <li class="nav-item">
    14. <a class="nav-link" >Friendsa>
    15. li>
    16. ul>
    17. <ul class="navbar-nav" v-if="!$store.state.user.is_login">
    18. <li class="nav-item">
    19. <a class="nav-link active" aria-current="page" >Logina>
    20. li>
    21. <li class="nav-item">
    22. <a class="nav-link" >Registera>
    23. li>
    24. ul>
    25. <ul class="navbar-nav" v-else>
    26. <li class="nav-item">
    27. <a class="nav-link active" aria-current="page" >
    28. a>
    29. li>
    30. <li class="nav-item">
    31. <a class="nav-link" style="cursor: pointer;">Exita>
    32. li>
    33. ul>
    34. div>
    35. div>
    36. nav>
    37. template>
    38. <script>
    39. import {useStore} from 'vuex';
    40. export default{
    41. name:"NavBar",
    42. setup() {
    43. const store = useStore();
    44. const logout = () =>{
    45. store.commit('logout');
    46. };
    47. return {
    48. logout,
    49. }
    50. }
    51. }
    52. script>
    53. <style scoped>
    54. style>

    userinfoindexview.vue

    1. <template>
    2. <NavBar/>
    3. template>
    4. <script>
    5. import NavBar from '@/components/userinfo/NavBar.vue';
    6. export default{
    7. components:{
    8. NavBar,
    9. }
    10. }
    11. script>
    12. <style scoped>
    13. style>

     完成后端三个api 增删查 ,不需要修改帖子

    1 add

    1. package com.kill9.backend.service.impl.user.activity;
    2. import com.kill9.backend.mapper.ActivityMapper;
    3. import com.kill9.backend.pojo.Activity;
    4. import com.kill9.backend.pojo.User;
    5. import com.kill9.backend.service.impl.utils.UserDetailsImpl;
    6. import com.kill9.backend.service.user.activity.AddActivityService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    9. import org.springframework.security.core.context.SecurityContextHolder;
    10. import org.springframework.stereotype.Service;
    11. import java.util.Date;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. @Service
    15. public class AddActivityServiceImpl implements AddActivityService {
    16. @Autowired
    17. private ActivityMapper activityMapper;
    18. @Override
    19. public Map<String, String> add(Map<String, String> data) {
    20. UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    21. UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
    22. User user = loginUser.getUser();
    23. Map<String,String> map = new HashMap<>();
    24. String content = data.get("content");
    25. if (content == null || content.length() == 0) {
    26. map.put("error_message", "内容不能为空");
    27. return map;
    28. }
    29. Date date = new Date();
    30. Activity activity = new Activity(null,user.getId(),user.getPhoto(),content,date);
    31. activityMapper.insert(activity);
    32. map.put("error_message","success");
    33. return map;
    34. }
    35. }

    2 getlist

    1. package com.kill9.backend.service.impl.user.activity;
    2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    3. import com.kill9.backend.mapper.ActivityMapper;
    4. import com.kill9.backend.pojo.Activity;
    5. import com.kill9.backend.pojo.User;
    6. import com.kill9.backend.service.impl.utils.UserDetailsImpl;
    7. import com.kill9.backend.service.user.activity.GetActivityListService;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    10. import org.springframework.security.core.context.SecurityContextHolder;
    11. import org.springframework.stereotype.Service;
    12. import java.util.List;
    13. @Service
    14. public class GetActivityListServiceImpl implements GetActivityListService {
    15. @Autowired
    16. private ActivityMapper activityMapper;
    17. @Override
    18. public List getList() {
    19. UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    20. UserDetailsImpl loginUser =(UserDetailsImpl) authenticationToken.getPrincipal();
    21. User user = loginUser.getUser();
    22. QueryWrapper queryWrapper = new QueryWrapper<>();
    23. queryWrapper.eq("user_id",user.getId());
    24. return activityMapper.selectList(queryWrapper);
    25. }
    26. }

    3 delete

    1. package com.kill9.backend.service.impl.user.activity;
    2. import com.kill9.backend.mapper.ActivityMapper;
    3. import com.kill9.backend.pojo.Activity;
    4. import com.kill9.backend.pojo.Bot;
    5. import com.kill9.backend.pojo.User;
    6. import com.kill9.backend.service.impl.utils.UserDetailsImpl;
    7. import com.kill9.backend.service.user.activity.RemoveActivityService;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    10. import org.springframework.security.core.context.SecurityContextHolder;
    11. import org.springframework.stereotype.Service;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. @Service
    15. public class RemoveActivityServiceImpl implements RemoveActivityService {
    16. @Autowired
    17. private ActivityMapper activityMapper;
    18. @Override
    19. public Map<String, String> remove(Map<String, String> data) {
    20. UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    21. UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
    22. User user = loginUser.getUser();
    23. int activity_id = Integer.parseInt(data.get("activity_id"));
    24. Map<String,String> map = new HashMap<>();
    25. Activity activity = activityMapper.selectById(activity_id);
    26. if (activity == null) {
    27. map.put("error_message", "帖子不存在或已被删除");
    28. return map;
    29. }
    30. if (!activity.getUserId().equals(user.getId())) {
    31. map.put("error_message", "没有权限删除该帖子");
    32. return map;
    33. }
    34. activityMapper.deleteById(activity_id);
    35. map.put("error_message", "success");
    36. return map;
    37. }
    38. }

    实现前端