• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

play-postgresql: PostgreSQL数据库在 Play Framework 下的测试

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

play-postgresql

开源软件地址:

https://gitee.com/tanggaowei/play-postgresql

开源软件介绍:

使用 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:testdb.default.driver=org.postgresql.Driverdb.default.user=postgresdb.default.pass=tgw@123jpa.default.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect

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

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

-- Enable PostGIS (includes raster)CREATE EXTENSION postgis;-- Enable TopologyCREATE EXTENSION postgis_topology;-- Enable PostGIS Advanced 3D -- and other geoprocessing algorithms-- sfcgal not available with all distributionsCREATE EXTENSION postgis_sfcgal;-- fuzzy matching needed for TigerCREATE EXTENSION fuzzystrmatch;-- rule based standardizerCREATE EXTENSION address_standardizer;-- example rule data setCREATE EXTENSION address_standardizer_data_us;  -- Enable US Tiger GeocoderCREATE 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.*/@MappedSuperclasspublic 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; @Entitypublic 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");    } }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap