【问题标题】:Unable to create MySQL table automatically using Spring Boot无法使用 Spring Boot 自动创建 MySQL 表
【发布时间】:2017-09-19 06:59:01
【问题描述】:

之前我能够使用我的程序在 MySQL 中创建一个表。但现在它似乎不起作用。 有人可以向我指出这个问题。

application.properties:

#jdbc
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/motsach
jdbc.username=root
jdbc.password=123456


#hibernate
hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto = create
hibernate.current.session.context.class = org.springframework.orm.hibernate5.SpringSessionContext
hibernate.show_sql=true
hibernate.format_sql=true

数据库配置文件:

@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setPackagesToScan(new String[] { "com.project.form.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.current.session.context.class", environment.getRequiredProperty("hibernate.current.session.context.class"));        
    return properties;
}

@Bean(name ="dataSource")
public DataSource getDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    System.out.println("## getDataSource: " + dataSource);
    return dataSource;
}

【问题讨论】:

  • 将“hibernate.hbm2ddl.auto = create”更改为“hibernate.hbm2ddl.auto = update”。或者最好只使用“spring.jpa.hibernate.ddl-auto = update”。不需要在hibernateProperties()中添加
  • 谢谢,但没有任何改变。
  • create 应该可以很好地在每次运行时删除和创建数据库
  • @D.Doe add spring.jpa.hibernate.ddl-auto = update". 在 application.properties 文件中。不需要在 hibernateProperties() 中添加
  • 谢谢老哥,还是报错。

标签: mysql spring hibernate spring-boot


【解决方案1】:

通过此配置,您可以在 spring boot 应用程序启动期间自动创建表。

server.contextPath=/demo-app
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

【讨论】:

    【解决方案2】:

    我正在使用application.yml 文件,这对我有用。

    Refered link

    # application.yml 
    spring:
        datasource:
            driverClassName: com.mysql.jdbc.Driver
            password: abcd
            url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/sonoo?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
            username: root
        jpa:
            hibernate.ddl-auto: update
            generate-ddl: true
            show-sql: true
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2021-01-13
    • 2018-05-22
    • 2017-03-05
    • 2018-01-17
    • 2022-01-24
    • 2018-05-11
    相关资源
    最近更新 更多