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"