4、圈子实现
3.1、环境搭建
Mongodb中实现字段的自增:两种解决方法(1、使用redis保证自动增长,2、使用mongodb自定义表)
3.1.1、mongo主键自增
第一步:创建实体类
1. package com.tanhua.domain.mongo; 2. 3. import lombok.AllArgsConstructor; 4. import lombok.Data; 5. import lombok.NoArgsConstructor; 6. import org.bson.types.ObjectId; 7. import org.springframework.data.annotation.Id; 8. import org.springframework.data.mongodb.core.mapping.Document; 9. import org.springframework.data.mongodb.core.mapping.Field; 10. 11. @Document(collection = "sequence") 12. @Data 13. @AllArgsConstructor 14. @NoArgsConstructor 15. public class Sequence { 16. 17. private ObjectId id; 18. 19. private long seqId; //自增序列 20. 21. private String collName; //集合名称 22. }
第二步:编写service
1. package com.tanhua.dubbo.utils; 2. 3. import com.tanhua.domain.mongo.Sequence; 4. import org.springframework.beans.factory.annotation.Autowired; 5. import org.springframework.data.mongodb.core.FindAndModifyOptions; 6. import org.springframework.data.mongodb.core.MongoTemplate; 7. import org.springframework.data.mongodb.core.query.Criteria; 8. import org.springframework.data.mongodb.core.query.Query; 9. import org.springframework.data.mongodb.core.query.Update; 10. import org.springframework.stereotype.Component; 11. 12. @Component 13. public class IdWorker { 14. 15. @Autowired 16. private MongoTemplate mongoTemplate; 17. 18. public Long getNextId(String collName) { 19. Query query = new Query(Criteria.where("collName").is(collName)); 20. 21. Update update = new Update(); 22. update.inc("seqId", 1); 23. 24. FindAndModifyOptions options = new FindAndModifyOptions(); 25. options.upsert(true); 26. options.returnNew(true); 27. 28. Sequence sequence = mongoTemplate.findAndModify(query, update, options, Sequence.class); 29. return sequence.getSeqId(); 30. } 31. }
3.1.2、实体类
写到tanhua-domain工程中:
Movement
Movement:发布信息表(总记录表数据)
1. package com.tanhua.domain.mongo; 2. 3. import lombok.AllArgsConstructor; 4. import lombok.Data; 5. import lombok.NoArgsConstructor; 6. import org.bson.types.ObjectId; 7. import org.springframework.data.mongodb.core.mapping.Document; 8. 9. import java.util.List; 10. 11. //动态详情表 12. @Data 13. @NoArgsConstructor 14. @AllArgsConstructor 15. @Document(collection = "movement") 16. public class Movement implements java.io.Serializable { 17. 18. 19. private ObjectId id; //主键id 20. private Long pid; //Long类型,用于推荐系统的模型(自动增长) 21. private Long created; //发布时间 22. private Long userId; 23. private String textContent; //文字 24. private List<String> medias; //媒体数据,图片或小视频 url 25. private String longitude; //经度 26. private String latitude; //纬度 27. private String locationName; //位置名称 28. private Integer state = 0;//状态 0:未审(默认),1:通过,2:驳回 29. }
MovementTimeLine
MovementTimeLine:好友时间线表,用于存储好友发布(或推荐)的数据,每一个用户一张表进行存储
1. package com.tanhua.domain.mongo; 2. 3. import lombok.AllArgsConstructor; 4. import lombok.Data; 5. import lombok.NoArgsConstructor; 6. import org.bson.types.ObjectId; 7. import org.springframework.data.mongodb.core.mapping.Document; 8. 9. /** 10. * 好友时间线表,用于存储好友发布的数据 11. */ 12. @Data 13. @NoArgsConstructor 14. @AllArgsConstructor 15. @Document(collection = "movement_timeLine") 16. public class MovementTimeLine implements java.io.Serializable { 17. 18. private static final long serialVersionUID = 9096178416317502524L; 19. private ObjectId id; 20. private ObjectId movementId;//动态id 21. private Long userId; //发布动态用户id 22. private Long friendId; // 可见好友id 23. private Long created; //发布的时间 24. }
Friend
Friend 好友关系表
1. package com.tanhua.domain.mongo; 2. 3. import lombok.AllArgsConstructor; 4. import lombok.Data; 5. import lombok.NoArgsConstructor; 6. import org.bson.types.ObjectId; 7. import org.springframework.data.mongodb.core.mapping.Document; 8. 9. /** 10. * 好友表:好友关系表 11. */ 12. @Data 13. @NoArgsConstructor 14. @AllArgsConstructor 15. @Document(collection = "friend") 16. public class Friend implements java.io.Serializable{ 17. 18. private static final long serialVersionUID = 6003135946820874230L; 19. private ObjectId id; 20. private Long userId; //用户id 21. private Long friendId; //好友id 22. private Long created; //时间 23. 24. }
3.1.3、API接口
1. package com.tanhua.dubbo.api.mongo; 2. 3. import com.tanhua.domain.mongo.Publish; 4. import com.tanhua.domain.vo.PageResult; 5. 6. public interface MovementApi { 7. 8. }
3.1.4、API实现类
1. package com.tanhua.dubbo.api.mongo; 2. 3. import com.tanhua.domain.mongo.*; 4. import com.tanhua.domain.vo.PageResult; 5. import com.tanhua.dubbo.utils.IdService; 6. import org.apache.dubbo.config.annotation.Service; 7. import org.bson.types.ObjectId; 8. import org.springframework.beans.factory.annotation.Autowired; 9. import org.springframework.data.domain.PageRequest; 10. import org.springframework.data.domain.Pageable; 11. import org.springframework.data.domain.Sort; 12. import org.springframework.data.mongodb.core.MongoTemplate; 13. import org.springframework.data.mongodb.core.query.Criteria; 14. import org.springframework.data.mongodb.core.query.Query; 15. 16. import java.util.ArrayList; 17. import java.util.List; 18. 19. @Service 20. public class MovementApiImpl implements PublishApi { 21. 22. }
3.1.5、MovementsController
1. package com.tanhua.server.controller; 2. 3. import com.tanhua.domain.mongo.Publish; 4. import com.tanhua.server.service.MovementsService; 5. import org.springframework.beans.factory.annotation.Autowired; 6. import org.springframework.http.ResponseEntity; 7. import org.springframework.web.bind.annotation.*; 8. import org.springframework.web.multipart.MultipartFile; 9. 10. import java.io.IOException; 11. 12. @RestController 13. @RequestMapping("/movements") 14. public class MovementsController { 15. 16. @Autowired 17. private MovementsService movementsService; 18. 19. }
3.1.6、MovementsService
1. package com.tanhua.server.service; 2. 3. import com.tanhua.autoconfig.templates.OssTemplate; 4. import com.tanhua.domain.db.UserInfo; 5. import com.tanhua.domain.mongo.Publish; 6. import com.tanhua.domain.vo.Movements; 7. import com.tanhua.domain.vo.PageResult; 8. import com.tanhua.dubbo.api.UserInfoApi; 9. import com.tanhua.dubbo.api.mongo.PublishApi; 10. import com.tanhua.server.interceptor.UserHolder; 11. import io.jsonwebtoken.lang.Collections; 12. import org.apache.dubbo.config.annotation.Reference; 13. import org.springframework.beans.BeanUtils; 14. import org.springframework.beans.factory.annotation.Autowired; 15. import org.springframework.http.ResponseEntity; 16. import org.springframework.stereotype.Service; 17. import org.springframework.util.StringUtils; 18. import org.springframework.web.multipart.MultipartFile; 19. 20. import java.io.IOException; 21. import java.text.SimpleDateFormat; 22. import java.util.*; 23. 24. @Service 25. public class MovementsService { 26. 27. @Autowired 28. private OssTemplate ossTemplate; 29. 30. @DubboReference 31. private MovementApi movementApi; 32. 33. @DubboReference 34. private UserInfoApi userInfoApi; 35. 36. @Autowired 37. private RedisTemplate<String,String> redisTemplate; 38. }