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());
        };
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值