黑马十次方项目day03-16之springdata-mongodb增删改查
发布日期:2021-06-30 13:14:43
浏览次数:2
分类:技术文章
本文共 6027 字,大约阅读时间需要 20 分钟。
文章目录
实体类
在com.tensquare.spit.pojo包下创建如下的实体类
package com.tensquare.spit.pojo;import org.springframework.data.annotation.Id;import java.io.Serializable;import java.util.Date;/** * 类名称:Spit * 类描述:吐槽模块的实体类 * * @author: taohongchao * 创建时间:2019/1/19 15:24 * Version 1.0 */public class Spit implements Serializable { /** * MongoDB的id,必须用_id来使用 */ @Id private String _id; private String content; private Date publishtime; private String userid; private String nickname; private Integer visits; private Integer thumbup; private Integer share; private Integer comment; private String state; private String parentid; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPublishtime() { return publishtime; } public void setPublishtime(Date publishtime) { this.publishtime = publishtime; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Integer getVisits() { return visits; } public void setVisits(Integer visits) { this.visits = visits; } public Integer getThumbup() { return thumbup; } public void setThumbup(Integer thumbup) { this.thumbup = thumbup; } public Integer getShare() { return share; } public void setShare(Integer share) { this.share = share; } public Integer getComment() { return comment; } public void setComment(Integer comment) { this.comment = comment; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getParentid() { return parentid; } public void setParentid(String parentid) { this.parentid = parentid; }}
dao
在com.tensquare.spit.dao 包下,创建如下的dao . 继承MongoRepository
package com.tensquare.spit.dao;import com.tensquare.spit.pojo.Spit;import org.springframework.data.mongodb.repository.MongoRepository;/** * 类名称:SpitDao * 类描述:吐槽微服务的dao * * @author: taohongchao * 创建时间:2019/1/19 15:30 * Version 1.0 */public interface SpitDao extends MongoRepository{ }
service
在com.tensquare.spit.service包下进行service层的编写
package com.tensquare.spit.service;import com.tensquare.spit.dao.SpitDao;import com.tensquare.spit.pojo.Spit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import util.IdWorker;import java.util.List;/** * 类名称:SpitService * 类描述: 吐槽微服务的service层 * * @author: taohongchao * 创建时间:2019/1/19 15:34 * Version 1.0 */@Service@Transactional(rollbackFor = Exception.class)public class SpitService { @Autowired private SpitDao spitDao; @Autowired private IdWorker idWorker; public ListfindAll(){ return spitDao.findAll(); } public Spit findById(String id) { return spitDao.findById(id).get(); } /** * 方法名: saveSpit * 方法描述: 新增吐槽,并用分布式id生成器 * 修改日期: 2019/1/19 15:42 * @param spit * @return void * @author taohongchao * @throws */ public void saveSpit(Spit spit) { spit.set_id(idWorker.nextId()+""); spitDao.save(spit); } public void update(Spit spit) { spitDao.save(spit); } public void deleteSpitById(String id) { spitDao.deleteById(id); }}
Controller层
在com.tensquare.spit.controller包下进行controller层的编写
需要注意的是如果接收的是json对象数据,后台用pojo接收的话, 必须得加上@RequestBody
注解,否则封装不上数据 package com.tensquare.spit.controller;import com.tensquare.spit.pojo.Spit;import com.tensquare.spit.service.SpitService;import entity.Result;import entity.StatusCode;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;/** * 类名称:SpitController * 类描述:吐槽模块的controller层 * * @author: taohongchao * 创建时间:2019/1/19 15:49 * Version 1.0 */@RestController@CrossOrigin@RequestMapping("/spit")public class SpitController { @Autowired private SpitService spitService; /** * 方法名: findAll * 方法描述: 查询所有数据 * 修改日期: 2019/1/19 15:56 * @param * @return entity.Result * @author taohongchao * @throws */ @RequestMapping(method = RequestMethod.GET) public Result findAll(){ return new Result(true, StatusCode.OK, "查询成功", spitService.findAll()); } /** * 方法名: findById * 方法描述: 通过id查询吐槽信息 * 修改日期: 2019/1/19 16:02 * @param spitId * @return entity.Result * @author taohongchao * @throws */ @RequestMapping(value = "/{spitId}",method = RequestMethod.GET) public Result findById(@PathVariable String spitId){ return new Result(true, StatusCode.OK, "查询成功", spitService.findById(spitId)); } @RequestMapping(method = RequestMethod.POST) public Result save(@RequestBody Spit spit) { spitService.saveSpit(spit); return new Result(true, StatusCode.OK, "保存成功"); } @RequestMapping(value = "/{spitId}",method = RequestMethod.PUT) public Result update(@RequestBody Spit spit,@PathVariable String spitId) { spit.set_id(spitId); spitService.update(spit); return new Result(true, StatusCode.OK, "修改成功"); } @RequestMapping(value = "/{spitId}",method = RequestMethod.DELETE) public Result delete(@PathVariable String spitId) { spitService.deleteSpitById(spitId); return new Result(true, StatusCode.OK, "删除成功"); }}
测试
在十次方的api中,可以对上面写的接口,进行增删改查测试操作.
转载地址:https://javaweixin6.blog.csdn.net/article/details/86554189 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月27日 07时28分18秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Glib学习(22) 主事件循环 The Main Event Loop
2019-05-01
Glib学习(23) IO通道 IO Channels
2019-05-01
Valgrind学习总结
2019-05-01
Glib学习(24) Base64编解码 Base64 Encoding
2019-05-01
Glib学习(25) 数据校验和 Data Checksums
2019-05-01
(13)香橙派+apache2与php+天猫精灵=自建平台语音支持--duerOS对接
2019-05-01
(1)CMake入门笔记--CMake官网教程
2019-05-01
(2)CMake入门笔记--CMake官网教程
2019-05-01
(3)CMake入门笔记--CMake官网教程
2019-05-01
(4)CMake入门笔记--CMake官网教程
2019-05-01
(XWZ)的python学习笔记Ⅰ
2019-05-01
(XWZ)的Python学习笔记Ⅱ------面向对象编程
2019-05-01
(XWZ)的Python学习笔记Ⅲ——面向对象高级编程
2019-05-01
(XWZ)的python学习笔记Ⅳ——错误、调试和测试
2019-05-01
(XWZ)的Python学习笔记Ⅴ——I/O编程
2019-05-01
(XWZ)的python学习笔记Ⅵ——进程和线程
2019-05-01
(XWZ)的python学习笔记Ⅶ——正则表达式
2019-05-01
(XWZ)的Python学习笔记Ⅷ--------numpy
2019-05-01
(XWZ)的python学习笔记——pandas
2019-05-01
基于Frobenius范数的标准NMF更新公式推导
2019-05-01