我使用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();
}
当我在
中执行此插入时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);
我收到了这个错误
user lacks privilege or object not found: PARSEDATETIME
在数据源资源管理器中执行相同的查询 - >数据库连接 - > 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
和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: (
以及建议的例子:
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
答案 0 :(得分:2)
我尝试通过从头开始创建具有spring-boot-starter-data-jpa
和h2
依赖项的Spring Boot项目来重现您的问题。我做了两件事:
1)使用/resources
和schema.sql
名称将您的脚本放在data.sql
中,以便相应地创建和填充数据库。默认情况下,Spring Boot 将按照here所述的那些位置加载SQL 。
2)我在testdb
中配置了application.properties
H2数据库,如下所示:
# 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/
结果是,H2数据库由您提供的示例数据填充,没有任何错误(我没有像DataSource
那样配置PersistenceConfig
,仅此而已/没别的)。
如果您想坚持使用自定义SQL脚本位置,请考虑按照此回答https://stackoverflow.com/a/41644743/2402959配置DataSource
。
答案 1 :(得分:1)
出于某种原因,存储函数PARSEDATETIME
的安装不允许您访问它。请提供SHOW CREATE FUNCTION PARSEDATETIME
。看看春天的东西。
或者,更有可能的是,PARSEDATETIME
是Java函数,而不是MySQL函数。
注意:您使用它的位置意味着它是一个MySQL函数。要将其用作Java函数,您需要将其“绑定”到INSERT
。
答案 2 :(得分:1)
您是否尝试过更改
中的插入语句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);
到
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, CALL PARSEDATETIME('03:05:06 GMT','HH:mm:ss z', 'en', 'GMT'), CALL PARSEDATETIME('03:05:06 GMT','HH:mm:ss z', 'en', 'GMT'), 1);