Mybatis-plus如何保存geometry类型的数据

使用的是mybatis-plus,mysql数据库,数据库中使用的类型是point。保存数据总是插入不进去。

【以下回答由 GPT 生成】

我理解您的问题是如何使用Mybatis-plus保存MySQL数据库中的geometry类型(point类型)的数据。以下是一个解决方案的步骤:

  1. 确保您的数据库表中有一个geometry类型的列来存储point数据。您可以使用如下的方式创建表:
CREATE TABLE your_table_name (
    id INT PRIMARY KEY AUTO_INCREMENT,
    location GEOMETRY
);
  1. 确保您的Java实体类中有一个与数据库表对应的属性。请确保该属性的类型为com.vividsolutions.jts.geom.Point,这是Mybatis-plus默认支持的geometry类型。
import com.vividsolutions.jts.geom.Point;

public class YourEntity {
    private Long id;
    private Point location;

    // Getters and setters...
}
  1. 在您的Mybatis-plus配置文件中,添加对geometry类型的支持。在MySQL数据库中,geometry类型需要使用JTS库进行解析和处理。您可以像下面这样配置Mybatis-plus的typeHandlers和typeAliases:
<!-- typeHandlers -->
<typeHandlers>
    <typeHandler handler="com.vividsolutions.jts.spatialtysql.mysql.MySQLGeometryTypeHandler"/>
</typeHandlers>

<!-- typeAliases -->
<typeAliases>
    <typeAlias alias="point" type="com.vividsolutions.jts.geom.Point"/>
</typeAliases>
  1. 更新您的Mapper接口,使用@Insert注解来保存数据。确保在插入数据时,正确地传递point类型的值。
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import com.vividsolutions.jts.geom.Point;

public interface YourMapper extends BaseMapper<YourEntity> {
    @Insert("INSERT INTO your_table_name (location) VALUES(#{location, typeHandler=com.vividsolutions.jts.spatialtysql.mysql.MySQLGeometryTypeHandler})")
    void insertWithGeometry(YourEntity entity);
}
  1. 最后,在您的业务逻辑代码中调用insertWithGeometry方法来保存数据。
YourEntity entity = new YourEntity();
entity.setLocation(point); // 请使用合适的point对象初始化

yourMapper.insertWithGeometry(entity);

这就是一个使用Mybatis-plus保存MySQL数据库中geometry类型的数据的解决方案。希望对您有帮助!

请注意,以上的解决方案假设您已经正确导入了Mybatis-plus和JTS库,并且在Mybatis-plus配置文件中正确配置了数据源和Mapper接口。如果在实际使用中遇到了问题,请仔细检查您的配置和依赖项是否正确。如果您已经尝试上述方法但仍然无法成功,请告诉我您遇到的错误提示,我将尽力帮助您解决问题。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

前提:确保你已经添加了 MyBatis-Plus 的依赖,以及相应的数据库驱动和 Geometry 类型的依赖

实体类写法

 // 使用 @TableField 注解指定字段类型为 Geometry,并定义数据库字段的数据类型
    @TableField(columnDefinition = "geometry")
    private Point location;

yml配置增加

mybatis-plus:
  type-aliases-package: com.yourpackage.entity

调用

// 保存 Geometry 数据
YourEntity entity = new YourEntity();
entity.setLocation(new GeometryFactory().createPoint(new Coordinate(1, 2)));
yourEntityMapper.insert(entity);

// 查询 Geometry 数据
YourEntity result = yourEntityMapper.selectById(1);
Point location = result.getLocation();

这篇文章好像可以解决你的问题,可以看下:https://blog.csdn.net/zhangkaixuan456/article/details/121235428

参考这篇文章,写一个typehandler就可以了:https://blog.csdn.net/u010412088/article/details/126509984

这个问题,可否提供详细信息

mysql+mybatis-plus+springboot使用代码生成器,生成point数据类型的,需要修改Mapper和实体类_mybatisplus操作mysql的point类型_爱北的琳儿的博客-CSDN博客 mysql表:字段:position_point, 类型:point生成的实体类:生成的Mapper文件这里发现生成的是Integer类型,所以需要修改实体类,以及对应的Mapper文件修改后的实体类注意:我这里用到的PointTypeHandler.class的包是:cn.changeforyou.location.mybatis.PointTypeHandler;修改后的Mapper文件修改完毕。..._mybatisplus操作mysql的point类型 https://blog.csdn.net/qq_42991839/article/details/124704579?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169163165116800222856202%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=169163165116800222856202&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~times_rank-9-124704579-null-null.142^v92^chatsearchT3_1&utm_term=mybatis-plus%E6%8F%92%E5%85%A5point&spm=1018.2226.3001.4187

MyBatis-Plus配置: 在MyBatis-Plus的配置文件中,你需要为POINT类型设置正确的类型处理器,以确保正确的数据转换。可以使用MySqlTypeHandler或者自定义的类型处理器。

import com.baomidou.mybatisplus.extension.handlers.MybatisTypeHandler;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
            configuration.getTypeHandlerRegistry().register(Point2D.class, MybatisTypeHandler.class);
        };
    }
}