spring-boot-starter-data-mongodb操纵MongoDB快速上手
spring-boot
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
项目地址:https://gitcode.com/gh_mirrors/sp/spring-boot
免费下载资源
·
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
yml配置文件
2.实体类
注意:实体类名称要和mongo数据库中集合名称对应,不然查询时查不出来,添加时,会将数据添加入以实体类为名称的集合内。
@Data
@AllArgsConstructor
@NoArgsConstructor 这三个为lombok Lombok快速上手
package com.diploma.comments.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document //表示映射为mongoDB数据库内一个集合
public class Comment implements Serializable {
@Id
private String _id;
private String uid;
private String content;
private String visits;//访问数量
private String comment;
private Date settime;
private String state;
}
3.dao层
package com.diploma.comments.dao;
import com.diploma.comments.entity.Comment;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CommentsDao extends MongoRepository<Comment,String> {
}
4.service层
package com.diploma.comments.service;
import com.diploma.comments.dao.CommentsDao;
import com.diploma.comments.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.IdWorker;
import java.util.List;
@Service
@Transactional
public class CommentsService {
@Autowired
private CommentsDao commentsDao;
@Autowired
private IdWorker idWorker;
public List<Comment> findAll(){
return commentsDao.findAll();
}
public Comment findById(String id){
return commentsDao.findById(id).get();
}
public void save(Comment comment){
String id = idWorker.nextId()+"";
comment.set_id(id);
commentsDao.save(comment);
}
public void update(Comment comment){
commentsDao.save(comment);
}
public void deleteById(String id){
commentsDao.deleteById(id);
}
}
5.controller层
package com.diploma.comments.controller;
import com.diploma.comments.entity.Comment;
import com.diploma.comments.service.CommentsService;
import entity.Result;
import entity.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/comments")
public class CommentsController {
@Autowired
private CommentsService commentsService;
@RequestMapping(method = RequestMethod.GET)
public Result findAll(){
System.out.println("-----------------------------");
List<Comment> list = commentsService.findAll();
for (Comment comment : list) {
System.out.println(comment);
}
System.out.println("------------------------------"+list.size());
return new Result(ResultCode.SUCCESS,list);
}
@RequestMapping(value = "/{comentId}",method = RequestMethod.GET)
public Result findById(@PathVariable String commentId){
return new Result(ResultCode.SUCCESS,commentsService.findById(commentId));
}
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody Comment comment){
return new Result(ResultCode.SUCCESS);
}
@RequestMapping(value = "/{comentId}",method = RequestMethod.PUT)
public Result update(@PathVariable String comentId,@RequestBody Comment comment){
comment.set_id(comentId);
commentsService.update(comment);
return new Result(ResultCode.SUCCESS);
}
@RequestMapping(value = "/{comentId}",method = RequestMethod.DELETE)
public Result delete(@PathVariable String comentId){
commentsService.deleteById(comentId);
return new Result(ResultCode.SUCCESS);
}
}
6.postman测试
over…
GitHub 加速计划 / sp / spring-boot
13
5
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:4 个月前 )
b444b8d3
* pr/43176:
Polish "Tighten rules around profile naming"
Tighten rules around profile naming
Closes gh-43176
7 小时前
53223529
See gh-43176
7 小时前
更多推荐
已为社区贡献3条内容
所有评论(0)