我的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'
但数据不会插入表中。请帮忙。
1条答案
按热度按时间f4t66c6m1#
您的sql查询错误(column1是bigint的主键类型,但您正在尝试插入字符串(您不能插入任何数据)。它是自动递增的)。这就是为什么它没有插入。
查询应为:----