发表于: 2020-08-15 23:57:58
1 1270
今天完成的事情:
写后台消息管理的接口。
我们项目使用了SpringBoot和SpringCloud:
之前只是写了Controller层提供了假数据
今天写了后台总服务里的消息管理:
Mapper接口:
package com.jnshu.dao;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnshu.pojo.News;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface NewsMapper {
boolean delete(Integer id);
int insert(News news);
News selectById(Integer id);
List<News> selectAll(Page<News> page);
List<News> selectByCond(Page<News> page,@Param("news") News news);
boolean update(News news);
}
Service接口:
package com.jnshu.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jnshu.pojo.News;
public interface NewsService {
boolean delete(Integer id);
int insert(News news);
News selectById(Integer id);
IPage<News> selectAll(int pagenow, int pagesize);
IPage<News> selectByCond(News news, int pagenow, int pagesize);
boolean update(News news);
}
ServiceImpl实现类:
package com.jnshu.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnshu.dao.NewsMapper;
import com.jnshu.pojo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsMapper newsMapper;
//删除消息
public boolean delete(Integer id) {
return newsMapper.delete(id);
}
//添加消息
public int insert(News news) {
newsMapper.insert(news);
return news.getId();
}
//查询单条消息
public News selectById(Integer id) {
return newsMapper.selectById(id);
}
//查询所有消息
public IPage<News> selectAll(int pagenow,int pagesize) {
Page<News> page = new Page<>(pagenow,pagesize);
page.setRecords(newsMapper.selectAll(page));
return page;
}
//根据条件查询消息
public IPage<News> selectByCond(News news, int pagenow, int pagesize) {
Page<News> page = new Page<>(pagenow,pagesize);
page.setRecords(newsMapper.selectByCond(page,news));
return page;
}
//编辑消息
public boolean update(News news) {
return newsMapper.update(news);
}
}
Controller:
package com.jnshu.Controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jnshu.Service.NewsService;
import com.jnshu.pojo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/a/news")
public class NewsController {
@Autowired
private NewsService newsService;
//查询所有消息
@GetMapping("/list")
public Map findAll(int pagenow,int pagesize){
Map<String,Object> map = new HashMap<>();
try{
IPage<News> page = newsService.selectAll(pagenow,pagesize);
Long total = page.getTotal();
map.put("code",200);
map.put("message","查询成功");
map.put("total",total);
map.put("data",page);
}catch (Exception e){
map.put("code",400);
map.put("message","查询失败");
}
return map;
}
//查询单条消息
@GetMapping("/{nid}")
public Map findById(@PathVariable int nid){
Map<String,Object> map = new HashMap<>();
try{
News news = newsService.selectById(nid);
map.put("code",200);
map.put("message","查询成功");
map.put("data",news);
}catch (Exception e){
map.put("code",400);
map.put("message","查询失败");
}
return map;
}
//根据条件查询消息
@GetMapping
public Map findByCond(News news,int pagenow,int pagesize){
Map<String,Object> map = new HashMap<>();
try{
IPage<News> page = newsService.selectByCond(news,pagenow,pagesize);
Long total = page.getTotal();
map.put("code",200);
map.put("message","查询成功");
map.put("total",total);
map.put("data",page);
}catch (Exception e){
map.put("code",400);
map.put("message","查询失败");
}
return map;
}
//新增消息
@PostMapping
public Map insert(News news){
Map<String,Object> map = new HashMap<>();
try{
newsService.insert(news);
map.put("code",200);
map.put("message","新增成功");
}catch (Exception e){
map.put("code",400);
map.put("message","新增失败");
}
return map;
}
//删除消息
@DeleteMapping("/{nid}")
public Map delete(@PathVariable int nid){
Map<String,Object> map = new HashMap<>();
try{
newsService.delete(nid);
map.put("code",200);
map.put("message","删除成功");
}catch (Exception e){
map.put("code",400);
map.put("message","删除失败");
}
return map;
}
//修改消息推送方式
@PutMapping("/{nid}")
public Map update(@PathVariable int nid){
Map<String,Object> map = new HashMap<>();
try{
News news = new News();
news.setId(nid);
news.setType(1);
newsService.update(news);
map.put("code",200);
map.put("message","修改成功");
}catch (Exception e){
map.put("code",400);
map.put("message","修改失败");
}
return map;
}
}
代码初步跑通,明天再优化细节。比如新增消息接口等。
明天计划的事情:
优化消息管理接口内部逻辑。
评论