mysql - 无法使用 Spring Boot 自动创建 MySQL 表

标签 mysql spring hibernate spring-boot

之前我能够使用我的程序在 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;
}

最佳答案

通过此配置,您可以在 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

关于mysql - 无法使用 Spring Boot 自动创建 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46293890/

相关文章:

java - 如何解决: org. hibernate.HibernateException: createCriteria在没有 Activity 事务的情况下无效

MySQL Varchar 数据在第 1 行为列 name_last 截断

java - 带有文件返回的 Ajax 发布

java - 如何使用 springdoc-openapi-starter-webmvc-ui :2. 0.2 在 Spring Boot 中访问/v3/api-docs 时修复 http 500 错误?

java - 如何在Spring集成DSL中为 channel 设置多个消息处理程序?

sql - PostgreSQL:大对象的权限被拒绝

不存在关系时的 Hibernate JOIN FETCH

MySQL 删除旧记录

MySQL:将数据从一个表移动到具有不同模式的另一个表

php - 显示由类别和/或标签确定的 mySQL 图像