目录
1.1.1 需求分析
通用设置,包含探花交友APP基本的软件设置功能。包含:
设置陌生人问题:当平台其他用户想进行在线交流时需要回答陌生人问题。
通用设置:包含一些APP通知设置
黑名单:对于不感兴趣的用户设置黑名单屏蔽骚扰
1.1.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='黑名单';
1.1.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
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 {
}
api服务实现类
(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;
}
1.2.1 接口文档
1.2.2 代码实现
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中的QuestionApi
和QuestionApiImpl
补充方法
@Override
public Question findByUserId(Long userId) {
QueryWrapper<Question> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
return questionMapper.selectOne(qw);
}
SettingApi
在tanhua-dubbo中的SettingApi
和SettingApiImpl
补充方法
//根据用户id查询
public Settings findByUserId(Long userId) {
QueryWrapper<Settings> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
return settingsMapper.selectOne(qw);
}
对数据库表进行操作:如果存在数据,更新数据库。如果不存在数据,保存数据库表数据
1.2.1 接口文档
1.2.2 代码实现
SettingsController
/**
* 设置陌生人问题
*/
@PostMapping("/questions")
public ResponseEntity questions(@RequestBody Map map) {
//获取参数
String content = (String) map.get("content");
settingsService.saveQuestion(content);
return ResponseEntity.ok(null);
}
SettingsService
//设置陌生人问题
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);
}
}
QuestionApi
tanhua-dubbo
工程中的QuestionApi
和QuestionApiImpl
中添加保存和更新方法
@Override
public void save(Question question) {
questionMapper.insert(question);
}
@Override
public void update(Question question) {
questionMapper.updateById(question);
}
1.3.1 接口文档
通知管理:对通知进行保存或者更新的操作
http://192.168.136.160:3000/project/19/interface/api/280
1.3.2 代码实现
SettingsController
/**
* 通知设置
*/
@PostMapping("/notifications/setting")
public ResponseEntity notifications(@RequestBody Map map) {
//获取参数
settingsService.saveSettings(map);
return ResponseEntity.ok(null);
}
SettingsService
//通知设置
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);
}
}
SettingsApi
tanhua-dubbo
工程中的SettingsApi
和SettingsApiImpl
中添加保存和更新方法
@Override
public void save(Settings settings) {
settingsMapper.insert(settings);
}
@Override
public void update(Settings settings) {
settingsMapper.updateById(settings);
}
1.3.1 接口文档
1.3.2 分页查询
vo对象
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;
}
}
SettingsController
/**
* 分页查询黑名单列表
*/
@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);
}
SettingService
//分页查询黑名单列表
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);
}
BlackListApi
@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);
}
UserInfoMapper
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);
}
MybatisPlusConfig
tanhua-dubbo-db
引导类开启mybatis-plus分页插件支持
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
使用mybatis-plus的分页: