转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/86087596 谢谢
mongodb搭建 https://blog.csdn.net/Amor_Leo/article/details/85858145
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring:
data:
mongodb:
uri: mongodb://root:[email protected]:27017/
database: mongo
authentication-database: admin
@Data
public class SpitEntity implements Serializable {
/**
* ID主键
* */
@Id
private String _id;
/**
* 吐槽内容
* */
private String content;
/**
* 发布日期
* */
private Date publishtime;
/**
* 发布人ID
* */
private String userid;
/**
* 发布人昵称
* */
private String nickname;
/**
* 浏览量
* */
private Integer visits;
/**
* 点赞数
* */
private Integer thumbup;
/**
* 分享数
* */
private Integer share;
/**
* 回复数
* */
private Integer comment;
/**
* 是否可见 0 不可见,1 可见
* */
private String state;
/**
* 上级ID 如果为0表示文章的顶级评论
* */
private String parentid;
}
@Repository
public interface SpitRepository extends MongoRepository<SpitEntity, String> {
public Page<SpitEntity> findByParentid(String parentid, Pageable pageable);
}
/**
* @author: LHL
* @ProjectName: mongodemo
* @Package: com.aqh.mongo.service.impl
* @ClassName: SpitServiceImpl
* @Description:
* @Version: 1.0
*/
@Service
public class SpitServiceImpl implements SpitService {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private SpitRepository spitDao;
@Override
public List<SpitEntity> findAll(){
return spitDao.findAll();
}
@Override
public SpitEntity findById(String id){
return spitDao.findById(id).get();
}
@Override
public SpitEntity findByName(String name) {
Query query=new Query(Criteria.where("nickname").is(name));
SpitEntity mgt = mongoTemplate.findOne(query , SpitEntity.class);
return mgt;
}
@Override
public void save(SpitEntity spit){
spit.set_id(UUID.randomUUID().toString());
spit.setPublishtime(new Date());
spit.setVisits(0);
spit.setShare(0);
spit.setThumbup(0);
spit.setComment(0);
spit.setState("1");
if(spit.getParentid()!=null && !"".equals(spit.getParentid())){
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
Update update = new Update();
update.inc("comment", 1);
mongoTemplate.updateFirst(query, update, "spitEntity");
}
spitDao.save(spit);
}
@Override
public void update(SpitEntity spit){
spitDao.save(spit);
}
@Override
public void deleteById(String id){
Query query=new Query(Criteria.where("_id").is(id));
mongoTemplate.remove(query,"spitEntity");
// spitDao.deleteById(id);
}
@Override
public Page<SpitEntity> pageQuery(String parentid, int page, int size){
Pageable pageable = PageRequest.of(page-1, size);
return spitDao.findByParentid(parentid, pageable);
}
@Override
public void addShare(String spitId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(spitId));
Update update = new Update();
update.inc("share", 1);
mongoTemplate.updateFirst(query, update, "spitEntity");
}
@Override
public void addthumbup(String id){
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(id));
Update update = new Update();
update.inc("thumbup", 1);
mongoTemplate.updateFirst(query, update, "spitEntity");
}
}
/**
* @author: LHL
* @ProjectName: mongodemo
* @Package: com.aqh.mongo.controller
* @ClassName: SpitController
* @Description:
* @Version: 1.0
*/
@RestController
@RequestMapping(value = "/spit")
public class SpitController {
@Autowired
private SpitService spitService;
@RequestMapping(method = RequestMethod.GET)
public Result findAll(){
return new Result(true, StatusCode.OK, "查询成功", spitService.findAll());
}
@RequestMapping(value = "/{spitId}", method = RequestMethod.GET)
public Result findById(@PathVariable String spitId){
Assert.notNull(spitId, "spitId is not null");
return new Result(true, StatusCode.OK, "查询成功", spitService.findById(spitId));
}
@RequestMapping(value = "/findname", method = RequestMethod.GET)
public Result findByName(@PathVariable String nickname){
Assert.notNull(nickname, "nickname is not null");
return new Result(true, StatusCode.OK, "查询成功", spitService.findByName(nickname));
}
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody SpitEntity spit){
Assert.notNull(spit, "spit is not null");
spitService.save(spit);
return new Result(true, StatusCode.OK, "添加成功");
}
@RequestMapping(value = "/{spitId}", method = RequestMethod.PUT)
public Result update(@PathVariable String spitId, @RequestBody SpitEntity spit){
Assert.notNull(spitId, "spitId is not null");
Assert.notNull(spit, "spit is not null");
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){
Assert.notNull(spitId, "spitId is not null");
spitService.deleteById(spitId);
return new Result(true, StatusCode.OK, "删除成功");
}
@RequestMapping(value = "/comment/{parentid}/{page}/{size}", method = RequestMethod.GET)
public Result comment(@PathVariable String parentid, @PathVariable int page, @PathVariable int size){
Page<SpitEntity> pageData = spitService.pageQuery(parentid, page, size);
Assert.notNull(parentid, "parentid is not null");
Assert.notNull(page, "page is not null");
Assert.notNull(size, "size is not null");
return new Result(true, StatusCode.OK, "查询成功", new PageResult<SpitEntity>(pageData.getTotalElements(), pageData.getContent()));
}
@RequestMapping(value = "/thumbup/{spitId}", method = RequestMethod.PUT)
public Result addthumbup(@PathVariable String spitId){
Assert.notNull(spitId, "spitId is not null");
spitService.addthumbup(spitId);
return new Result(true, StatusCode.OK, "点赞成功");
}
@PutMapping("/share/{spitId}")
public Result addShare(@PathVariable String spitId) {
Assert.notNull(spitId, "spitId is not null");
spitService.addShare(spitId);
return new Result(true, StatusCode.OK, "分享成功!");
}
}
GitHub