我们准备设计博客,那就要设计数据库。

我们可以使用Hibernate来自动生成数据库。

博客数据库的结构:

实体类:

  • 博客 Blog
  • 博客分类 Type
  • 博客标签 Tag
  • 博客评论 Comment
  • 用户 User

项目截图:

首先,在pom.xml中添加以下的一些依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

这样就可以使用Hibernate框架了,下面实现自动创建数据库表的功能:

打开application.properties文件添加以下的代码:

  jpa:
hibernate:
ddl-auto: update
show-sql: true

这里除了update参数外还有其他的参数,这里解释一下:

 //1:value="create-drop" 表示当JPA应用的时候自动创建表,在解应用的时候删除相应的表,这个在做测试的时候比较有用,但在开发过程中不这么用

//2:value="create"这个在每次应用启动的时候都会创建数据库表(会删除以前数据库里的数据。

//3:value="update" 这个属性的作用是a:每次只会更新数据库表里的信息

下面是创建实体类的代码:

Blog

package com.lrm.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; @Entity
@Table(name = "t_blog")
public class Blog {
@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; private Date createTime; 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) {
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 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) {
this.comments = comments;
} @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 +
'}';
}
}

Comment

package com.lrm.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; @Entity
@Table(name = "t_comment")
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; 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;
} @Override
public String toString() {
return "Comment{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
", content='" + content + '\'' +
", avatar='" + avatar + '\'' +
", createTime=" + createTime +
'}';
}
}

Tag

package com.lrm.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List; @Entity
@Table(name = "t_tag")
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 + '\'' +
'}';
}
}

Type

package com.lrm.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List; @Entity
@Table(name = "t_type")
public class Type {
@Id
@GeneratedValue
private long id; 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 + '\'' +
'}';
}
}

User

package com.lrm.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; /**
* Created by limi on 2017/10/14.
*/
@Entity
@Table(name = "t_user")
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 +
'}';
}
}

OK,既然,实体类已将创建完毕,运行接口。就可以生成数据库。但是前提,你是配置数据库

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
username: root
password: root

测试结果:

真的很nice,确实是一波骚操作。

SpringBoot使用Hibernate,实现自动创建数据库表【博客数据库设计】的更多相关文章

  1. Springboot项目启动后自动创建多表关联的数据库与表的方案

    文/朱季谦 在一些项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据 ...

  2. SpringBoot+Mybatis 自动创建数据表(适用mysql)

    Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...

  3. springboot项目启动-自动创建数据表

    很多时候,我们部署一个项目的时候,需要创建大量的数据表.例如mysql,一般的方法就是通过source命令完成数据表的移植,如:source /root/test.sql.如果我们需要一个项目启动后, ...

  4. A.CTable 自动创建数据表

    1.添加依赖 <!-- A.CTable 自动创建数据表 --> <dependency> <groupId>com.gitee.sunchenbin.mybati ...

  5. oracle数据库表空间追加数据库文件方法

    oracle数据库表空间追加数据库文件方法   针对非大文件方式表空间,允许追加文件进行表空间的扩展,单个文件最大大小是32G  第一种方式:表空间增加数据文件    www.2cto.com   1 ...

  6. django初探-创建简单的博客系统

    django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...

  7. django初探-创建简单的博客系统(一)

    django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...

  8. django初探-创建简单的博客系统(二)

    上篇django初探-创建简单的博客系统(一)已经记录了Django实现博客的发布的整个过程,接下来继续说明博客标题和内容的显示. 显示博客详细 将博客内容保存到数据库还不是发布博客的终极目的,博客一 ...

  9. 学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用

    学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用 一.SpringBoot系列教程 二.SpringBoot ...

随机推荐

  1. linux SSH设置

    环境:centos7 一. ssh连接超时设置 (1)客户端设置(推荐) 客户端是windows系统 连接工具:SecureCRT 1.SecureCRT客户端->Options(选项)-> ...

  2. 01-day-vuex的使用

    知识点1===>简单的使用vuex 进行state取值 使用yarn下载 yarn add vuex -D vuex的包叫做 store 跟pages同级 创建store文件夹,文件夹下有sto ...

  3. RabbitMQ学习笔记(一、消息中间件基础)

    目录: 什么是消息中间件 消息中间件的作用 JMS规范 AMQP协议 RabbitMQ简介 Hello World 什么是消息中间件: 消息中间件(Message Queue Middleware,简 ...

  4. python3.5.3rc1学习二

    #if else示例x = 5y = 8 if x > y: print("x is greater than y")else: print("x is not g ...

  5. TransportFireWall

    1.shadow for python install on windows (1.) install python pip (2.) install openssl for windows (3.) ...

  6. luoguP2852 [USACO06DEC]Milk Patterns

    题意 显然如果有一个子串出现过\(k\)次,那么它必定是一个至少长为k的后缀序的\(LCP\),求出所有相邻的长为\(k-1\)的\(height\)数组的最小值,在其中取最大值即可 code: #i ...

  7. LeetCode98. 验证二叉搜索树

     验证二叉搜索树  *  * https://leetcode-cn.com/problems/validate-binary-search-tree/description/  *  * algor ...

  8. 解决 Github 图片加载慢的问题

    一.前言 本文主要介绍一种解决 Github 图片加载慢的方法,亲测有效. 笔者博客是使用 Github 作为图床,每次打开博客时的图片加载很慢或者根本加载不出来.这是因为 GitHub 的 CDN ...

  9. D3力布图绘制--节点间的多条关系连接线的方法(转)

    在项目中遇到这样的场景,在使用D3.js绘制力布图的过程中,需要在2个节点间绘制多条连接线,找到一个不错的算法,在此分享下. 效果图: HTML中要连接 <!DOCTYPE html> & ...

  10. 使用OpenCL提升OpenCV图像处理性能 | speed up opencv image processing with OpenCL

    本文首发于个人博客https://kezunlin.me/post/59afd8b3/,欢迎阅读最新内容! speed up opencv image processing with OpenCL G ...