java 如何在 Spring Boot 1.4.1 中为 @DataJpaTest 添加 mode=mysql 到嵌入式 H2 DB?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42364935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:34:13  来源:igfitidea点击:

How to add the mode=mysql to embedded H2 DB in Spring Boot 1.4.1 for @DataJpaTest?

javamysqlhibernatespring-booth2

提问by Marco

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=mysqlto the H2 url.

我在执行 junit 测试时使用 schema.sql 文件创建我的 sql 模式时遇到了一些问题,而该模式包含 mysql 特定表达式。我必须添加mode=mysql到 H2 网址。

For example something like this: jdbc:h2:mem:testd;MODE=MYSQL

例如这样的事情: jdbc:h2:mem:testd;MODE=MYSQL

But Spring boot automatically uses the url defined in the enum org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection with its url

但是 Spring boot 会自动使用枚举 org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection 中定义的 url 及其 url

jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE.

jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE.

I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQLfrom my test-application.properties. All other settings from my test-application.properties have been read successfully.

我尝试过类似的方法来spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL让它工作,但 spring 没有从我的 test-application.properties 中获取。我的 test-application.properties 中的所有其他设置都已成功读取。

If I let spring/hibernate create the schema (without the schema.sql file) with the javax.persistence annotations in my entities everything works fine.

如果我让 spring/hibernate 在我的实体中使用 javax.persistence 注释创建模式(没有 schema.sql 文件),一切正常。

Is there a simple way to add a mode?

有没有简单的方法来添加模式?

回答by anztenney

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.

我遇到了同样的问题。运行测试时它不会获取 url。我正在使用 flyway 来管理我的脚本。通过遵循以下几个步骤,我能够让所有这些工作一起工作。

Created a V1_init.sqlscript in src/test/resources/db/migration so that it is the first script run by flyway.

V1_init.sql在 src/test/resources/db/migration 中创建一个脚本,使其成为 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*/

Updated application-test.yamlto include the schema name public:

更新application-test.yaml以包含架构名称 public:

flyway:
  schemas: public

Ensure the test specified the profile: @ActiveProfiles("test")

确保测试指定了配置文件: @ActiveProfiles("test")

回答by Evgeny Batalov

Set

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL

in application-test.properties, plus

在 application-test.properties 中,加上

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")

on the test class

在测试课上

回答by Slava Semushin

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

我尝试过类似的方法来让它工作,但 spring 没有从我的 test-application.properties 中获取 spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL

Did you try to append this parameters instead of rewriting the existing ones?

您是否尝试附加此参数而不是重写现有参数?

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL

All other settings from my test-application.properties have been read successfully.

我的 test-application.properties 中的所有其他设置都已成功读取。

I thought that file should be named application-test.properties.

我认为该文件应该命名为application-test.properties.

回答by Sergey Ponomarev

I was able to run it with this config:

我能够使用此配置运行它:

# for integration tests use H2 in MySQL mode
spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_LOWER=TRUE;MODE=MySQL;
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect

The main trick here is to force Hibernate to generate SQL scripts for MariaDB dialect because otherwise Hibernate tries to use H2 dialect while H2 is already waiting for MySQL like commands.

这里的主要技巧是强制 Hibernate 为 MariaDB 方言生成 SQL 脚本,否则 Hibernate 会尝试使用 H2 方言,而 H2 已经在等待 MySQL 之类的命令。

Also I tried to use more fresh MariaDB103Dialectfor MariaDB 10.3 but it doesn't worked properly.

此外,我尝试MariaDB103Dialect为 MariaDB 10.3使用更新鲜的内容,但它无法正常工作。

回答by MariuszS

You need to set MYSQLmode on h2 and disable replacing of datasource url for embedded database:

您需要MYSQL在 h2上设置模式并禁用替换嵌入式数据库的数据源 url:

Modify application-test.yaml

调整 application-test.yaml

spring:
  datasource:
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL  
  test:
    database:
      replace: NONE