概述
一般来说整合某个资源,需要引入其jar包,配置application.yml,引入其模板构建
引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置application.yml
server:
port: 9006
spring:
application:
name: tensquare-spit
data:
mongodb:
host: 192.168.126.128
database: spitdb
redis:
host: 192.168.126.128
service
@Service
@Transactional
public class SpitService {
@Autowired
private SpitDao spitDao;
@Autowired
private IdWorker idWorker;
@Autowired
private MongoTemplate mongoTemplate;
public void thumbup(String spitId) {
/*Spit spit = spitDao.findById(spitId).get();
spit.setThumbup((spit.getThumbup()==null? 0 : spit.getThumbup())+1);
spitDao.save(spit);*/
//使用mongb来实现自增 db.spit.update({"_id":"1"},{$inc:{thumbup:NumberInt(1)});
Query query = new Query();
query.addCriteria(Criteria.where("_id").is("2"));
Update update = new Update();
update.inc("thumbup",1);
mongoTemplate.updateFirst(query,update,"spit");
}
dao接口
public interface SpitDao extends MongoRepository<Spit,String> {
public Page<Spit> findByParentid(String parentid, Pageable pageable);
}