项目亮点之通用的新模块开发流程

#通用的新模块开发流程

  1. Database Column(数据库都有哪些列)
  2. Model:模型定义,和数据库相匹配(对象的属性和数据库的column对应)
  3. DAO:数据读取(封装对数据库的操作)
  4. Service:服务包装(封装业务逻辑)
  5. Controller:业务入口
  6. Test 单元测试

采用新模块开发流程开发评论中心

##Database Column:

  • id(int):每条评论都有自己的ID
  • content(String):评论的内容
  • entity_id (int):就是newsID
  • entity_type(int):entity是哪种形式,new?还是comment?
  • created_date (Date):创建的日期
  • user_id(int):是哪个用户发的
  • status(int):

屏幕快照 2019-03-15 下午8.18.03

##Model:

实体类:

/**
 * 定义的实体
 */
public class Comment {
    private int id;
    private int userId;
    private int entityId;
    private int entityType;
    private String content;
    private Date createdDate;
    private int status;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getEntityId() {
        return entityId;
    }

    public void setEntityId(int entityId) {
        this.entityId = entityId;
    }

    public int getEntityType() {
        return entityType;
    }

    public void setEntityType(int entityType) {
        this.entityType = entityType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

DAO:

/**
 * 读取数据
 */
@Mapper
public interface CommentDAO{
    String TABLE_NAME = " comment ";
    String INSERT_FIELDS = " user_id, content, created_date, entity_id, entity_type, status ";
    String SELECT_FIELDS = " id, " + INSERT_FIELDS;

    @Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,
            ") values (#{userId},#{content},#{createdDate},#{entityId},#{entityType},#{status})"})
    int addComment(Comment comment);


    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where entity_type=#{entityType} and entity_id=#{entityId} order by id desc "})
    List<Comment> selectByEntity(@Param("entityId") int entityId, @Param("entityType") int entityType);

    @Select({"select count(id) from ", TABLE_NAME, " where entity_type=#{entityType} and entity_id=#{entityId}"})
    int getCommentCount(@Param("entityId") int entityId, @Param("entityType") int entityType);
}

通过注解使用的mybatis。通过这种ORM(Object Relational Mapping)框架,对数据库进行增删改查操作。

Service:

@Service
public class CommentService {
    private static final Logger logger = LoggerFactory.getLogger(QiniuService.class);

    @Autowired
    CommentDAO commentDAO;
    //通过entityId和entityType能唯一确定这条评论的类型和这种类型中的哪个
    public List<Comment> getCommentsByEntity(int entityId, int entityType) {
        return commentDAO.selectByEntity(entityId, entityType);
    }

    public int addComment(Comment comment) {
        return commentDAO.addComment(comment);
    }

    public int getCommentCount(int entityId, int entityType) {
        return commentDAO.getCommentCount(entityId, entityType);
    }
}

Controller:

com.nowcoder.controller.NewsController:

    //增加评论
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    public String addComment(@RequestParam("newsId") int newsId,
                             @RequestParam("content") String content){

        try {
            Comment comment=new Comment();
            comment.setUserId(hostHolder.getUser().getId());
            comment.setEntityId(newsId);
            comment.setEntityType(EntityType.ENTITY_NEWS);
            comment.setContent(content);
            comment.setCreatedDate(new Date());
            comment.setStatus(0);
            commentService.addComment(comment);
            //查看type=news的这条new(由newid标识)有多少条评论
            int count= commentService.getCommentCount(comment.getEntityId(),comment.getEntityType());
            //更新news里的评论数量
            newsService.updateCommentCount(comment.getEntityId(),count);

        }catch (Exception e){
            logger.error("添加评论失败"+e.getMessage());
        }
        return "redirect:/news/"+String.valueOf(newsId);
    }

映射处理的路径为:/addComment时,执行增加评论的操作,其实就是在数据库comment表中增加一条记录,并更新该条新闻的评论数量,此记录附带newID(就是entityID)和评论类型(entityType)。


   转载规则


《项目亮点之通用的新模块开发流程》 xuxinghua 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录
I I