发表于: 2021-01-29 23:46:15
3 1179
今天完成的事情:
写了message模块
Title模块
明天计划的事情:
继续写剩下的模块
遇到的问题:
没有添加@PathVariable,报错
收获:
message模块
package com.kbk.kbk_controller;
import com.kbk.Rest.Restful;
import com.kbk.model.Message;
import com.kbk.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class MessageController {
@Autowired
private MessageService messageService;
/**
* post
* 添加留言
*
* @param message
* @return
*/
@RequestMapping(value = "/api/message", method = RequestMethod.POST, consumes = "application/json")
//@ResponseBody 是将下面方法返回的参数(一般地, 返回的数据不是某个具体的视图页面, 而是某种格式的数据(json, xml等)),
// 转换为指定格式后, 写入到response对象的的body数据区。因为开启了springMVC的注解驱动,所以返回的是json格式的数据
//告诉spring-mvc框架 不进行视图跳转(不用跳到jsp去) 直接进行数据响应(直接返回当前数据)
public Map<String, Object> postContents(@RequestBody Message message) {
//@RequestBody 用于读取request请求的body部分的数据(浏览器传输过来的),解析后,把相应的数据绑定到请求处理方法的参数上
//判断留言是否为空
//判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
if (StringUtils.isBlank(message.getContents())) {
return Restful.set(404,"留言已添加");
} else {
message.setCreatedTime(System.currentTimeMillis());
messageService.insertMessage(message);
return Restful.set(200, "添加成功", message);
}
}
}
postman:
Title模块:
package com.kbk.kbk_controller;
import com.kbk.Rest.Restful;
import com.kbk.model.Title;
import com.kbk.service.Impl.TitleServiceImpl;
import com.kbk.service.TitleService;
import com.kbk.vo.TitleTestVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
public class TitleController {
@Autowired
private TitleServiceImpl titleService;
/**
* get
* 查询全部
*
* @param
* @return Title
*/
@GetMapping("/api/title")
public Map<String, Object> getFirst() {
//判断一级标题是否存在
List<TitleTestVo> title = titleService.selectOrTitle();
if (null == title) {
return Restful.set(404, "查询title失败");
} else {
return Restful.set(200, "查找title成功", title);
}
}
}
实现类:
package com.kbk.service.Impl;
import com.kbk.dao.TitleMapper;
import com.kbk.model.Title;
import com.kbk.service.TitleService;
import com.kbk.vo.BoyVo;
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;
@Service
public class TitleServiceImpl implements TitleService {
@Autowired
TitleMapper titleMapper;
@Override
public List<TitleTestVo> selectOrTitle() {
//获取数据库的内容
List<Title> titleList = titleMapper.selectFirst();
if (null != titleList && !titleList.isEmpty()) {
//获取筛选出父类的数据,赋予给 titleParentList
List<Title> titleParentList = new ArrayList<>();
for (Title title : titleList) {
if (title.getParentId() == 0) {
titleParentList.add(title);
}
}
//这是创建一整个导航栏对象(包含父类和子类一起)
List<TitleTestVo> titleTestVoList = new ArrayList<>();
//将父类的数据(ID,Name)赋予给TitleTestVo
for (Title parentTitle : titleParentList) {
//将数据库的内容赋予给 TitleTestVo(整个导航栏对象)
TitleTestVo titleTestVo = new TitleTestVo();
titleTestVo.setId(parentTitle.getId());
titleTestVo.setName(parentTitle.getName());
//创建一个子级导航栏集合,把筛选好的子类集合添加到这个集合里面
List<BoyVo> boyVoList = new ArrayList<>();
for (Title boyTitle : titleList) {
if (parentTitle.getId() .equals(boyTitle.getParentId())) {
BoyVo boyVo = new BoyVo();
boyVo.setId(boyTitle.getId());
boyVo.setName(boyTitle.getName());
boyVoList.add(boyVo);
}
}
//将子类的数据(ID和Name)赋予给父类的 boyVoList
titleTestVo.setBoyVoList(boyVoList);
//将父类对象的数据赋予的父类
titleTestVoList.add(titleTestVo);
}
System.out.println("=============================="+titleTestVoList);
return titleTestVoList;
}else {
return null;
}
}
}
postman:
评论