[英]Initial Data not loaded in Spring from data.sql
我的 Spring 应用程序中有以下 Config.java:
package config;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableTransactionManagement
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class})
@ComponentScans(value = { @ComponentScan("boot.entry"),
@ComponentScan("Controller"),
@ComponentScan("DAO"),
@ComponentScan("Model"),
@ComponentScan("securityHash"),
@ComponentScan("Service")})
public class Config {
@Value("${spring.datasource.driver-class-name}")
private String SPRING_DRIVER;
@Value("${spring.datasource.password}")
private String SPRING_PASSWORD;
@Value("${spring.datasource.url}")
private String SPRING_URL;
@Value("${spring.datasource.username}")
private String SPRING_USERNAME;
@Value("${hibernate.show_sql}")
private String HIBERNATE_SHOW_SQL;
@Value("${spring.jpa.properties.hibernate.dialect}")
private String HIBERNATE_DIALECT;
@Value("${entitymanager.packagesToScan}")
private String ENTITYMANAGER_PACKAGES_TO_SCAN;
@Value("${spring.devtools.livereload.enabled}")
private String LIVE_RELOAD;
@Value("${spring.jpa.open-in-view}")
private String OPEN_IN_VIEW;
@Value("${spring.datasource.initialization-mode}")
private String INITIALIZATION_MODE;
@Value("${spring.jpa.hibernate.ddl-auto}")
private String HIBERNATE_HBM2DDL_AUTO;
@Value("${spring.datasource.continue-on-error}")
private String CONTINUE_ON_ERROR;
@Value("${spring.jpa.generate-ddl}")
private String GENERATE_DDL;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
hibernateProperties.put("spring.jpa.properties.hibernate.dialect", HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
hibernateProperties.put("spring.devtools.livereload.enabled", LIVE_RELOAD);
hibernateProperties.put("spring.jpa.open-in-view", OPEN_IN_VIEW);
hibernateProperties.put("spring.datasource.initialization-mode", INITIALIZATION_MODE);
hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
hibernateProperties.put("spring.datasource.continue-on-error", CONTINUE_ON_ERROR);
hibernateProperties.put("spring.jpa.generate-ddl", GENERATE_DDL);
sessionFactory.setHibernateProperties(hibernateProperties);
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(SPRING_DRIVER);
dataSource.setUrl(SPRING_URL);
dataSource.setUsername(SPRING_USERNAME);
dataSource.setPassword(SPRING_PASSWORD);
return dataSource;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver= new InternalResourceViewResolver();
resolver.setPrefix("/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
以及 src/main/resources/ 文件夹中的以下 application.properties 文件:
spring.datasource.url=jdbc:mysql://localhost:3306/myDatabase?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&jdbcCompliantTruncation=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=mysql
entitymanager.packagesToScan=Model
hibernate.show_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.devtools.livereload.enabled=true
spring.jpa.open-in-view=true
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.continue-on-error=true
spring.jpa.generate-ddl=true
以及 src/main/resources 中的以下 data.sql:
INSERT INTO myDatabase.table1
(column1, column2, column3, column4, column5) VALUES ('ABC', '100', '1000000', '100', '0');
INSERT INTO myDatabase.table1
(column1, column2, column3, column4, column5) VALUES ('DEF', '100', '1000000', '100', '0');
INSERT INTO myDatabase.table1
(column1, column2, column3, column4, column5) VALUES ('GHI', '100', '1000000', '100', '0');
当我运行应用程序时,正在创建所有表。 这是来自日志:
Hibernate: drop table if exists products
Hibernate: create table table1 (column1 bigint not null auto_increment, column2 integer, column3 integer, column4 bigint, column5 bigint, primary key (column1)) engine=InnoDB
2020-11-19 00:27:38.931 INFO 5580 --- [ restartedMain] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@2e27f3fe'
但是数据没有插入到表中。 请帮忙。
你的 SQL 查询是错误的(column1 是 bigint 的主键类型,但你试图插入字符串(你不能插入任何数据。它是自动递增的)。这就是它不插入的原因。
查询应该是:----
INSERT INTO myDatabase.table1
( column2, column3, column4, column5) VALUES (100,100, 401421228216, 401421228216);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.