JSD-2204-微博项目(完结)-Day16

本文档详述了一个微博项目的完整构建过程,涵盖了从创建数据库和表,到实现用户注册登录、发布及查看微博、记住登录状态、删除微博、评论功能以及评论列表的显示。涉及的技术包括前端页面、控制器、实体类和数据映射层的创建与交互。
摘要由CSDN通过智能技术生成

1.微博项目

1.1准备工作:

  • 创建boot6工程 步骤和其它工程一样
  • application.properties里面添加和boot5一样的内容
  • 创建数据库和表
  •         create database weibo charset=utf8;
  •         use weibo;
  •         create table user(id int primary key auto_increment,username varchar(50),password         varchar(50),nick varchar(50));

1.2注册登录功能:

  • 创建reg.html页面参考boot3工程中的内容 注册时向/reg发出请求
  • 创建controller.UserController 添加reg方法处理/reg请求

1.3创建User实体类, 声明在reg方法的参数列表处

  • 创建mapper.UserMapper 添加insert和selectByUsername方法
  • 在Controller里面调用mapper的方法
  • 创建login.html页面 向/login发出请求
  • 在Controller里面添加login方法处理/login请求, 在方法中调用mapper中的方法 参考boot3中代码

1.4实现记住登录状态:

  • HttpSession对象的作用是: 同一个客户端多次请求会共享同一个Session对象,把数据放到Session对象里面可以将此数据在多次请求之间共享.

         

  • 在登录成功时,将当前登录成功的用户对象保存到会话对象中, 之后的请求如果需要判断是否登录或者需要用到当前登录的用户信息的话,直接从Session会话对象中获取即可.

1.5创建微博表

  • use weibo;
  • create table weibo(id int primary key auto_increment,content varchar(200),url varchar(200),created timestamp,user_id int,nick varchar(50));

1.6发布微博步骤:

  • 创建send.html页面
  • 创建WeiboController,添加处理添加weibo请求的方法
  • 创建Weibo实体类
  • 创建WeiboMapper 准备添加weibo的insert方法

1.7首页显示微博步骤:

  • 在首页vue里面准备arr数组, created里面发出获取所有weibo的请求 将得到的数据给到arr数组,在页面中遍历arr数组显示微博信息
  • 在WeiboController里面处理查询微博的请求 调用mapper里面的方法得到数据 响应给页面
  • 实现mapper里面的查询方法

1.8我的微博步骤:

  • 首页添加我的微博超链接 请求/myList.html
  • 创建mylist.html页面 , 在created方法中发出请求获取当前登录用户的所有微博
  • 在WeiboController里面添加selectMy方法处理请求 先拿到当前登录的用户对象, 通过当前用户的id查询相关weibo 响应给客户端
  • 实现mapper中的selectByUserId方法

1.9删除微博步骤:

  • 和之前的基础增删改查一样

1.10微博详情页面步骤:

  • 给首页微博信息处添加超链接 请求/detail.html页面 同时把weibo的id传递过去
  • 创建detail.html页面 在created里面通过weibo的id查询微博的详细信息 赋值给data里面的weibo对象 页面内容和此对象进行绑定 显示出weibo信息
  • WeiboController里面添加 方法处理通过id查询的请求, 调用mapper通过id查询微博信息的方法 把返回的微博对象响应给客户端
  • 实现mapper里面的方法

1.11发评论功能步骤:

  • 在detail.html页面中添加文本框和按钮 点击按钮时向/comment/insert发请求,把评论相关的信息提交,
  • 创建CommentController和Comment实体类 CommentMapper 处理添加评论的请求

1.12显示评论列表步骤:

  • 在detail.html页面中的created方法中 向 /comment/select地址发出请求 同时将当前页面的微博id作为参数传递过去
  • 在CommentController里面 处理/comment/select请求, 方法中调用mapper里面的selectByWeiboId方法通过weibo的id查询到所有相关的评论, 把得到的List集合响应给客户端
  • 实现mapper里面的方法

1.13项目目录

1.14代码展示

1.14.1CommentController.java(评论事务控制层)

package cn.tedu.boot6.controller;

import cn.tedu.boot6.entity.Comment;
import cn.tedu.boot6.entity.User;
import cn.tedu.boot6.mapping.CommentMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {

    @Autowired
    CommentMapping mapping;

    @RequestMapping("/comment/insert")
    public int commentInsert(@RequestBody Comment comment, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user==null){
            return 2;
        }
        comment.setCreated(new Date());
        comment.setNick(user.getNick());
        System.out.println("comment = " + comment );
        mapping.commentInsert(comment);
        return 1;
    }

    @RequestMapping("/comment/selectByWeiboId")
    public List<Comment> selectByWeiboId(int id){
        return mapping.selectByWeiboId(id);
    }

}

1.14.2UploadController.java(文件上传业务)

package cn.tedu.boot6.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {

    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        //得到文件的原始文件名字
        String fileName = picFile.getOriginalFilename();
        //得到后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一未见名
        fileName = UUID.randomUUID()+suffix;
        //文件夹路径
        String dirPath = "E:/files";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //完整的文件路径
        String filePath = dirPath+"/"+fileName;
        //吧文件的路径
        picFile.transferTo(new File(filePath));
        //吧文件路径返回给客户端
        return "/"+fileName;
    }

    @RequestMapping("/remove")
    public void remove(String url){
        //得到文件的完整路径
        String filePath = "E:/files"+url;
        //删除图片文件
        new File(filePath).delete();
    }

}

1.14.3UserController.java(用户事务控制层)

package cn.tedu.boot6.controller;

import cn.tedu.boot6.entity.User;
import cn.tedu.boot6.entity.WeiBo;
import cn.tedu.boot6.mapping.UserMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {

    @Autowired
    UserMapping mapping;

    @RequestMapping("/reg"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值