From 2d068611ad8fc2329be0d26ca085ebf16d7b175e Mon Sep 17 00:00:00 2001 From: "hukekuan@163.com" Date: Wed, 10 May 2017 19:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/gis3c/dao/PostGISDao.java | 8 ++ .../com/gis3c/dao/impl/PostGISDaoImpl.java | 29 ++++ src/main/java/com/gis3c/demo/Hello.java | 8 -- .../java/com/gis3c/demo/impl/HelloImpl.java | 26 ---- .../com/gis3c/postgis/PostGISHandler.java | 73 ++++++++++ .../com/gis3c/service/PostGISService.java | 8 ++ .../service/impl/PostGISServiceImpl.java | 29 ++++ .../java/com/gis3c/sqlmaps/PostGISMapper.xml | 131 ++++++++++++++++++ src/main/resources/resources.properties | 8 +- src/main/resources/spring-config.xml | 9 +- src/main/resources/sql-map-config-mybatis.xml | 5 +- 11 files changed, 291 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/gis3c/dao/PostGISDao.java create mode 100644 src/main/java/com/gis3c/dao/impl/PostGISDaoImpl.java delete mode 100644 src/main/java/com/gis3c/demo/Hello.java delete mode 100644 src/main/java/com/gis3c/demo/impl/HelloImpl.java create mode 100644 src/main/java/com/gis3c/postgis/PostGISHandler.java create mode 100644 src/main/java/com/gis3c/service/PostGISService.java create mode 100644 src/main/java/com/gis3c/service/impl/PostGISServiceImpl.java create mode 100644 src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml diff --git a/src/main/java/com/gis3c/dao/PostGISDao.java b/src/main/java/com/gis3c/dao/PostGISDao.java new file mode 100644 index 0000000..211f0d2 --- /dev/null +++ b/src/main/java/com/gis3c/dao/PostGISDao.java @@ -0,0 +1,8 @@ +package com.gis3c.dao; + +import java.util.Map; + +public interface PostGISDao { + public void CommonCreateTable(Map tableStructure); + public void CreateSpatialIndex(Map params); +} diff --git a/src/main/java/com/gis3c/dao/impl/PostGISDaoImpl.java b/src/main/java/com/gis3c/dao/impl/PostGISDaoImpl.java new file mode 100644 index 0000000..af2b7c5 --- /dev/null +++ b/src/main/java/com/gis3c/dao/impl/PostGISDaoImpl.java @@ -0,0 +1,29 @@ +package com.gis3c.dao.impl; + +import com.gis3c.dao.PostGISDao; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.support.SqlSessionDaoSupport; +import org.springframework.stereotype.Repository; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.Map; + +@Repository +public class PostGISDaoImpl extends SqlSessionDaoSupport implements PostGISDao { + @Resource(name="sqlSessionFactory_GIS") + private SqlSessionFactory sqlSessionFactory; + @PostConstruct + public void injectSessionFactory(){ + super.setSqlSessionFactory(sqlSessionFactory); + } + + @Override + public void CommonCreateTable(Map tableStructure){ + getSqlSession().update("PostGISSql.commonCreateTable",tableStructure); + } + @Override + public void CreateSpatialIndex(String tableName){ + getSqlSession().update("PostGISSql.createSpatialIndex",tableName); + } +} diff --git a/src/main/java/com/gis3c/demo/Hello.java b/src/main/java/com/gis3c/demo/Hello.java deleted file mode 100644 index 5211bdf..0000000 --- a/src/main/java/com/gis3c/demo/Hello.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gis3c.demo; - -/** - * Created by Administrator on 2016/12/27. - */ -public interface Hello { - public String SayHello(); -} diff --git a/src/main/java/com/gis3c/demo/impl/HelloImpl.java b/src/main/java/com/gis3c/demo/impl/HelloImpl.java deleted file mode 100644 index 750d2f1..0000000 --- a/src/main/java/com/gis3c/demo/impl/HelloImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.gis3c.demo.impl; -import com.gis3c.demo.Hello; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component("hello") -public class HelloImpl implements Hello{ - private String name; - - @Override - public String SayHello(){ - return getName(); - } - - public String getName() { - return name; - } - - @Autowired - public void setName( -// @Value(value = "#{configProperties['gis.name']}") - @Value(value = "HUKEKUAN") String name) { - this.name = name; - } -} diff --git a/src/main/java/com/gis3c/postgis/PostGISHandler.java b/src/main/java/com/gis3c/postgis/PostGISHandler.java new file mode 100644 index 0000000..958a3eb --- /dev/null +++ b/src/main/java/com/gis3c/postgis/PostGISHandler.java @@ -0,0 +1,73 @@ +package com.gis3c.postgis; + +import java.io.IOException; +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedTypes; +import org.apache.ibatis.type.TypeHandler; +import org.geotools.data.postgis.WKBAttributeIO; + +import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.GeometryCollection; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.MultiLineString; +import com.vividsolutions.jts.geom.MultiPoint; +import com.vividsolutions.jts.geom.MultiPolygon; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.Polygon; +import com.vividsolutions.jts.io.ParseException; +import com.vividsolutions.jts.io.WKBReader; + +@MappedTypes({ Point.class, LineString.class, Polygon.class, MultiPoint.class, MultiLineString.class, MultiPolygon.class, + Envelope.class,GeometryCollection.class,Geometry.class }) +public class PostGISHandler implements TypeHandler { + private static WKBReader wKBReader = new WKBReader(); + private static WKBAttributeIO wKBAttributeIO = new WKBAttributeIO(); + @Override + public void setParameter(PreparedStatement ps, int i, Geometry parameter,JdbcType jdbcType) + throws SQLException { + try { + wKBAttributeIO.write(ps, i, parameter); + } catch (IOException e) { + throw new SQLException(e.getMessage()); + } + } + + @Override + public Geometry getResult(ResultSet rs, String columnName)throws SQLException { + Geometry resultGeometry = null; + try { + resultGeometry = wKBReader.read(WKBReader.hexToBytes(rs.getObject(columnName).toString())); + } catch (ParseException e) { + throw new SQLException(e.getMessage()); + } + return resultGeometry; + } + + @Override + public Geometry getResult(ResultSet rs, int columnIndex)throws SQLException { + Geometry resultGeometry = null; + try { + resultGeometry = wKBReader.read(WKBReader.hexToBytes(rs.getObject(columnIndex).toString())); + } catch (ParseException e) { + throw new SQLException(e.getMessage()); + } + return resultGeometry; + } + + @Override + public Geometry getResult(CallableStatement cs, int columnIndex)throws SQLException { + Geometry resultGeometry = null; + try { + resultGeometry = wKBReader.read(WKBReader.hexToBytes(cs.getObject(columnIndex).toString())); + } catch (ParseException e) { + throw new SQLException(e.getMessage()); + } + return resultGeometry; + } +} diff --git a/src/main/java/com/gis3c/service/PostGISService.java b/src/main/java/com/gis3c/service/PostGISService.java new file mode 100644 index 0000000..79fd1d9 --- /dev/null +++ b/src/main/java/com/gis3c/service/PostGISService.java @@ -0,0 +1,8 @@ +package com.gis3c.service; + +import java.util.List; +import java.util.Map; + +public interface PostGISService { + public void CommonCreateTable(String tableName,String geometryColumn,List> columns); +} diff --git a/src/main/java/com/gis3c/service/impl/PostGISServiceImpl.java b/src/main/java/com/gis3c/service/impl/PostGISServiceImpl.java new file mode 100644 index 0000000..12b646c --- /dev/null +++ b/src/main/java/com/gis3c/service/impl/PostGISServiceImpl.java @@ -0,0 +1,29 @@ +package com.gis3c.service.impl; + +import com.gis3c.dao.PostGISDao; +import com.gis3c.service.PostGISService; +import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service("postGISService") +public class PostGISServiceImpl implements PostGISService { + @Resource private PostGISDao postGISDao; + + public void CommonCreateTable( + String tableName,String geometryColumn,List> columns){ + Map tableStructure = new HashMap<>(); + tableStructure.put("tableName",tableName); + tableStructure.put("columnList",columns); + + Map SpatialIndexParam = new HashMap<>(); + SpatialIndexParam.put("spatialIndexName","sidx_" + tableName + "_" + geometryColumn); + SpatialIndexParam.put("tableName",tableName); + SpatialIndexParam.put("geometryColumn",geometryColumn); + + postGISDao.CommonCreateTable(tableStructure); + postGISDao.CreateSpatialIndex(SpatialIndexParam); + } +} diff --git a/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml b/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml new file mode 100644 index 0000000..a5f6ada --- /dev/null +++ b/src/main/java/com/gis3c/sqlmaps/PostGISMapper.xml @@ -0,0 +1,131 @@ + + + + + + + + + CREATE TABLE ${tableName} + + + + ${item.columnName} numeric + + + ${item.columnName} NUMBER + + + ${item.columnName} character varying(254) + + + geo geometry(POINT,4326) + + + geo geometry(MULTIPOINT,4326) + + + geo geometry(LINESTRING,4326) + + + geo geometry(MULTILINESTRING,4326) + + + geo geometry(POLYGON,4326) + + + geo geometry(MULTIPOLYGON,4326) + + + + ,CONSTRAINT fid_pkey PRIMARY KEY (fid)); + + + + + CREATE INDEX ${spatialIndexName} + ON ${tableName} + USING gist + ($(geometryColumn)); + + + + + + + + drop table ${_parameter}; + + + + + insert into ${tableName} + + ${item} + + + + + + #{featureValue.value,jdbcType=INTEGER} + + + st_geomfromText(#{featureValue.value,jdbcType=VARCHAR},4326) + + + #{featureValue.value,jdbcType=VARCHAR} + + + + + + + + insert into ${tableName} + + ${item} + + + + + #{featureValue.value,jdbcType=INTEGER} + + + st_geomfromText(#{featureValue.value,jdbcType=VARCHAR},4326) + + + #{featureValue.value,jdbcType=VARCHAR} + + + + + + + + + + + delete from ${_parameter} + + + + \ No newline at end of file diff --git a/src/main/resources/resources.properties b/src/main/resources/resources.properties index 171428a..aa53626 100644 --- a/src/main/resources/resources.properties +++ b/src/main/resources/resources.properties @@ -1,4 +1,10 @@ gis.driverClassName=oracle.jdbc.driver.OracleDriver gis.url=jdbc:oracle:thin:@172.16.6.13:1521:orcl gis.username=spatial -gis.password=spatial \ No newline at end of file +gis.password=spatial + + +postgis.driverClassName=org.postgresql.Driver +postgis.url=jdbc:postgresql://localhost:5432/gisdb +postgis.username=postgres +postgis.password=postgres \ No newline at end of file diff --git a/src/main/resources/spring-config.xml b/src/main/resources/spring-config.xml index 33ef925..d40846c 100644 --- a/src/main/resources/spring-config.xml +++ b/src/main/resources/spring-config.xml @@ -29,11 +29,10 @@ - - - - - + + + + diff --git a/src/main/resources/sql-map-config-mybatis.xml b/src/main/resources/sql-map-config-mybatis.xml index f9e0057..1887544 100644 --- a/src/main/resources/sql-map-config-mybatis.xml +++ b/src/main/resources/sql-map-config-mybatis.xml @@ -5,11 +5,10 @@ - + + \ No newline at end of file