【问题标题】:SpringHibernate - set up mysql tables for a Simple Blogging appSpring Hibernate - 为简单的博客应用程序设置 mysql 表
【发布时间】: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


【解决方案1】:

确保你的 pom.xml 中有所需的依赖项

 <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

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

    <!-- Use MySQL Connector-J -->

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

并且不要忘记将此行添加到您的 application.properties。一旦您运行了项目,请替换 create 以更新或 none,这将避免表的固有删除以创建新表。

spring.jpa.hibernate.ddl-auto=create

如果您有任何问题,以下链接将是一个好的开始

https://spring.io/guides/gs/accessing-data-mysql/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-14
    • 2013-08-10
    • 1970-01-01
    • 2012-06-29
    • 2011-05-07
    • 2013-10-06
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多