课程介绍
- MongoDB环境搭建
- MongoDB基本CRUD操作
- 通过JavaApi操作MongoDB
- SpringBoot整合MongoDB
1、通用设置
1.1 需求分析
1.1.1 需求分析
通用设置,包含探花交友APP基本的软件设置功能。包含:
设置陌生人问题:当平台其他用户想进行在线交流时需要回答陌生人问题。
通用设置:包含一些APP通知设置
黑名单:对于不感兴趣的用户设置黑名单屏蔽骚扰
1.1.2 数据库表
通用设置
1. CREATE TABLE `tb_settings` ( 2. `id` bigint(20) NOT NULL AUTO_INCREMENT, 3. `user_id` bigint(20) DEFAULT NULL, 4. `like_notification` tinyint(4) DEFAULT '1' COMMENT '推送喜欢通知', 5. `pinglun_notification` tinyint(4) DEFAULT '1' COMMENT '推送评论通知', 6. `gonggao_notification` tinyint(4) DEFAULT '1' COMMENT '推送公告通知', 7. `created` datetime DEFAULT NULL, 8. `updated` datetime DEFAULT NULL, 9. PRIMARY KEY (`id`) 10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设置表';
问题表
1. CREATE TABLE `tb_question` ( 2. `id` bigint(20) NOT NULL AUTO_INCREMENT, 3. `user_id` bigint(20) DEFAULT NULL COMMENT '用户id', 4. `txt` varchar(200) DEFAULT NULL COMMENT '问题内容', 5. `created` datetime DEFAULT NULL, 6. `updated` datetime DEFAULT NULL, 7. PRIMARY KEY (`id`), 8. KEY `user_id` (`user_id`) 9. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
黑名单
1. CREATE TABLE `tb_black_list` ( 2. `id` bigint(20) NOT NULL AUTO_INCREMENT, 3. `user_id` bigint(20) DEFAULT NULL, 4. `black_user_id` bigint(20) DEFAULT NULL, 5. `created` datetime DEFAULT NULL, 6. `updated` datetime DEFAULT NULL, 7. PRIMARY KEY (`id`), 8. KEY `user_id` (`user_id`) 9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='黑名单';
1.1.3 搭建提供者环境
实体类
(1) Settings
1. CREATE TABLE `tb_black_list` ( 2. `id` bigint(20) NOT NULL AUTO_INCREMENT, 3. `user_id` bigint(20) DEFAULT NULL, 4. `black_user_id` bigint(20) DEFAULT NULL, 5. `created` datetime DEFAULT NULL, 6. `updated` datetime DEFAULT NULL, 7. PRIMARY KEY (`id`), 8. KEY `user_id` (`user_id`) 9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='黑名单';
(2)Question
1. @Data 2. @NoArgsConstructor 3. @AllArgsConstructor 4. public class Question extends BasePojo { 5. 6. private Long id; 7. private Long userId; 8. //问题内容 9. private String txt; 10. 11. }
(3)BlackList
1. @Data 2. @NoArgsConstructor 3. @AllArgsConstructor 4. public class BlackList extends BasePojo { 5. 6. private Long id; 7. private Long userId; 8. private Long blackUserId; 9. }
mapper接口
(1)SettingsMapper
1. public interface SettingsMapper extends BaseMapper<Settings> { 2. }
(2)QuestionMapper
1. public interface QuestionMapper extends BaseMapper<Question> { 2. 3. }
(3)BlackListMapper
1. public interface BlackListMapper extends BaseMapper<BlackList> { 2. 3. }
api接口
(1) SettingApi
1. package com.tanhua.dubbo.api; 2. 3. import com.tanhua.domain.db.Settings; 4. 5. public interface SettingsApi { 6. 7. }
(2)QuestionApi
1. package com.tanhua.dubbo.api; 2. 3. import com.tanhua.domain.db.Question; 4. 5. 6. public interface QuestionApi { 7. 8. }
(3)BlackListApi
1. package com.tanhua.dubbo.api; 2. 3. import com.baomidou.mybatisplus.core.metadata.IPage; 4. import com.tanhua.domain.db.UserInfo; 5. 6. public interface BlackListApi { 7. 8. }
api服务实现类
(1)SettingServiceImpl
1. package com.tanhua.dubbo.api; 2. 3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 4. import com.tanhua.domain.db.Settings; 5. import com.tanhua.dubbo.mapper.SettingsMapper; 6. import org.apache.dubbo.config.annotation.Service; 7. import org.springframework.beans.factory.annotation.Autowired; 8. 9. @DubboService 10. public class SettingsApiImpl implements SettingsApi { 11. 12. @Autowired 13. private SettingsMapper settingsMapper; 14. 15. }
(2)QuestionServiceImpl
1. package com.tanhua.dubbo.api; 2. 3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 4. import com.tanhua.domain.db.Question; 5. import com.tanhua.dubbo.mapper.QuestionMapper; 6. import org.apache.dubbo.config.annotation.Service; 7. import org.springframework.beans.factory.annotation.Autowired; 8. 9. @DubboService 10. public class QuestionApiImpl implements QuestionApi { 11. 12. @Autowired 13. private QuestionMapper questionMapper; 14. 15. }
(3)BlackListServiceImpl
1. package com.tanhua.dubbo.api; 2. 3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 4. import com.baomidou.mybatisplus.core.metadata.IPage; 5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 6. import com.tanhua.domain.db.BlackList; 7. import com.tanhua.domain.db.UserInfo; 8. import com.tanhua.dubbo.mapper.BlackListMapper; 9. import com.tanhua.dubbo.mapper.UserInfoMapper; 10. import org.apache.dubbo.config.annotation.Service; 11. import org.springframework.beans.factory.annotation.Autowired; 12. 13. @DubboService 14. public class BlackListApiImpl implements BlackListApi { 15. 16. @Autowired 17. private BlackListMapper blackListMapper; 18. }
1.2 查询通用设置
1.2.1 接口文档
1.2.2 代码实现
vo对象
1. @Data 2. @NoArgsConstructor 3. @AllArgsConstructor 4. public class SettingsVo implements Serializable { 5. 6. private Long id; 7. private String strangerQuestion = ""; 8. private String phone; 9. private Boolean likeNotification = true; 10. private Boolean pinglunNotification = true; 11. private Boolean gonggaoNotification = true; 12. 13. }
SettingsController
在tanhua-server
工程创建SettingsController
完成代码编写
1. @RestController 2. @RequestMapping("/users") 3. public class SettingsController { 4. 5. @Autowired 6. private SettingsService settingsService; 7. 8. /** 9. * 查询通用设置 10. */ 11. @GetMapping("/settings") 12. public ResponseEntity settings() { 13. SettingsVo vo = settingsService.settings(); 14. return ResponseEntity.ok(vo); 15. } 16. }
SettingService
在tanhua-server
工程创建SettingService
完成代码编写
1. @Service 2. public class SettingsService { 3. 4. @DubboReference 5. private QuestionApi questionApi; 6. 7. @DubboReference 8. private SettingsApi settingsApi; 9. 10. @DubboReference 11. private BlackListApi blackListApi; 12. 13. //查询通用设置 14. public SettingsVo settings() { 15. SettingsVo vo = new SettingsVo(); 16. //1、获取用户id 17. Long userId = UserHolder.getUserId(); 18. vo.setId(userId); 19. //2、获取用户的手机号码 20. vo.setPhone(UserHolder.getMobile()); 21. //3、获取用户的陌生人问题 22. Question question = questionApi.findByUserId(userId); 23. String txt = question == null ? "你喜欢java吗?" : question.getTxt(); 24. vo.setStrangerQuestion(txt); 25. //4、获取用户的APP通知开关数据 26. Settings settings = settingsApi.findByUserId(userId); 27. if(settings != null) { 28. vo.setGonggaoNotification(settings.getGonggaoNotification()); 29. vo.setPinglunNotification(settings.getPinglunNotification()); 30. vo.setLikeNotification(settings.getLikeNotification()); 31. } 32. return vo; 33. } 34. }
QuestionApi
在tanhua-dubbo中的QuestionApi
和QuestionApiImpl
补充方法
1. @Override 2. public Question findByUserId(Long userId) { 3. QueryWrapper<Question> qw = new QueryWrapper<>(); 4. qw.eq("user_id",userId); 5. return questionMapper.selectOne(qw); 6. }
SettingApi
在tanhua-dubbo中的SettingApi
和SettingApiImpl
补充方法
1. //根据用户id查询 2. public Settings findByUserId(Long userId) { 3. QueryWrapper<Settings> qw = new QueryWrapper<>(); 4. qw.eq("user_id",userId); 5. return settingsMapper.selectOne(qw); 6. }