实体类
博客 Blog
博客分类Type
博客标签Tag
博客评论Comment
用户User
1.实体关系:
2、评论类自关联关系
3.Blog类
4.Comment类
5.User类
6.应用分层(数据库表)
7.命名约定
Service/DAO层命名约定:
- 获取单个对象的方法用get做前缀
- 获取多个对象的方法用list做前缀
- 获取统计值的方法用count做前缀
- 插入的方法用save(推荐)或insert做前缀
- 删除的方法用remove(推荐)或delete做前缀
- 修改的方法用update做前缀
8.新建po目录
9.新建Blog实体类
package com.xl.blog.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity(name = "t_blog")
@Table
public class Blog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; //主键
private String title; //标题
@Basic(fetch = FetchType.LAZY) //使用时才会用
@Lob //大字段内容
private String content; //内容
private String firstPicture; //首图
private String flag; //标记
private Integer views; //浏览次数
private boolean appreciation; //赞赏开启
private boolean shareStatement; //版权开启
private boolean commentabled; //评论开启
private boolean published; //发布
private boolean recommend; //推荐
@Temporal(TemporalType.TIMESTAMP) //设置时间生成的格式
private Date createTime; //创建时间
@Temporal(TemporalType.TIMESTAMP) //设置时间生成的格式
private Date updateTime; //更新时间
/*关系维护端*/
@ManyToOne /*多对一*/
private Type type;
@ManyToMany(cascade = {CascadeType.PERSIST})/*级联新增*/
private List<Tag> tags=new ArrayList<>();
@ManyToOne
private User user;
@OneToMany(mappedBy = "blog")
private List<Comment> comments=new ArrayList<>();
@Transient
private String tagIds;
private String description;
public Blog() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFirstPicture() {
return firstPicture;
}
public void setFirstPicture(String firstPicture) {
this.firstPicture = firstPicture;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public Integer getViews() {
return views;
}
public void setViews(Integer views) {
this.views = views;
}
public boolean isAppreciation() {
return appreciation;
}
public void setAppreciation(boolean appreciation) {
this.appreciation = appreciation;
}
public boolean isShareStatement() {
return shareStatement;
}
public void setShareStatement(boolean shareStatement) {
this.shareStatement = shareStatement;
}
public boolean isCommentabled() {
return commentabled;
}
public void setCommentabled(boolean commentabled) {
this.commentabled = commentabled;
}
public boolean isPublished() {
return published;
}
public void setPublished(boolean published) {
this.published = published;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Comment> getComments() {
return comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
public String getTagIds() {
return tagIds;
}
public void setTagIds(String tagIds) {
this.tagIds = tagIds;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void init(){
this.tagIds=tagsToIds(this.getTags());
}
//tags集合 内容:1,2,3
private String tagsToIds(List<Tag> tags) {
if (!tags.isEmpty()) {
StringBuffer ids = new StringBuffer();
boolean flag = false;
for (Tag tag : tags) {
if (flag) {
ids.append(",");
} else {
flag = true;
}
ids.append(tag.getId());
}
return ids.toString();
} else {
return tagIds;
}
}
@Override
public String toString() {
return "Blog{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", firstPicture='" + firstPicture + '\'' +
", flag='" + flag + '\'' +
", views=" + views +
", appreciation=" + appreciation +
", shareStatement=" + shareStatement +
", commentabled=" + commentabled +
", published=" + published +
", recommend=" + recommend +
", createTime=" + createTime +
", updateTime=" + updateTime +
", type=" + type +
", tags=" + tags +
", user=" + user +
", comments=" + comments +
", tagIds='" + tagIds + '\'' +
", description='" + description + '\'' +
'}';
}
}
10.新建type类:
package com.xl.blog.po;
import org.hibernate.validator.constraints.NotBlank;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
* 功能:博客分类
* 作者:xl
* 日期:2021-06-11
*/
@Entity(name = "t_type")
@Table
public class Type {
@Id
@GeneratedValue
private Long id;
@NotBlank(message="分类名称不能为空")
private String name;
/*关系被维护端*/
@OneToMany(mappedBy = "type")
private List<Blog> blogs=new ArrayList<>();
public Type() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
@Override
public String toString() {
return "Type{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
11.新建Comment类:
package com.xl.blog.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 功能:博客评论实体类
* 作者:xl
* 日期:2021-06-11
*/
@Entity(name = "t_comment")
@Table
public class Comment {
@Id
@GeneratedValue
private Long id;
private String nickname;//昵称
private String email; //邮箱
private String content;//评论的内容
private String avatar;//头像
@Temporal(TemporalType.TIMESTAMP) //设置时间生成的格式
private Date createTime;//创建时间
@ManyToOne
private Blog blog;
@OneToMany(mappedBy = "parentComment")
private List<Comment> replyComments=new ArrayList<>();
@ManyToOne
private Comment parentComment;
private boolean adminComment;//标记是否为管理员
public Comment() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Blog getBlog() {
return blog;
}
public void setBlog(Blog blog) {
this.blog = blog;
}
public List<Comment> getReplyComments() {
return replyComments;
}
public void setReplyComments(List<Comment> replyComments) {
this.replyComments = replyComments;
}
public Comment getParentComment() {
return parentComment;
}
public void setParentComment(Comment parentComment) {
this.parentComment = parentComment;
}
public boolean isAdminComment() {
return adminComment;
}
public void setAdminComment(boolean adminComment) {
this.adminComment = adminComment;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
", content='" + content + '\'' +
", avatar='" + avatar + '\'' +
", createTime=" + createTime +
", blog=" + blog +
", replyComments=" + replyComments +
", parentComment=" + parentComment +
", adminComment=" + adminComment +
'}';
}
}
12.新建tag类:
package com.xl.blog.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
* 功能:博客标签
* 作者:xl
* 日期:2021-06-11
*/
@Entity(name = "t_tag")
@Table
public class Tag {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany(mappedBy = "tags")
private List<Blog> blogs=new ArrayList<>();
public Tag() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
@Override
public String toString() {
return "Tag{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
13.新建user类:
package com.xl.blog.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity(name = "t_user")
@Table
public class User {
@Id
@GeneratedValue
private Long id;
private String nickname;//昵称
private String username;//用户名
private String password;//密码
private String email;//邮箱
private String avatar;//头像
private Integer type;//类型
@Temporal(TemporalType.TIMESTAMP) //设置时间生成的格式
private Date createTime;//创建时间
@Temporal(TemporalType.TIMESTAMP) //设置时间生成的格式
private Date updateTime;//更新时间
@OneToMany(mappedBy = "user")
private List<Blog> blogs=new ArrayList<>();
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", avatar='" + avatar + '\'' +
", type=" + type +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}