java如何配置springboot项目以使用inmemory空间数据库进行测试?
这是我现在的配置。我想使用hibernate spatial在生产中使用postgis
spring:
profiles: production
datasource:
platform: postgres
url: jdbc:postgresql://192.168.99.100:5432/dragon
username: dragon
password: dragon
database:
driverClassName: org.postgresql.Driver
jpa:
database: POSTGRESQL
database-platform: org.hibernate.spatial.dialect.postgis.PostgisDialect
show-sql: true
hibernate:
ddl-auto: update
---
spring:
profiles: development
datasource: SpatialInMemoryDb
jpa:
database-platform: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
hibernate:
ddl-auto: create-drop
对于测试,所有发现都是h2gis项目
public class SpatialInMemoryDb extends SingleConnectionDataSource{
public SpatialInMemoryDb() {
setDriverClassName("org.h2.Driver");
setUrl("jdbc:g2:mem:test");
setSuppressClose(true);
}
@Override
public Connection getConnection() throws SQLException {
System.out.println("************");
Connection connection = super.getConnection();
try (Statement st = connection.createStatement()) {
// Import spatial functions, domains and drivers
// If you are using a file database, you have to do only that once.
CreateSpatialExtension.initSpatialExtension(connection);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
不确定它是否能与geodbdialect或postgisdialect一起使用,尽管它似乎非常接近postgisdialect
无论如何,有人能推荐一些简单的解决方案吗
# 1 楼答案
为了让其他任何想让这一切顺利进行的人更容易,@Mateusz Stefek answer是正确的方法。以下是确保postgis与hibernate模型和h2 db配合使用以进行单元测试用例所需的全部内容。请注意,下面的说明不适用于hibernate 4,因此最好升级到版本5。请注意,在hibernate 5中,改进的命名策略不再起作用。如果您逐渐退出,您可以看看其他stackoverflow解决方案:ImprovedNamingStrategy no longer working in Hibernate 5
确保您具有以下依赖项
hibernate spatial+h2gis的maven repos
maven依赖项
Hibernate-JPA模型
下面确保了当我们调用RESTAPI端点时,spring boot可以从postgres序列化我们的postgis几何数据
如果您使用flyway,您可以使它在测试脚本中运行,以确保在h2 db上执行以下内容
您的测试应用程序。属性文件
你的飞行路线初始的内容。sql脚本
确保您的测试应用程序。属性文件hibernate拨号指向GeoDBDialect
# 2 楼答案
GeoDBDialect与h2gis库的结合在H2中效果良好。我可以毫无问题地存储和加载
com.vividsolutions.jts.geom.Polygon
我正在使用Hibernate 5.2+
org.hibernate:hibernate-spatial:1.2.4
冬眠方言:
org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
列类型:
geometry
H2数据库应按照h2gis文档(https://github.com/orbisgis/h2gis)中所述进行初始化。这些应该是初始化数据库时的第一个sql之一
(
H2GISFunctions
应该在类路径上。)