【发布时间】:2018-02-06 06:00:15
【问题描述】:
我正在使用 Spring Boot 创建一个简单的博客应用程序,遵循(不完整的)教程:http://www.nakov.com/blog/2016/08/05/creating-a-blog-system-with-spring-mvc-thymeleaf-jpa-and-mysql/#comment-406107。
模型实体类如下,Post和User:
首先是 Post 的代码:
@Entity
@Table(name="posts")
public class Post {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@Column(nullable=false, length = 300)
private String title;
@Lob @Column(nullable=false)
private String body;
@ManyToOne(optional=false, fetch=FetchType.LAZY)
private User author;
@Column(nullable = false)
private Date date = new Date();
public Post() {
}
public Post(long id, String title, String body, User author) {
this.id = id;
this.title = title;
this.body = body;
this.author = author;
}
这是用户的代码:
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(nullable=false, length=30, unique=true)
private String username;
@Column(length=60)
private String passwordHash;
@Column(length=100)
private String fullName;
@OneToMany(mappedBy="author")
private Set<Post> posts = new HashSet<>();
public User() {
}
public User(Long id, String username, String fullName) {
this.id = id;
this.username = username;
this.fullName = fullName;
}
请注意,为方便起见,我省略了包、导入和 getter/setter。
以防万一,我包含了我的 application.properties 文件:
spring.thymeleaf.cache = false
server.ssl.enabled=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/blog_db
spring.datasource.username=root
spring.datasource.password=somepassword
#Configure Hibernate DDL mode: create/update
spring.jpa.properties.hibernmate.hbm2ddl.auto=update
我想为我的代码创建一个相应的数据库,以连接到使用 mysql 社区服务器(更具体地说是工作台),由于我完全不熟悉 mysql,所以我没有任何成功。 (tut作者未能提供db脚本,所以我正在尝试重新创建它)。
我希望有人愿意帮助我编写 mysql 数据库脚本。
【问题讨论】:
-
也许我误解了休眠的工作原理。似乎我需要做的就是创建数据库模式 blog_db 并且只要我的应用程序属性正确,hibernate 就会实例化表本身!哇......全新的令人敬畏的水平......猜想我回答了我自己的问题:)哈哈......
标签: mysql spring hibernate spring-mvc spring-boot