spring在使用h2方言进行测试时正在设置mysql方言

zi8p0yeb  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(582)

我在Spring做一些测试,我看到一件奇怪的事。
当我在 application.properties 这样地

spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

使用 @DataJpaTest 为测试设置h2数据库,spring使用mysql方言代替h2方言。
这是一些控制台的线路

2018-08-22 14:42:53.881  INFO 852 --- [           main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2018-08-22 14:42:53.882  INFO 852 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2018-08-22 14:42:54.066  INFO 852 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:7ee61de9-3134-4b5e-93cb-02b1c11727ee;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2018-08-22 14:42:54.410  INFO 852 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-08-22 14:42:54.426  INFO 852 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [  name: default   ...]
2018-08-22 14:42:54.479  INFO 852 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.17.Final}
2018-08-22 14:42:54.480  INFO 852 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-08-22 14:42:54.510  INFO 852 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-08-22 14:42:54.605  INFO 852 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect

我看到了 H2EmbeddedDatabaseConfigurer.java spring没有为h2数据库设置方言。
测试通过了,没有错误,但就像h2被自动置于mysql模式一样。
与derby和hsql数据库相同。有人能解释为什么会这样吗?

nkhmeac6

nkhmeac61#

我发现绕过这个问题的唯一方法就是创建 application.yml/src/test/resources/ 手动配置h2:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
    username: sa
    password: sa

相关问题