Nuñ*_*ada 5 mysql spring spring-mvc h2 spring-boot
我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行JAR文件生成了一个Spring Boot Web应用程序.
使用的技术:
Spring Boot 1.4.2.RELEASE,Spring 4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat Embed 8.5.6,Maven 3,Java 8
这是我在启动数据库时调用的bean
@SpringBootApplication
@EnableAutoConfiguration
@Import({SecurityConfig.class})
public class BookApplication {
public static void main(String[] args) {
SpringApplication.run(BookApplication.class, args);
}
}
@Configuration
public class PersistenceConfig {
...
/**
* Creates an in-memory "books" database populated
* with test data for fast testing
*/
@Bean
public DataSource dataSource(){
return
(new EmbeddedDatabaseBuilder())
.addScript("classpath:db/H2.schema.sql")
.addScript("classpath:db/H2.data.sql")
.build();
}
Run Code Online (Sandbox Code Playgroud)
当我执行此插入时
CREATE TABLE IF NOT EXISTS t_time_lapse (
id bigint PRIMARY KEY,
name varchar(50) NOT NULL,
description varchar(200) NOT NULL,
sunday boolean DEFAULT NULL,
monday boolean DEFAULT NULL,
tuesday boolean DEFAULT NULL,
wednesday boolean DEFAULT NULL,
thursday boolean DEFAULT NULL,
friday boolean DEFAULT NULL,
saturday boolean DEFAULT NULL,
init_period date NOT NULL ,
end_period date NOT NULL ,
init_time time DEFAULT NULL,
end_time time DEFAULT NULL,
company_id bigint DEFAULT NULL,
FOREIGN KEY (company_id) REFERENCES public.t_company(id)
);
insert into T_TIME_LAPSE (ID, NAME, DESCRIPTION, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, INIT_PERIOD, END_PERIOD, INIT_TIME, END_TIME, COMPANY_ID)
values (9090,'key', 'key', 1,1,1,1,1,1,1,CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, PARSEDATETIME('03:05:06 GMT','HH:mm:ss z', 'en', 'GMT'), PARSEDATETIME('03:05:06 GMT','HH:mm:ss z', 'en', 'GMT'), 1);
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
user lacks privilege or object not found: PARSEDATETIME
Run Code Online (Sandbox Code Playgroud)
在Data Source Explorer中执行相同的查询 - > DataBase Connections - > SQL Scrapbook一切都很好!
SHOW CREATE FUNCTION PARSEDATETIME在脚本中添加:
Failed to execute SQL script statement #1 of class path resource [db/H2.data.sql]: SHOW CREATE FUNCTION PARSEDATETIME; nested exception is java.sql.SQLSyntaxErrorException: unexpected token: SHOW
Run Code Online (Sandbox Code Playgroud)
和 CREATE FUNCTION PARSEDATETIME;
Failed to execute SQL script statement #1 of class path resource [db/H2.data.sql]: CREATE FUNCTION PARSEDATETIME; nested exception is java.sql.SQLSyntaxErrorException: unexpected end of statement: required: (
Run Code Online (Sandbox Code Playgroud)
以及提出的例子:
Failed to execute SQL script statement #2 of class path resource [db/H2.data.sql]: INSERT INTO test values (1, CALL PARSEDATETIME('03:05:06 GMT','HH:mm:ss z', 'en', 'GMT')); nested exception is java.sql.SQLSyntaxErrorException: unexpected token: CALL
Run Code Online (Sandbox Code Playgroud)
小智 3
我尝试通过使用依赖项从头开始创建 Spring Boot 项目来重现您的spring-boot-starter-data-jpa问题h2。我做了两件事:
1)将脚本放入名称中,以便/resources相应地创建和填充数据库。默认情况下,Spring Boot将从这些位置加载 SQL ,如此处所述。schema.sqldata.sql
2)我已经配置了testdbH2数据库,application.properties如下所示:
# H2 database configuration
spring.datasource.url = jdbc:h2:file:~/testdb;DB_CLOSE_ON_EXIT=FALSE
# Enable SQL script scanning in /resources folder
spring.jpa.hibernate.ddl-auto=none
# Enable H2 console under http://localhost:8080/console/ for dev purposes
spring.h2.console.enabled=true
spring.h2.console.path=/console/
Run Code Online (Sandbox Code Playgroud)
结果是 H2 数据库由您提供的示例数据填充,没有任何错误(我没有DataSource像您那样进行配置PersistenceConfig,仅此而已)。
如果您想坚持使用自定义 SQL 脚本位置,请考虑配置DataSource以下答案/sf/answers/2915132041/。
| 归档时间: |
|
| 查看次数: |
840 次 |
| 最近记录: |