SpringBoot开源项目个人博客——(5)创建实体类

上一章已经创建了User类,接下来补充剩下的所有实体类,后续需要使用。

直接引用了oneStar的实体类结构

entity包中继续补充实体类。

1.博客实体类Blog

除了数据库中设计的属性外,由于外键关联着Type和User;comment和博客也有着逻辑外键的关系,这些一对多、多对一的关系,都需要在实体类中体现出来,。

所以除数据库中的字段外,还需要额外的变量,type,user,comments。

(省去get、set、toString、构造)此后也是,不再强调。

entity包中创建Blog实体类。

public class Blog {

    private Long id; //主键id
    private String title; //博客标题
    private String content; //博客内容
    private String firstPicture; //首图地址
    private String flag; //是否为原创
    private Integer views; //浏览次数
    private Integer commentCount; //评论次数
    private boolean appreciation; //是否开启赞赏
    private boolean shareStatement; //是否开启版权
    private boolean commentabled;//是否开启评论
    private boolean published;//是否发布
    private boolean recommend;//是否推荐
    private Date createTime;//创建时间
    private Date updateTime;//更新时间
    private String description; //博客描述

    private Long typeId;
    private Long userId;
    
    private User user;
    private Type type;
    private List<Comment> comments = new ArrayList<>();
    /*存 一对多 / 多对一 关系*/
    /*
    * 博客blog和分类type : 多 对 一
    * 博客blog和用户user : 多 对 一
    * 博客blog和评论Comment 一 对 多
    * */
}

typeId, userId就是为了在mybatis多表查询中做出结果映射,这样才能查出Blog中这些非普通java对象字段(复杂类型),comments类似。

2.分类实体类Type

上面博客和实体类有多对一的关系,同样在Type也需要表现出来这种关系。

这和后面的根据单个分类查询所有博客列表功能有关,有了需求就要做出相应处理。所以也不是有了关系就一定要多出对应字段(对于本项目来说,User自身不需要什么功能,别人关联它就行,它不需要关联别人)。


笔者的理解,仅供参考。

entity包下创建Type实体类

public class Type {
    private Long id;
    private String name;

    /*博客和类型: 多对一*/
    private List<Blog> blogs = new ArrayList<>();
}

3.评论实体类Comment

Comment和blog是逻辑外键,且没有对应的功能需要关联Blog类,就不需要多出Blog字段了,blogId足以。

除此之外,评论为了实现回复功能自身是有关联的,评论和回复是一对多的关系,需要回复评论集合(replyComments)用来存储评论的回复信息、(parentComment)存储父评论,(parentNickname)父评论昵称用来显示父级评论姓名。

public class Comment {

    private Long id;
    private String nickname;
    private String email;
    private String content;
    private String avatar;
    private Date createTime;
    private Long blogId; //博客id
    private Long parentCommentId; //父评论id(查询用)
    private boolean adminComment; //是否是管理员

    //回复评论  一对多(子)
    private List<Comment> replyComments = new ArrayList<>(); //回复
    private Comment parentComment; //父评论
    private String parentNickname; //父评论姓名
}

4.留言实体类Message

留言表是独立的,和Comment类似,只是没有关联blogId,属于给整个网站的评论,只需要自身的关联来实现评论及回复功能。

//和评论一样 少了博客id(blogId)
public class Message {

    private Long id;
    private String nickname;
    private String email;
    private String content;
    private String avatar;
    private Date createTime;
    private Long parentMessageId; //父留言id
    private boolean adminMessage; //是否是管理员

    //回复留言  一对多(子)
    private List<Message> replyMessages = new ArrayList<>();
    private Message parentMessage;
    private String parentNickname;
}

5.友链实体类

友链也是独立的,数据库中的字段就是其属性。

public class FriendLink {

    private Long id;
    private String blogaddress;
    private String blogname;
    private String pictureaddress;
    private Date createTime;
}

6.相册图片实体类

相册图片也是独立的,数据库中的字段就是其属性。

public class Picture {

    private Long id;
    private String picturename;
    private String pictureaddress;
    private String picturetime;
    private String picturedescription;
}

实体类构建完成,下次讲述分类管理。(因为博客外键关联分类,不能急着写博客管理)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值