JTS:05 MyBatis 数据转换类TypeHandler

版本

org.locationtech.jts:jts-core:1.19.0
链接: github

代码

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * JTS.Geometry 转换 
 * @author Lihan
 * @since 2021年11月15日 10:48:55
 */
@MappedTypes(value = {Geometry.class})
public class JTSGeometryTypeHandler extends BaseTypeHandler<Geometry> {
	
	private final WKBReader wkbReader = new WKBReader();
	private final WKBWriter wkbWriter = new WKBWriter(2, 1, true);

	
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Geometry parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setBytes(i, wkbWriter.write(parameter));
	}

	@Override
	public Geometry getNullableResult(ResultSet rs, String columnName) throws SQLException {
		try {
			return wkbReader.read(WKBReader.hexToBytes(rs.getString(columnName)));
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public Geometry getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		try {
			return wkbReader.read(WKBReader.hexToBytes(rs.getString(columnIndex)));
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public Geometry getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		try {
			return wkbReader.read(WKBReader.hexToBytes(cs.getString(columnIndex)));
		} catch (Exception e) {
			return null;
		}
	}
}

Mybatis-Plus 配置

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
        	TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            typeHandlerRegistry.register(PGgeometry.class, new GeometryTypeHandler());
            typeHandlerRegistry.register(Geometry.class, new JTSGeometryTypeHandler());
        };
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中对于几何(Geometry)型的转换,可以通过使用 TypeHandler 来实现。TypeHandlerMyBatis 提供的一个接口,用于将 Java 对象和数据库中的数据进行转换。 对于几何型的转换,你可以自定义一个实现了 TypeHandler 接口的,然后在 MyBatis 的配置文件中进行注册。 以下是一个示例的 TypeHandler 实现: ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.io.WKTReader; public class GeometryTypeHandler extends BaseTypeHandler<Geometry> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Geometry parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter.toText()); } @Override public Geometry getNullableResult(ResultSet rs, String columnName) throws SQLException { String geometryString = rs.getString(columnName); return parseGeometry(geometryString); } @Override public Geometry getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String geometryString = rs.getString(columnIndex); return parseGeometry(geometryString); } @Override public Geometry getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String geometryString = cs.getString(columnIndex); return parseGeometry(geometryString); } private Geometry parseGeometry(String geometryString) { if (geometryString != null) { WKTReader reader = new WKTReader(); try { return reader.read(geometryString); } catch (Exception e) { // Handle exception } } return null; } } ``` 在以上的示例代码中,我们使用了 JTS Topology Suite(JTS)来处理几何型。在 `setNonNullParameter` 方法中,我们将 Geometry 对象转换为 WKT(Well-Known Text)格式的字符串,并设置到 PreparedStatement 中。在 `getNullableResult` 方法中,我们从 ResultSet 或 CallableStatement 中取出字符串,并将其解析为 Geometry 对象。 接下来,在 MyBatis 的配置文件中注册这个 TypeHandler: ```xml <typeHandlers> <typeHandler handler="com.example.GeometryTypeHandler"/> </typeHandlers> ``` 需要注意的是,你需要根据你自己的数据库和几何型库选择合适的依赖和实现方式。以上示例代码仅供参考,具体实现可能会有所差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值