发表于: 2021-02-06 23:29:59
1 1216
今天完成的事情:
留言回复的功能
添加work内容
添加工作室内容
服务器上运行
明天计划的事情:
查看Nginx的响应时间,每一个请求不超过200MS,查看每一个Service的响应时间,列出来每一个请求的时耗分解图
======
配置日志
遇到的问题:
找不到spring-mvc.xml,加上 *
===========
收获:
service实现类:
package com.kbk.service.Impl;
import com.kbk.dao.MessageMapper;
import com.kbk.model.Message;
import com.kbk.model.Title;
import com.kbk.service.MessageService;
import com.kbk.vo.BoyVo;
import com.kbk.vo.MessageTestVO;
import com.kbk.vo.ReplyVo;
import com.kbk.vo.TitleTestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
MessageMapper messageMapper;
@Override
public int insertMessage(Message message) {
return messageMapper.insertMessage(message);
}
@Override
public List<MessageTestVO> selectByWorkId(Long WorkId) {
//根据作品id查询所有留言
List<Message> messageList = messageMapper.selectByWorkId(WorkId);
if (null != messageList && !messageList.isEmpty()) {
//获取筛选出父类的数据,赋予给 messageParentList
List<Message> messageParentList = new ArrayList<>();
for (Message message : messageList) {
if (message.getReplyId() == 0) {
messageParentList.add(message);
}
}
//创建作品的所有留言类容
List<MessageTestVO> messageTestVOList = new ArrayList<>();
//将父类的数据(ID,workId)赋予给messageTestVO
for (Message parentMessage : messageParentList){
//将父类的数据赋予给 messageTestVOList(游客留言类容)
MessageTestVO messageTestVO = new MessageTestVO();
messageTestVO.setId(parentMessage.getId());
messageTestVO.setWorkId(parentMessage.getWorkId());
messageTestVO.setTouristName(parentMessage.getTouristName());
messageTestVO.setContents(parentMessage.getContents());
messageTestVO.setCreatedTime(parentMessage.getCreatedTime());
//创建一个回复类容集合,把筛选好的子类集合添加到这个集合里面
List<ReplyVo> replyVoList= new ArrayList<>();
for (Message boyMessage : messageList) {
if (parentMessage.getId() .equals(boyMessage.getReplyId())) {
ReplyVo replyVo = new ReplyVo();
replyVo.setId(boyMessage.getId());
replyVo.setReplyId(boyMessage.getReplyId());
replyVo.setContents(boyMessage.getContents());
replyVo.setTouristName(boyMessage.getTouristName());
replyVo.setCreatedTime(boyMessage.getCreatedTime());
replyVoList.add(replyVo);
}
}
//将子类的数据(ID和Name)赋予给父类的 boyVoList
messageTestVO.setReplyVo(replyVoList);
//将父类对象的数据赋予的父类
messageTestVOList.add(messageTestVO);
}
System.out.println("=============================="+messageTestVOList);
return messageTestVOList;
}else {
return null;
}
}
}
Controller:
// 根据作品id查询对应的留言、回复,递归遍历,前端返回作品id
@RequestMapping(value = "/api/message/{workId}",method = RequestMethod.GET)
@ResponseBody
//@PathVariable,用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数。
public Map<String,Object> selectMessageByWorkId(@PathVariable(value = "workId") Long workId){
List<MessageTestVO> message = messageService.selectByWorkId(workId);
if (null != message) {
return Restful.set(200, "查找title成功", message);
} else {
return Restful.set(404, "查询title失败");
}
}
POSTMAN测试:
======================
Controller:
/**
* post
* 添加work列表
*
* @return work
*/
// 添加banner接口,因为multipartFile需要用form-data格式表单处理数据,这里banner就不需要用@RequestBody注解
@RequestMapping(value = "/api/work/add",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> insertWork(@RequestParam("uploadFile") MultipartFile multipartFile, Work work) throws IOException {
// 调用 ImageUtil 类的图片上传方法,返回图片的名字
String coverName = ImageUtil.upload(multipartFile);
work.setPicture(coverName);
//默认下架架状态
work.setStatus(0);
work.setCreatedTime(System.currentTimeMillis());
work.setUpdatedTime(System.currentTimeMillis());
workService.insertSelective(work);
if(work != null){
return Restful.set(200, "添加work成功", work);
}else {
return Restful.set(404, "添加work失败");
}
}
POSTMAN测试:
数据库:
==========================
Controller:
package com.kbk.kbk_controller;
import com.kbk.Rest.Restful;
import com.kbk.model.Studio;
import com.kbk.service.StudioService;
import com.kbk.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
public class StudioController {
@Autowired
private StudioService studioService;
/**
* post
* 添加studio列表
*
* @return studio
*/
// 添加工作室简介,5个图片上传,因为multipartFile需要用form-data格式表单处理数据,这里banner就不需要用@RequestBody注解
@RequestMapping(value = "/api/studio/add",method = RequestMethod.POST)
@ResponseBody
//注意MultipartFile在这里是数组,上传五张图片
public Map<String , Object> insertStudio(@RequestParam("uploadFile") MultipartFile[] multipartFile, Studio studio) throws IOException{
List<String> imagesPathList = new ArrayList<>();
for (MultipartFile file : multipartFile) {
// 调用 ImageUtil 类的图片上传方法,返回图片的名字
String pictureName = ImageUtil.upload(file);
// 将图片地址添加入list集合中,最多5个地址
imagesPathList.add(pictureName);
}
// 将list集合转为String类型存储数据库
String picture = String.join("," , imagesPathList);
studio.setPicture(picture);
// 将String类型转为list集合,输出查看
List<String> listString = Arrays.asList(picture.split(","));
for (String string : listString) {
System.out.println(string);
}
//默认下架架状态
studio.setStatus(0);
studio.setCreatedTime(System.currentTimeMillis());
studio.setUpdatedTime(System.currentTimeMillis());
studioService.insertSelective(studio);
if(studio != null){
return Restful.set(200, "添加work成功", studio);
}else {
return Restful.set(404, "添加work失败");
}
}
}
POSTMAN测试:
数据库:
成功上传到本地:
=====================
在服务器上运行war包:
============
postman测试:
成功上传图片:
评论