⽂章⽬录
连接数据库
在SpringBoot的配置⽂件l中添加
spring:
jpa:
hibernate:
ddl-auto: update
show-sql:true
datasource:
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/blog?serverTimezone=CST
username: root
password:123456
连接到本机的名为blog的MySQL数据库,⽤户名为root,密码设置为⾃⼰的密码。没有MySQL数据库的搜索教程安装⼀下。实体设计
实体类
博客 Blog
博客分类 Type
博客标签 Tag
博客评论 Comment
⽤户 User
实体关系
Blog -> Type:多对⼀
Blog -> Tag:多对多
Blog -> User:多对⼀
Blog -> Comment:⼀对多
评论类⾃关联关系
⽗评论 parentComment -> ⼦评论 replayComment:⼀对多
Blog类的属性
标题
内容
⾸图
标记(原创、转载、翻译)
浏览量
开启赞赏
开启版权
开启评论
发布(不发布为草稿)
创建时间
更新时间
Type类和Tag类的属性superjunior m
名称
Comment类的属性
昵称
头像
评论内容
创建时间
User类的属性
昵称
潘之琳个人资料⽤户名
密码
邮箱
类型
头像
创建时间
更新时间
命名约定
Service/DAO层命名约定
获取单个对象的⽅法⽤get做前缀
获取多个对象的⽅法⽤list做前缀。
获取统计值的⽅法⽤ count做前缀。
插⼊的⽅法⽤save(推荐)或 insert做前缀。
删除的⽅法⽤ remover推荐)或 delete做前缀。
修改的⽅法⽤ update做前缀。
在项⽬中创建实体类
创建⼀个名为domian的包,⾥⾯存放实体类,细节
Blog.java
package yonmin.blog.domain;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity// 与数据库建⽴联系
@Table(name ="t_blog")// 创建⼀张名为 t_blog的表
public class Blog implements Serializable {
@Id// 主键
@GeneratedValue// ⾃增
private Long id;
private String title;
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 creatTime;
@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<>();
public Blog(){
}
姚晨老公曹郁public Long getId(){
董洁婚纱照return id;
}
public void setId(Long id){
this.id = id;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getContent(){
return Content;
}
public void setContent(String content){
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){
thismentabled = commentabled;
}
public boolean isPublished(){
return published;
}
public void setPublished(boolean published){
this.published = published;
}
public boolean isRecommend(){
public boolean isRecommend(){
return recommend;
}
public void setRecommend(boolean recommend){ end = recommend;
}
public Date getCreatTime(){
return creatTime;
}
public void setCreatTime(Date creatTime){
}
public Date getUpdateTime(){
return updateTime;
}
public void setUpdateTime(Date updateTime){
this.updateTime = updateTime;
}
public Type getType(){
return type;
}
public void setType(Type type){
}
public List<Tag>getTags(){
return tags;
山东旅游}
public void setTags(List<Tag> tags){
this.tags = tags;
}
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){ thisments = comments;
}
@Override
public String toString(){
return"Blog{"+
"id="+ id +
", title='"+ title +'\''+
", Content='"+ Content +'\''+
", firstPicture='"+ firstPicture +'\''+
", flag='"+ flag +'\''+
", views="+ views +
", appreciation="+ appreciation +
发布评论