版本
需要MySQL8以上
CREATE TABLE `lezudev`.`tb_city` (
`id` bigint NOT NULL COMMENT 'ID',
`name` varchar(255) NULL COMMENT '名称',
`location` point NOT NULL COMMENT '中心经纬度',
`code` varchar(255) NULL COMMENT '行政码',
`level` int NULL COMMENT '类型:1-省份、2-城市',
PRIMARY KEY (`id`),
SPATIAL INDEX `idx_location`(`location`) COMMENT '经纬度'
) COMMENT = '行政区域';
重点:point类型,不能为空,如果有空间查询需要加上索引
Java操作
保存数据
<!-- 插入城市数据(使用 ST_GeomFromText 转换 Geometry) -->
<insert id="insertCity" parameterType="com.project.entity.TbCity">
INSERT INTO tb_city (id,
name,
parent_id,
location,
code,
level,
remark,
deleted,
create_time,
create_user,
update_time,
update_user)
VALUES (#{id},
#{name},
#{parentId},
ST_GeomFromText(#{location}),
#{code},
#{level},
#{remark},
#{deleted},
#{createTime},
#{createUser},
#{updateTime},
#{updateUser})
</insert>
获取数据自动转
/**
* 中心经纬度
*/
@TableField(typeHandler = PointStringTypeHandler.class)
private String location;