如何在Spring Boot 1.4.1中为@DataJpaTest添加mode = mysql到嵌入式H2数据库? [英] How to add the mode=mysql to embedded H2 DB in Spring Boot 1.4.1 for @DataJpaTest?
问题描述
在执行junit测试时,使用schema.sql文件创建我的sql架构时遇到一些问题,而此架构包含mysql特定表达式。我必须将 mode = mysql
添加到H2网址。
I have some problems with using a schema.sql file to create my sql schema when executing a junit test while this schema contains mysql specific expression. I have to add the mode=mysql
to the H2 url.
例如:
jdbc:h2:mem:testd; MODE = MYSQL
但Spring启动会自动使用enum
org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection中定义的url及其url
But Spring boot automatically uses the url defined in the enum org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection with its url
jdbc:h2:mem:testdb; DB_CLOSE_DELAY = -1; DB_CLOSE_ON_EXIT = FALSE
。
我尝试过类似的方法让它工作,但是春天没有采用 spring.datasource.url = jdbc:h2:mem:testdb; MODE = MYSQL
来自我的test-application.properties。我的test-application.properties中的所有其他设置都已成功读取。
I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL
from my test-application.properties. All other settings from my test-application.properties have been read successfully.
如果我让spring / hibernate使用javax创建模式(没有schema.sql文件)我的实体中的.persistence注释一切正常。
If I let spring/hibernate create the schema (without the schema.sql file) with the javax.persistence annotations in my entities everything works fine.
有没有简单的方法来添加模式?
Is there a simple way to add a mode?
推荐答案
我遇到了同样的问题。在运行测试时它不会获取URL。我正在使用flyway来管理我的脚本。按照以下几个步骤,我能够完成所有这些工作。
I was having this same issue. It would not pick up the url when running tests. I'm using flyway to manage my scripts. I was able to get all of these working together by following these few steps.
创建一个 V1_init.sql
脚本在src / test / resources / db / migration中,这是它第一个由flyway运行的脚本。
Created a V1_init.sql
script in src/test/resources/db/migration so that it is the first script run by flyway.
SET MODE MYSQL; /* another h2 way to set mode */
CREATE SCHEMA IF NOT EXISTS "public"; /* required due to issue with flyway --> https://stackoverflow.com/a/19115417/1224584*/
更新 application-test.yaml
包含模式名称public:
Updated application-test.yaml
to include the schema name public:
flyway:
schemas: public
确保测试指定了配置文件: @ActiveProfiles(test )
Ensure the test specified the profile: @ActiveProfiles("test")
这篇关于如何在Spring Boot 1.4.1中为@DataJpaTest添加mode = mysql到嵌入式H2数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!