赞
踩
通用设置,包含探花交友APP基本的软件设置功能。包含:
设置陌生人问题:当平台其他用户想进行在线交流时需要回答陌生人问题。
通用设置:包含一些APP通知设置
黑名单:对于不感兴趣的用户设置黑名单屏蔽骚扰


通用设置
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='黑名单';
(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;
}
(1)SettingsMapper
public interface SettingsMapper extends BaseMapper<Settings> {
}
(2)QuestionMapper
public interface QuestionMapper extends BaseMapper<Question> {
}
(3)BlackListMapper
public interface BlackListMapper extends BaseMapper<BlackList> {
}
(1) SettingApi
package com.tanhua.dubbo.api;
import com.tanhua.domain.db.Settings;
public interface SettingsApi {
}
(2)QuestionApi
package com.tanhua.dubbo.api;
import com.tanhua.domain.db.Question;
public interface QuestionApi {
}
(3)BlackListApi
package com.tanhua.dubbo.api;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.UserInfo;
public interface BlackListApi {
}
(1)SettingServiceImpl
package com.tanhua.dubbo.api; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.tanhua.domain.db.Settings; import com.tanhua.dubbo.mapper.SettingsMapper; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; @Service public class SettingsApiImpl implements SettingsApi { @Autowired private SettingsMapper settingsMapper; }
(2)QuestionServiceImpl
package com.tanhua.dubbo.api; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.tanhua.domain.db.Question; import com.tanhua.dubbo.mapper.QuestionMapper; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; @Service public class QuestionApiImpl implements QuestionApi { @Autowired private QuestionMapper questionMapper; }
(3)BlackListServiceImpl
package com.tanhua.dubbo.api; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.tanhua.domain.db.BlackList; import com.tanhua.domain.db.UserInfo; import com.tanhua.dubbo.mapper.BlackListMapper; import com.tanhua.dubbo.mapper.UserInfoMapper; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; @Service public class BlackListApiImpl implements BlackListApi { @Autowired private BlackListMapper blackListMapper; }


@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;
}
在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); } }
在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; } }
在tanhua-dubbo中的QuestionApi和QuestionApiImpl补充方法
@Override
public Question findByUserId(Long userId) {
QueryWrapper<Question> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
return questionMapper.selectOne(qw);
}
在tanhua-dubbo中的SettingApi和SettingApiImpl补充方法
//根据用户id查询
public Settings findByUserId(Long userId) {
QueryWrapper<Settings> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
return settingsMapper.selectOne(qw);
}
对数据库表进行操作:如果存在数据,更新数据库。如果不存在数据,保存数据库表数据

/**
* 设置陌生人问题
*/
@PostMapping("/questions")
public ResponseEntity questions(@RequestBody Map map) {
//获取参数
String content = (String) map.get("content");
settingsService.saveQuestion(content);
return ResponseEntity.ok(null);
}
//设置陌生人问题 public void saveQuestion(String content) { //1、获取当前用户id Long userId = UserHolder.getUserId(); //2、调用api查询当前用户的陌生人问题 Question question = questionApi.findByUserId(userId); //3、判断问题是否存在 if(question == null) { //3.1 如果不存在,保存 question = new Question(); question.setUserId(userId); question.setTxt(content); questionApi.save(question); }else { //3.2 如果存在,更新 question.setTxt(content); questionApi.update(question); } }
tanhua-dubbo工程中的QuestionApi和QuestionApiImpl中添加保存和更新方法
@Override
public void save(Question question) {
questionMapper.insert(question);
}
@Override
public void update(Question question) {
questionMapper.updateById(question);
}
通知管理:对通知进行保存或者更新的操作
http://192.168.136.160:3000/project/19/interface/api/280

/**
* 通知设置
*/
@PostMapping("/notifications/setting")
public ResponseEntity notifications(@RequestBody Map map) {
//获取参数
settingsService.saveSettings(map);
return ResponseEntity.ok(null);
}
//通知设置 public void saveSettings(Map map) { boolean likeNotification = (Boolean) map.get("likeNotification"); boolean pinglunNotification = (Boolean) map.get("pinglunNotification"); boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification"); //1、获取当前用户id Long userId = UserHolder.getUserId(); //2、根据用户id,查询用户的通知设置 Settings settings = settingsApi.findByUserId(userId); //3、判断 if(settings == null) { //保存 settings = new Settings(); settings.setUserId(userId); settings.setPinglunNotification(pinglunNotification); settings.setLikeNotification(likeNotification); settings.setGonggaoNotification(gonggaoNotification); settingsApi.save(settings); }else { settings.setPinglunNotification(pinglunNotification); settings.setLikeNotification(likeNotification); settings.setGonggaoNotification(gonggaoNotification); settingsApi.update(settings); } }
tanhua-dubbo工程中的SettingsApi和SettingsApiImpl中添加保存和更新方法
@Override
public void save(Settings settings) {
settingsMapper.insert(settings);
}
@Override
public void update(Settings settings) {
settingsMapper.updateById(settings);
}



tanhua-domain工程的配置分页vo对象
package com.tanhua.domain.vo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.util.Collections; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor public class PageResult implements Serializable { private Integer counts = 0;//总记录数 private Integer pagesize;//页大小 private Integer pages = 0;//总页数 private Integer page;//当前页码 private List<?> items = Collections.emptyList(); //列表 public PageResult(Integer page,Integer pagesize, int counts,List list) { this.page = page; this.pagesize = pagesize; this.items = list; this.counts = counts; this.pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1; } }
/** * 分页查询黑名单列表 */ @GetMapping("/blacklist") public ResponseEntity blacklist( @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) { //1、调用service查询 PageResult pr = settingsService.blacklist(page,size); //2、构造返回 return ResponseEntity.ok(pr); } /** * 取消黑名单 */ @DeleteMapping("/blacklist/{uid}") public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) { settingsService.deleteBlackList(blackUserId); return ResponseEntity.ok(null); }
//分页查询黑名单列表 public PageResult blacklist(int page, int size) { //1、获取当前用户的id Long userId = UserHolder.getUserId(); //2、调用API查询用户的黑名单分页列表 Ipage对象 IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size); //3、对象转化,将查询的Ipage对象的内容封装到PageResult中 PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords()); //4、返回 return pr; } //取消黑名单 public void deleteBlackList(Long blackUserId) { //1、获取当前用户id Long userId = UserHolder.getUserId(); //2、调用api删除 blackListApi.delete(userId,blackUserId); }
@Override
public IPage<UserInfo> findByUserId(Long userId, int page, int size) {
//1、构建分页参数对象Page
Page pages = new Page(page,size);
//2、调用方法分页(自定义编写 分页参数Page,sql条件参数)
return userInfoMapper.findBlackList(pages,userId);
}
@Override
public void delete(Long userId, Long blackUserId) {
QueryWrapper<BlackList> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
qw.eq("black_user_id",blackUserId);
blackListMapper.delete(qw);
}
public interface UserInfoMapper extends BaseMapper<UserInfo> {
@Select("select * from tb_user_info where id in (\n" +
" SELECT black_user_id FROM tb_black_list where user_id=#{userId}\n" +
")")
IPage<UserInfo> findBlackList(@Param("pages") Page pages, @Param("userId") Long userId);
}
tanhua-dubbo-db引导类开启mybatis-plus分页插件支持
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
使用mybatis-plus的分页:
对于社交类软件的功能,我们需要对它的功能特点做分析:
针对以上特点,我们来分析一下:
探花交友
MongoDB:是一个高效的非关系型数据库(不支持表关系:只能操作单表)

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的,它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。
MongoDB最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
官网:https://www.mongodb.com
MongoDB 最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。它是一个面向集合的,模式自由的文档型数据库。具体特点总结如下:
在课程资料的虚拟机中已经提供了MongoDB的镜像和容器,我们只需要使用简单的命令即可启动
#进入base目录
cd /root/docker-file/base/
#批量创建启动容器,其中已经包含了redis,zookeeper,mongodb容器
docker-compose up -d
#查看容器
docker ps -a

可以看到mongoDB已经启动,对外暴露了27017的操作端口
MongoDB 的逻辑结构是一种层次结构。主要由: 文档(document)、集合(collection)、数据库(database)这三部分组成的。逻辑结构是面 向用户的,用户使用 MongoDB 开发应用程序使用的就是逻辑结构。

为了更好的理解,下面与SQL中的概念进行对比:
| SQL术语/概念 | MongoDB术语/概念 | 解释/说明 |
|---|---|---|
| database | database | 数据库 |
| table | collection | 数据库表/集合 |
| row | document | 表中的一条数据 |
| column | field | 数据字段/域 |
| index | index | 索引 |
| table joins | 表连接,MongoDB不支持 | |
| primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。