当前位置: 首页 > news >正文

wordpress插件dx seo下载怎么seo关键词优化排名

wordpress插件dx seo下载,怎么seo关键词优化排名,做视频网站收费侵权吗,大连电商平台有哪些前言 最近做了一个功能,需要通过用户当前位置点获取指定范围内的数据。由于后端存储用的是 MySQL,故选择使用 MySQL 中的 Point 实现范围查找功能。ORM 框架用的是 MyBatis,MyBatis 原生并不支持 Point 字段与 POJO 的映射,需要自…

前言

最近做了一个功能,需要通过用户当前位置点获取指定范围内的数据。由于后端存储用的是 MySQL,故选择使用 MySQL 中的 Point 实现范围查找功能。ORM 框架用的是 MyBatis,MyBatis 原生并不支持 Point 字段与 POJO 的映射,需要自定义 MyBatis 的 TypeHandler 实现该功能。

当然,你可以通过定义两个 MySQL 字段(经度和维度)来代替 Point 也可以实现范围查找,但是既然是使用的 MyBatis,那么还是希望能在 MyBatis 中直接操作 Point,提高代码通用性。

关于 MySQL 的 POINT

在MySQL中,POINT 是一种用于存储地理空间数据的数据类型,它表示二维空间中的一个点。MySQL 从 5.7 版本开始,提供了对地理空间数据类型的原生支持,包括 POINT、LINESTRING、POLYGON 等。

POINT 数据类型用于存储一个二维坐标点,其格式为 (X, Y),其中 X 和 Y 分别表示该点在二维平面上的横坐标和纵坐标。

注意,在用 POINT 存储经纬度时,X 为经度,Y 为纬度,不要弄反了。因为将经纬度存储到 POINT 时并没有循序限制,但是使用 POINT 相关函数时就有限制了。比如ST_Distance_Sphere

组件版本

  • SpringBoot 2.4.3
  • MyBatis-Plus 3.4.2
  • MySQL 8.0.26

建表(含 POINT 字段)

create table group_ride_info
(id             bigint unsigned                            not null comment '主键id'primary key,create_time    datetime                                   not null comment '创建时间',update_time    datetime         default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP comment '修改时间',create_by      int unsigned     default '0'               not null comment '创建人',update_by      int unsigned     default '0'               not null comment '修改人',is_delete      tinyint unsigned default '0'               not null comment '是否删除。默认0,1-是,0-否',...create_point   point                                      not null comment '创建时坐标'
)comment '团信息表';create spatial index create_pointon group_ride_info (create_point);

其中,create_point 是通过 POINT 字段记录的经纬度坐标,POINT 字段建议设置为为空。同时需要给 PIOINT 类型字段创建空间索引。

alter table group_ride_info add SPATIAL index(create_point);

定义 GeoPoint 对象

@Builder
@AllArgsConstructor
@Data
public class GeoPoint implements Serializable {/*** 经度*/private Double longitude;/*** 纬度*/private Double latitude;
}

定义表对象

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("group_ride_info")
@ApiModel(value="GroupRideInfo对象", description="团信息表")
public class GroupRideInfo implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "主键id")@TableId(value = "id", type = IdType.NONE)private Long id;@ApiModelProperty(value = "创建时间")private LocalDateTime createTime;@ApiModelProperty(value = "修改时间")private LocalDateTime updateTime;@ApiModelProperty(value = "创建人")private Integer createBy;@ApiModelProperty(value = "修改人")private Integer updateBy;@ApiModelProperty(value = "是否删除。默认0,1-是,0-否")private Integer isDelete;...@ApiModelProperty(value = "创建时坐标")private GeoPoint createPoint;}

定义坐标转换器 GeoPointConverter

public class GeoPointConverter {/*** Little endian or Big endian*/private int byteOrder = ByteOrderValues.LITTLE_ENDIAN;/*** Precision model*/private PrecisionModel precisionModel = new PrecisionModel();/*** Coordinate sequence factory*/private CoordinateSequenceFactory coordinateSequenceFactory = CoordinateArraySequenceFactory.instance();/*** Output dimension*/private int outputDimension = 2;/*** Convert byte array containing SRID + WKB Geometry into Geometry object*/public GeoPoint from(byte[] bytes) {if (bytes == null) {return null;}try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {// Read SRIDbyte[] sridBytes = new byte[4];inputStream.read(sridBytes);int srid = ByteOrderValues.getInt(sridBytes, byteOrder);// Prepare Geometry factoryGeometryFactory geometryFactory = new GeometryFactory(precisionModel, srid, coordinateSequenceFactory);// Read GeometryWKBReader wkbReader = new WKBReader(geometryFactory);Geometry geometry = wkbReader.read(new InputStreamInStream(inputStream));Point point = (Point) geometry;// convert to GeoPointGeoPoint geoPoint = new GeoPoint(point.getX(), point.getY());return geoPoint;} catch (IOException | ParseException e) {throw new IllegalArgumentException(e);}}/*** Convert Geometry object into byte array containing SRID + WKB Geometry*/public byte[] to(GeoPoint geoPoint) {if (geoPoint == null) {return null;}Coordinate coordinate = new Coordinate(geoPoint.getLongitude(), geoPoint.getLatitude());CoordinateArraySequence coordinateArraySequence = new CoordinateArraySequence(new Coordinate[]{coordinate}, 2);Point point = new Point(coordinateArraySequence, new GeometryFactory());try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {// Write SRIDbyte[] sridBytes = new byte[4];ByteOrderValues.putInt(point.getSRID(), sridBytes, byteOrder);outputStream.write(sridBytes);// Write GeometryWKBWriter wkbWriter = new WKBWriter(outputDimension, byteOrder);wkbWriter.write(point, new OutputStreamOutStream(outputStream));return outputStream.toByteArray();} catch (IOException ioe) {throw new IllegalArgumentException(ioe);}}
}

定义 GeoPointTypeHandler

@MappedTypes({GeoPoint.class})
public class GeoPointTypeHandler extends BaseTypeHandler<GeoPoint> {GeoPointConverter converter = new GeoPointConverter();@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, GeoPoint parameter, JdbcType jdbcType) throws SQLException {ps.setBytes(i, converter.to(parameter));}@Overridepublic GeoPoint getNullableResult(ResultSet rs, String columnName) throws SQLException {return converter.from(rs.getBytes(columnName));}@Overridepublic GeoPoint getNullableResult(ResultSet rs, int columnIndex) throws SQLException {return converter.from(rs.getBytes(columnIndex));}@Overridepublic GeoPoint getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {return converter.from(cs.getBytes(columnIndex));}
}

配置扫描 TypeHandler

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"${spring.xxx.data.db.basepackage}"}, sqlSessionFactoryRef = "sqlSessionFactoryMasterDb")
@Slf4j
public class DbConfig implements TransactionManagementConfigurer {/*** 注释** @return SqlSessionFactory* @throws Exception 异常*/@Beanpublic SqlSessionFactory sqlSessionFactoryMasterDb() throws Exception {MybatisSqlSessionFactoryBean factoryBean =new MybatisSqlSessionFactoryBean();...// 此处为定义TypeHandler所在的包名factoryBean.setTypeHandlersPackage("com.xxx.module.typehandler");return factoryBean.getObject();}
}

注意,此处代码仅为示例代码,关键代码在factoryBean.setTypeHandlersPackage("com.xxx.module.typehandler");

SpringBoot 项目也可以在配置文件中配置,请自行百度,目的是让自定义 TypeHandler 生效。

MyBatis 使用 POINT

原生 getById()

自定义 SQL(指定范围查找)

  1. Mapper 接口中定义方法
List<GroupRideInfo> getListByPoint2(Integer distance, GeoPoint point, String ticket);
  1. Mapper.xml 中定义查询语句
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.xxx.groupride.domain.po.GroupRideInfo"><id column="id" property="id" /><result column="create_time" property="createTime" /><result column="update_time" property="updateTime" /><result column="create_by" property="createBy" /><result column="update_by" property="updateBy" /><result column="is_delete" property="isDelete" />...<result column="create_point" property="createPoint" />
</resultMap>
<select id="getListByPoint2" resultMap="BaseResultMap">SELECT *,ST_Distance_Sphere(create_point, #{point}) AS distance_metersFROM group_ride_infoHAVING distance_meters &lt; #{distance} and status=1 and ticket=#{ticket}ORDER BY distance_meters asc
</select>
  1. 调用方法

可以看到,可以在 MyBatis 中像普通类型参数一样使用 POINT 了。上面示例仅列举了查询操作,新增/修改也是可以的。

http://www.hkea.cn/news/392991/

相关文章:

  • 江苏天德建设工程有限公司网站一个平台怎么推广
  • 石家庄做网络推广的网站推广平台收费标准
  • 贵阳天柱网站建设招聘域名注册平台有哪些
  • 网站建设电话营销百度问一问官网
  • 网站优化建设河南怎么关闭seo综合查询
  • 自贡做响应式网站开发公司google搜索引擎入口google
  • 东莞哪种网站推广好微信朋友圈推广文案
  • 现在学做网站赚钱吗东莞市优速网络科技有限公司
  • 宁津做网站公司宣传推广图片
  • 陕西的建设厅官方网站数据分析报告
  • 企业网站建设的定位互联网
  • 注册域名之后如何做网站优化清理大师
  • wordpress+在线播放推广seo网站
  • 丽水网站建设明恩玉杰网站开发框架
  • 如何设计网站中的上传功能搜索引擎技术基础
  • 余江区建设局网站百度搜索引擎优化的方法
  • 做网站用c 还是java万网域名注册教程
  • 青岛做网站那家好专业的网站优化公司排名
  • 网站如何做淘宝推广seo服务 收费
  • 学完js了可以做哪些网站营业推广的形式包括
  • 网站会员系统怎么做模版seo是指什么职位
  • 上海集团网站制作新闻 近期大事件
  • 商城网站验收标准seo关键词排名优化怎样收费
  • 睢宁做网站公司珠海百度关键字优化
  • 临安市住房和建设局网站伊春seo
  • 天津百度做网站多少钱游戏代理平台哪个好
  • b2b模式的网站google网站
  • 做优化网站哪个公司好十大营销策略
  • 软件商店app苏州网站关键词优化推广
  • wordpress添加日历首页优化公司