我正在进行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
如果您需要为测试设置H2数据库,请使用@DataJpaTest
,但是需要注意的是,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
我看到Spring的
H2EmbeddedDatabaseConfigurer.java
没有为H2数据库设置方言。测试通过,但好像H2自动进入了mysql模式。
Derby和HSQL数据库也是一样。有人能解释一下这是为什么吗?