仿9GAG制作过程(五)
仿9GAG制作过程(五)
- 有话要说
- 数据库设计
- 接口设计
- 后台结构
- 具体例子
- bean类
- 消息实体类
- 段子消息实体类
- 段子实体类
- dao层
- 查询段子方法
- servlet层
- 反思
- 预告
有话要说
在做完了数据展示功能之后,就想着完善整个APP。发现现在后台非常的混乱,有好多点都不具备,比方说:图片应该有略缩图和原图,段子、评论、点赞应该联动起来,段子应该有创建时间等。
于是就重新设计了数据库,重新爬取了数据,重新设计了后台接口。
这次主要讲这次重构的主要内容。
数据库设计
一共设计了六张表,分别为:
- 段子表,主要存放每一个段子的图片等信息
- 评论表,主要存放评论信息,评论可以上传图片
- 用户表
- 标签表,每条段子发表之前会自定义标签,该表存放的就是这些标签
- 点赞记录表,因为用户点赞与段子之间是多对多的关系,因此要加一张表用来存放点赞记录
- 段子标签关联表,因为段子和标签是多对多的,因此需要多一张表存放关联关系
接口设计
橙色的为表,咖啡色为接口。
目前设计了十四个接口,上图写明了各接口和相关的表之间的关系。
后台结构
bean
包下为基本实体类;
implement
包下为消息实体类的子类;
dao
包为涉及到数据库的具体实现类;
servlet
为接口类;
util
为过程中用到的工具类。
具体例子
下面以查询段子接口为例,介绍具体的结构。
bean类
消息实体类
public class MessageEntity {// 返回信息描述private String reason;// 返回码private int errorCode;public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}public int getErrorCode() {return errorCode;}public void setErrorCode(int errorCode) {this.errorCode = errorCode;}}
段子消息实体类
public class TopicMessageEntity extends MessageEntity {// 获取段子的结果private List<TopicEntity> result;public List<TopicEntity> getResult() {return result;}public void setResult(List<TopicEntity> result) {this.result = result;}}
段子实体类
public class TopicEntity {// 段子标识private int id;// 段子作者private String author = "";// 段子标题private String title = "";// 段子点赞数private int upvote;// 段子评论数private int commentCount;// 段子略缩图地址private String thumbNail = "";// 段子原图地址private String orgPicture = "";// 段子发表时间private String postTime = "";// 点的是赞还是踩,0代表没点,1代表赞,-1代表踩private int like = 0;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getUpvote() {return upvote;}public void setUpvote(int upvote) {this.upvote = upvote;}public int getCommentCount() {return commentCount;}public void setCommentCount(int commentCount) {this.commentCount = commentCount;}public String getThumbNail() {return thumbNail;}public void setThumbNail(String thumbNail) {this.thumbNail = thumbNail;}public String getOrgPicture() {return orgPicture;}public void setOrgPicture(String orgPicture) {this.orgPicture = orgPicture;}public String getPostTime() {return postTime;}public void setPostTime(String postTime) {this.postTime = postTime;}public int getLike() {return like;}public void setLike(int like) {this.like = like;}}
这里和数据库表略有不同,主要是like
字段。
like
字段代表的是当前获取数据的人对该段子是否点了赞。
dao层
查询段子方法
public List<TopicEntity> query(int topicId, int count, boolean after) {List<TopicEntity> topicList = new ArrayList<TopicEntity>();if (topicId <= 0) {topicId = 0;}if (count <= 0) {count = 10;}if (after) {queryAfter(topicId, count, topicList);} else {queryBefore(topicId, count, topicList);}return topicList;}
private void queryAfter(int topicId, int count, List<TopicEntity> topicList) {String queryAfter = "SELECT * FROM 9gag_topics WHERE id > ? LIMIT ?";Connection conn = DatabaseUtil.getConnection();PreparedStatement pstmt = null;ResultSet rs = null;try {pstmt = conn.prepareStatement(queryAfter);pstmt.setInt(1, topicId);pstmt.setInt(2, count);rs = pstmt.executeQuery();while (rs.next()) {TopicEntity topicEntity = new TopicEntity();topicEntity.setId(rs.getInt("id"));topicEntity.setAuthor(rs.getString("author"));topicEntity.setTitle(rs.getString("title"));topicEntity.setUpvote(rs.getInt("upvote"));topicEntity.setCommentCount(rs.getInt("commentcount"));topicEntity.setThumbNail(rs.getString("thumbnail"));topicEntity.setOrgPicture(rs.getString("orgpicture"));topicEntity.setPostTime(rs.getString("posttime"));topicList.add(topicEntity);}} catch (SQLException e) {e.printStackTrace();} finally {DatabaseUtil.close(conn, pstmt, rs);}}
private void queryBefore(int topicId, int count, List<TopicEntity> topicList) {String queryBefore = "SELECT * FROM 9gag_topics WHERE id < ? ORDER BY id DESC LIMIT ?";Connection conn = DatabaseUtil.getConnection();PreparedStatement pstmt = null;ResultSet rs = null;try {pstmt = conn.prepareStatement(queryBefore);pstmt.setInt(1, topicId);pstmt.setInt(2, count);rs = pstmt.executeQuery();while (rs.next()) {TopicEntity topicEntity = new TopicEntity();topicEntity.setId(rs.getInt("id"));topicEntity.setAuthor(rs.getString("author"));topicEntity.setTitle(rs.getString("title"));topicEntity.setUpvote(rs.getInt("upvote"));topicEntity.setCommentCount(rs.getInt("commentcount"));topicEntity.setThumbNail(rs.getString("thumbnail"));topicEntity.setOrgPicture(rs.getString("orgpicture"));topicEntity.setPostTime(rs.getString("posttime"));topicList.add(topicEntity);}} catch (SQLException e) {e.printStackTrace();} finally {DatabaseUtil.close(conn, pstmt, rs);}// 获取完数据之后逆序,因为查找的时候是逆序Collections.reverse(topicList);}
这三个方法实现了查询指定段子前(或者后)count条记录。
servlet层
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/json; charset=utf-8");PrintWriter out = response.getWriter();TopicMessageEntity message = new TopicMessageEntity();TopicDAO topicDao = new TopicDAO();UpvoteDAO upvoteDao = new UpvoteDAO();Gson gson = GsonUtil.getGson();request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");int topicId = Integer.parseInt(request.getParameter("topicId"));int count = Integer.parseInt(request.getParameter("count"));boolean after = Boolean.parseBoolean(request.getParameter("after"));String author = request.getParameter("author");if (count <= 0) {message.setErrorCode(-1);message.setReason("count值不能为负数!");out.print(gson.toJson(message));return;}try {List<TopicEntity> topics = topicDao.query(topicId, count, after);// 判断作者是否点过赞if (author != null) {List<UpvoteEntity> upvoteList = upvoteDao.findUpvoteByAuthor(author, true);if (upvoteList != null) {for (TopicEntity topic : topics) {for (UpvoteEntity upvote : upvoteList) {if (upvote.getLikedId() == topic.getId()) {int like = upvote.isLiked() ? 1 : -1;topic.setLike(like);}}} }}Collections.reverse(topics);message.setErrorCode(0);message.setReason("success");message.setResult(topics);} catch (Exception e) {message.setErrorCode(-1);message.setReason(e.getMessage());} finally {out.print(gson.toJson(message));}}
主要逻辑:查找到需要的段子→遍历段子→如果段子被点过赞或者踩,就把段子相应字段更改为赞或者踩→由于查出来的数据时顺序的,要改为逆序展示。
反思
这次主要重构了后台的设计逻辑,其实还有好多不完备的地方。
通过这次重构,明白了一个要点。要做一件事情首先要规划好,首先是设计,把一切的流程,框架设计好之后按部就班的做。这样做出来的东西才会比较好。
否则在过程中会很混乱,严重影响效率。
预告
下一章准备讲述点赞的逻辑,因为点赞的逻辑比较复杂。
仿9GAG制作过程(五)相关推荐
- 嘉立创电路板制作过程全流程详解(五):测试、锣边、V-CUT、QC、发货
第1篇文章,点击这里:嘉立创电路板制作过程全流程详解(一):MI.钻孔 第2篇文章,点击这里:嘉立创电路板制作过程全流程详解二:沉铜.线路 第3篇文章,点击这里:嘉立创电路板制作过程全流程详解三:图电 ...
- (转载)连连看游戏外挂详细制作过程
标 题: [原创]新人入手第一个游戏外挂,附上详细制作过程 作 者: caigui 时 间: 2013-01-09,00:56:16 链 接: http://bbs.pediy.com/showthr ...
- uniapp、uniCloud实现微信公众号自动查询淘宝京东优惠券制作过程
uniapp.uniCloud实现微信公众号自动查询淘宝京东优惠券制作过程 微信公众号自动查询淘宝京东优惠券机器人制作教程.服务器通过uniapp提供的uniCloud云服务搭建,建议使用阿里云,不要 ...
- c语言佛像怎么打,佛像的制作过程,让你大开眼界!
佛像的制作过程,让你大开眼界! 中国遗存着大量古代建筑,尤以宗教建筑居多.在佛寺.道观内散落着众多精美的历代泥塑.今天就来看看泥塑是怎么从一坨泥巴变成金光闪闪的佛像的. 细腻.黏性强的泥土即可,一般在 ...
- TTS数据制作过程分享
TTS数据制作过程分享 本文来自:数据堂AI开放实验室 概述: TTS(Text To Speech)又称语音合成,是一种将文本转化成相应语音的技术.TTS技术从诞生到现在已经有200多年的历史.在1 ...
- 随着国产动漫的崛起,越来越好奇3D动漫的制作过程了
我们首先来了解一下,3d动画的定义. 三维动画软件在计算机中首先建立一个虚拟的世界,设计师在这个虚拟的三维世界中按照要表现的对象的形状尺寸建立模型以及场景,再根据要求设定模型的运动轨迹.虚拟摄影机的运 ...
- java斗地主程序制作过程
java斗地主程序制作过程 效果 项目概况 代码分布 效果 项目概况 代码分布 首先对卡牌进行编写 package com;import java.awt.Point; import java.awt ...
- 嘉立创电路板制作过程全流程详解(二):沉铜、线路
上一篇文章,我们了解了第1道和第2道工序,MI和钻孔,这篇文章,我们将了解第3道工序和第4道工序:沉铜和线路. 看上一篇文章,点击这里: 嘉立创电路板制作过程全流程详解(一):MI.钻孔 第3道工序: ...
- 手机ROM简单制作过程
1.安卓基础知识普及: Android一词的本义指"机器人",同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件. ...
- html网站制作流程,网页制作过程(实际案例)
网页制作过程概括为几步: 0)前提:拿到UI设计师制作好的网页UI图:(photoshop中-------UI设计师) 1)切图(photoshop中------美工) 2)输出切图&并将大U ...
最新文章
- Ubuntu下使用CMake编译OpenSSL源码操作步骤(C语言)
- struts.properties配置详解
- Variable W already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE……
- strstr和strcchr查找字符串和区别
- 欢乐纪中A组周六赛【2019.5.25】
- 在比特币世界中矿工的作用是什么
- ASP.NET MVC Html.BeginForm用法1
- Python 学习---------Day4
- Linux学习----文件的使者-Rsync(马哥教育原创)
- 基于springboot+mysql的房地产中介管理系统
- 电子书格式转换方法集锦
- puzzle(010.1)自我指涉的选择题
- 定位弹窗软件----找到真凶并做后续防御
- ceph课程(一)ceph组件与原理
- FreeRTOS+STM32L+BC20+MQTT协议传输温湿度数据,控制继电器云平台——④华为云
- c语言ifmn除2余1,C语言编程题及答案 2
- 学生成绩录入案例----python基础入门
- oracle11g 迁移至达梦8数据库环境
- 微信公众平台java开发详解
- unity 调用Anidroid 相册 选取图片裁剪