基于分布式教育平台
1、项目架构
1.1 项目介绍
- 提供给学员观看技术视频的网站。
- 学员使用手机号注册登录后,可以选择适合自己的课程,并观看课程视频。课程包含免费与VIP付费课程。
2.2 前端技术选型
-
Vue.js :是一套用于构建用户界面的渐进式JavaScript框架
-
Element UI库:饿了么前端出品的基于 Vue.js的 后台组件库,方便程序员进行页面 快速布局和构建
-
node.js :简单的说 Node.js 就是运行在服务端的 JavaScript 运行环境
-
axios :对ajax的封装, 简单来说就是ajax技术实现了局部数据的刷新,axios实现了对ajax 的封装后端技术选型
2.3 后端技术选型
- Web层:借助springmvc接收请求,进行视图跳转
- Service层 :借助spring进行IOC、AOP、及事务管理
- dao层: 借助mybatis进行数据库交互
- EasyCode插件: IDEA快速生成实体类的插件
- Zookeeper :服务注册与服务发现
- Dubbo :分布式框架,远程RPC调用
- Redis: 内存数据库,缓存
- Lombok :消除实体类中冗余的get和set
- SpringSocial :SpringSocial (Spring社交),简单理解就是和第三方应用打交道,微信登录用
2.4 分布式架构
controller
-
前台访问controller,消费方作为一台单独的服务器,远程调用注册中心(zookeeper+dubbo)的服务,再给予反馈。
-
服务消费方大体一览
- Spring配置文件
- fastDFS配置文件
service
- 服务提供方同样将自己的服务暴露出去(注册中心),消费方再远程引用。
- Spring配置文件:将服务暴露
2、点赞功能实现
2.1 功能分析
- 点赞:
- 先查看用户在该留言下是否存在点赞信息。
- 存在:更新状态,将留言用户点赞表(记录用户点赞,主要包含留言Id、用户Id、点赞状态(0=已点赞、1=取消赞))的点赞状态设为0。
- 将留言表点赞总数+1。
- 取消赞:
- 将留言用户点赞表中状态设为1。
- 将留言表点赞总数-1。
2.2 sql语句
- course_comment_favorite_record :留言用户点赞表
- course_comment:留言表
<!--查询评论下是否有该用户的评论信息-->
<select id="existsFavorite" resultType="int">
select count(*) from course_comment_favorite_record where
comment_id=#{cid} and user_id=#{uid}
</select>
<!--没有点过赞,保存点赞信息-->
<insert id="saveCommentFavorite">
insert into
`course_comment_favorite_record`(user_id,`comment_id`,`is_del`,`create_time`,`update_time`)
values
(#{uid},#{cid},0,sysdate(),sysdate())
</insert>
<!--点过赞 更新点赞状态-->
<update id="updateFavoriteStatus" >
update `course_comment_favorite_record` set is_del=#{status}
where comment_id = #{cid} and user_id = #{uid}
</update>
<!--更新评论赞数量-->
<update id="updateLikeCount" >
update course_comment set like_count=like_count+#{x}
where id=#{comment_id}
</update>
2.3 dao接口
/**
* 查询评论下是否有该用户的评论信息
* @param comment_id 留言编号
* @param userid 用户编号
* @return 0:不存在 1存在
*/
Integer existsFavorite(@Param("cid")Integer comment_id,@Param("uid")Integer userid);
/**
* 保存点赞信息
* @param comment_id 留言编号
* @param userid 用户编号
* @return 0:保存失败,1:保存成功
*/
Integer saveCommentFavorite(@Param("cid")Integer
comment_id,@Param("uid")Integer userid);
/**
* 更新点赞信息的状态(将is_del=0,表示已赞)
* @param status 状态,0:已赞,1:取消赞
* @param comment_id 留言编号
* @param userid 用户编号
* @return 0:保存失败,1:保存成功
*/
Integer updateFavoriteStatus( @Param("status")Integer status,
@Param("cid")Integer comment_id,@Param("uid")Integer userid);
/**
* 更新点赞的数量
* @param x +1的话,赞的数量增加,-1的话,赞的数量减少
* @param comment_id 某条留言的编号
* @return 0:保存失败,1:保存成功
*/
Integer updateLikeCount(@Param("x")Integer x ,@Param("comment_id")Integer
comment_id);
2.4 service接口实现类
/**
* 点赞
* @param comment_id 留言编号
* @param userid 用户编号
* @return
*/
@Override
public Integer saveFavorite(Integer comment_id, Integer userid) {
//1.查看评论下是否有该用户的点赞信息
Integer existsFavorite = courseCommentDao.existsFavorite(comment_id, userid);
if (existsFavorite==0){//不存在,新增点赞信息
courseCommentDao.saveCommentFavorite(comment_id,userid);
}else{
//存在,更新点赞信息 将is_del修改为0
courseCommentDao.updateFavoriteStatus(0,comment_id,userid);
}
//更新留言表中赞数量
courseCommentDao.updateLikeCount(1,comment_id);
return comment_id;
}
/**
* 取消赞
* @param comment_id 留言编号
* @param userid 用户编号
* @return
*/
@Override
public Integer cancelFavorite(Integer comment_id, Integer userid) {
//将用户赞状态 is_del修改为1
courseCommentDao.updateFavoriteStatus(1,comment_id,userid);
//更新留言表中赞数量
courseCommentDao.updateLikeCount(-1,comment_id);
return comment_id;
}
courseCommentDao.updateFavoriteStatus(1,comment_id,userid);
//更新留言表中赞数量
courseCommentDao.updateLikeCount(-1,comment_id);
return comment_id;
}
查询留言包括点赞信息
- 首页显示所有留言时,将点赞表作为集合传入留言实体。
-
查询所有留言sql编写
-
注意:此处要筛选点赞状态为0(is_del)的点赞信息,使用左外连接 连接点赞表(ccfr)。
-
resultMap
-
前端动态显示点赞样式: