2 Star 4 Fork 0

知莫问 / play-postgresql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

使用 PostgreSQL 可以快速查询某一点到数据表存在的其它点之间的距离,并且能按距离远近进行顺序。

当我们要查询附近的人或物时,这个非常实用!

1、环境

  • Play Framework 1.4.3
  • PostgreSQL 9.6.2
  • PostGIS 2.3
  • Java 7

2、依赖

  • postgresql-9.4.1212.jre7.jar
  • hibernate-spatial-4.0.1.jar
  • jts-1.13.jar
  • postgis-jdbc-2.1.7.jar

这 4 个 jar 包可以从百度云下载:http://pan.baidu.com/s/1dEU7x9B

3、application.conf 数据库连接配置

db.default.url=jdbc:postgresql:test
db.default.driver=org.postgresql.Driver
db.default.user=postgres
db.default.pass=tgw@123
jpa.default.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect

4、给需要使用的数据库添加 PostGIS 扩展

在对应的数据库上执行下列查询语句:

-- Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
-- Enable Topology
CREATE EXTENSION postgis_topology;
-- Enable PostGIS Advanced 3D 
-- and other geoprocessing algorithms
-- sfcgal not available with all distributions
CREATE EXTENSION postgis_sfcgal;
-- fuzzy matching needed for Tiger
CREATE EXTENSION fuzzystrmatch;
-- rule based standardizer
CREATE EXTENSION address_standardizer;
-- example rule data set
CREATE EXTENSION address_standardizer_data_us;  
-- Enable US Tiger Geocoder
CREATE EXTENSION postgis_tiger_geocoder;

提示:如果不给数据库添加 PostGIS 扩展,Model 里面的 geometry(Point,4326) 配置将会报错!

5、使用

BaseModel.java - 配置 PostgreSQL 数据库的自增 ID:

package models;
 
import play.db.jpa.GenericModel; 
import javax.persistence.*;
 
/**
* Created by TangGaowei on 2016/11/15.
*/
@MappedSuperclass
public class BaseModel extends GenericModel {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;
 
    public Long getId() {
        return id;
    }
 
    @Override
    public Object _key() {
        return getId();
    }
}

UserModel.java - Model 中 PostGIS 地理字段的定义方法:

package models;
 
import javax.persistence.*;
import org.hibernate.annotations.Type;
import com.vividsolutions.jts.geom.Point;
 
@Entity
public class UserModel extends BaseModel {
    public String name; // 姓名
 
    @Column(columnDefinition = "geometry(Point,4326)")
    @Type(type = "org.hibernate.spatial.GeometryType")
    public Point location;; // 所在位置
 
    public Integer state; // 状态
}

Application.java - Model 的新建与修改:

package controllers;
 
import com.vividsolutions.jts.geom.*;
import org.hibernate.spatial.jts.mgeom.MCoordinateSequence;
import org.hibernate.spatial.jts.mgeom.MGeometryFactory;
import play.mvc.*;
import models.*;
 
public class Application extends Controller {
 
    public static void index() {
        UserModel user = UserModel.find("name=?", "test").first();
        if(user == null){
            user = new UserModel();
            user.name = "test";
            user.save();
        }
        else{
            // 修改地理位置字段
            MGeometryFactory mgf = new MGeometryFactory(new PrecisionModel(), 4326);
            Coordinate coord = new Coordinate( 32.581112, 124.381802 );
            MCoordinateSequence coordinates = new MCoordinateSequence(new Coordinate[]{coord});
            Point location =  mgf.createPoint(coordinates);
            user.location = location;
            user.save();
        }
        renderText("hello");
    }
 
}

空文件

简介

PostgreSQL数据库在 Play Framework 下的测试 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/tanggaowei/play-postgresql.git
git@gitee.com:tanggaowei/play-postgresql.git
tanggaowei
play-postgresql
play-postgresql
master

搜索帮助