开发者社区 问答 正文

无法启动Spring以自动创建数据库架构?mysql

我无法启动启动时自动启动数据库架构的Spring Boot。

这是我的application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=test spring.datasource.password= spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.jpa.database = MYSQL

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = create spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy 这是我的Application.java:

@EnableAutoConfiguration @ComponentScan public class Application { public static void main(final String[] args){ SpringApplication.run(Application.class, args); } } 这是一个示例实体:

@Entity @Table(name = "survey") public class Survey implements Serializable {

private Long _id;

private String _name;

private List<Question> _questions;

/**
 * @return survey's id.
 */
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "survey_id", unique = true, nullable = false)
public Long getId() {
    return _id;
}

/**
 * @return the survey name.
 */
@Column(name = "name")
public String getName() {
    return _name;
}


/**
 * @return a list of survey questions.
 */
@OneToMany(mappedBy = "survey")
@OrderBy("id")
public List<Question> getQuestions() {
    return _questions;
}

/**
 * @param id the id to set to.
 */
public void setId(Long id) {
    _id = id;
}

/**
 * @param name the name for the question.
 */
public void setName(final String name) {
    _name = name;
}

/**
 * @param questions list of questions to set.
 */
public void setQuestions(List<Question> questions) {
    _questions = questions;
}

} 有什么想法我做错了吗?

展开
收起
保持可爱mmm 2020-05-17 10:18:55 756 分享 版权
1 条回答
写回答
取消 提交回答
  • 有几种可能的原因:

    您的实体类位于与之相对的同一个子包中,或者在一个子包中,@EnableAutoConfiguration.如果没有,则您的spring应用不会看到它们,因此不会在db中创建任何内容 检查您的配置,似乎您正在使用某些休眠特定选项,尝试将其替换为:

    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=test spring.datasource.password= 您application.properties必须在src/main/resources文件夹中。

    如果您未正确指定方言,则可能会尝试默认将其与启动内存数据库捆绑在一起,并且(与我一样)我会看到它尝试连接到本地HSQL实例(请参阅控制台输出),并且无法更新模式。来源:stack overflow

    2020-05-17 10:36:58
    赞同 展开评论