七(3)分页查询-MongoDB

这篇博客详细介绍了MongoDB在探花交友APP中用于通用设置、陌生人问题、通知设置和黑名单管理的分页查询操作。内容涵盖需求分析、数据库表设计、代码实现和接口文档,展示了MongoDB在处理大量数据时的高效性和灵活性。此外,还介绍了MongoDB的基本特点、与Redis和MySQL的对比,以及MongoDB在社交软件中的适用场景。文中还提及了MongoDB的安装和数据类型,以及如何通过SpringData-Mongo简化MongoDB操作。
摘要由CSDN通过智能技术生成

一. 通用设置

1.1 需求分析

1. 需求分析

通用设置,包含探花交友APP基本的软件设置功能。包含:

设置陌生人问题:当平台其他用户想进行在线交流时需要回答陌生人问题。

通用设置:包含一些APP通知设置

黑名单:对于不感兴趣的用户设置黑名单屏蔽骚扰

image-20201031224532138 image-20201031224515065
2. 数据库表

通用设置

CREATE TABLE `tb_settings` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT NULL,
  `like_notification` tinyint(4) DEFAULT '1' COMMENT '推送喜欢通知',
  `pinglun_notification` tinyint(4) DEFAULT '1' COMMENT '推送评论通知',
  `gonggao_notification` tinyint(4) DEFAULT '1' COMMENT '推送公告通知',
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设置表';

问题表

CREATE TABLE `tb_question` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
  `txt` varchar(200) DEFAULT NULL COMMENT '问题内容',
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

黑名单

CREATE TABLE `tb_black_list` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT NULL,
  `black_user_id` bigint(20) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='黑名单';
3. 搭建提供者环境
实体类

(1) Settings

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Settings extends BasePojo {
   

    private Long id;
    private Long userId;
    private Boolean likeNotification;
    private Boolean pinglunNotification;
    private Boolean gonggaoNotification;

}

(2)Question

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Question extends BasePojo {
   

    private Long id;
    private Long userId;
    //问题内容
    private String txt;

}

(3)BlackList

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BlackList extends BasePojo {
   

    private Long id;
    private Long userId;
    private Long blackUserId;
}
mapper接口

(1)SettingsMapper

public interface SettingsMapper extends BaseMapper<Settings> {
   
}

(2)QuestionMapper

public interface QuestionMapper extends BaseMapper<Question> {
   

}

(3)BlackListMapper

public interface BlackListMapper extends BaseMapper<BlackList> {
   
    
}
api接口

(1) SettingApi

public interface SettingsApi {
   

}

(2)QuestionApi

import com.tanhua.domain.db.Question;


public interface QuestionApi {
   

}

(3)BlackListApi

public interface BlackListApi {
   

}

api服务实现类

(1)SettingServiceImpl

@Service
public class SettingsApiImpl implements SettingsApi {
   

    @Autowired
    private SettingsMapper settingsMapper;

}

(2)QuestionServiceImpl

@Service
public class QuestionApiImpl implements QuestionApi {
   

    @Autowired
    private QuestionMapper questionMapper;

}

(3)BlackListServiceImpl

@Service
public class BlackListApiImpl implements BlackListApi {
   

    @Autowired
    private BlackListMapper blackListMapper;
}

1.2 查询通用设置

image-20220924094746731

代码流程

image-20220924100057865

1. 接口文档

image-20201031180903518

image-20201031180913892

2. 代码实现

步骤

image-20220924092709121
vo对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SettingsVo implements Serializable {
   

    private Long id;
    private String strangerQuestion = "";
    private String phone;
    private Boolean likeNotification = true;
    private Boolean pinglunNotification = true;
    private Boolean gonggaoNotification = true;

}
SettingsController

tanhua-server工程创建SettingsController完成代码编写

@RestController
@RequestMapping("/users")
public class SettingsController {
   

    @Autowired
    private SettingsService settingsService;

    /**
     * 查询通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
   
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }
}
SettingService

tanhua-server工程创建SettingService完成代码编写

@Service
public class SettingsService {
   

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    //查询通用设置
    public SettingsVo settings() {
   
        SettingsVo vo = new SettingsVo();
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2、获取用户的手机号码
        vo.setPhone(UserHolder.getMobile());
        //3、获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗?" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //4、获取用户的APP通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if(settings != null) {
   
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }
}
QuestionApi

在tanhua-dubbo中的QuestionApiQuestionApiImpl补充方法

@Override
public Question findByUserId(Long userId) {
   
    QueryWrapper<Question> qw = new QueryWrapper<>();
    qw.eq("user_id",userId);
    return questionMapper.selectOne(qw);
}
SettingApi

在tanhua-dubbo中的SettingApiSettingApiImpl补充方法

//根据用户id查询
public Settings findByUserId
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值