如何配置spring-boot项目以与内存空间数据库一起进行测试? [英] How to configure spring-boot project to work with inmemory spatial database for tests?
问题描述
这是我的配置.我想使用休眠空间在生产中使用Postgis.
Here is my config now. I want to use hibernate spatial to work with postgis in production.
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项目.
For tests all found is h2gis project.
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非常接近.
Not sure that it will work with geodbdialect or postgisdialect, altough it seems very close to postgisdialect.
无论如何,有人可以推荐一些简单的解决方案吗?
Anyway can someone recommend some easy solution?
推荐答案
将GeoDBDialect与h2gis库结合在H2中效果很好.我可以毫无问题地存储和加载com.vividsolutions.jts.geom.Polygon
.
Combining GeoDBDialect with h2gis library works fine in H2. I can store and load com.vividsolutions.jts.geom.Polygon
with no problem.
我正在使用Hibernate 5.2 + org.hibernate:hibernate-spatial:1.2.4
I'm using 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之一.
H2 database should be initialized as described in the h2gis documentation (https://github.com/orbisgis/h2gis). These should be one of the first sql, when you initialize the database.
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();
(H2GISFunctions
应该在类路径上.)
这篇关于如何配置spring-boot项目以与内存空间数据库一起进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!