Springboot 使用 BaseTypeHandler 与 TypeHandler 记录

1.首先TypeHandler 是个啥,有啥用?

        TypeHandler是 springboot 在使用Mybatis插件的时候,有需要将Java对象和数据库JDBC数据类型之间进行转换。

 根据上图,可看到抽象类BaseTypeHandler,实现了TypeHandler并进行了扩展。

2.如何使用

  直接继承BaseTypeHandler,主要将库表中的 type 类型转成java 中的Geometry 类型,库表geom字段保存为WKB 类型。

 自定义转换类:

@Slf4j
@MappedTypes(Geometry.class)
@MappedJdbcTypes(value = JdbcType.OTHER, includeNullJdbcType = true)
public class TypeHandler extends BaseTypeHandler<Geometry> {
    private static WKBReader wkbReader;
    private static WKBWriter wkbWriter;

    static {
        wkbReader = new WKBReader();
        wkbWriter = new WKBWriter(3, true);
    }

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

    @Override
    public Geometry getNullableResult(ResultSet rs, String columnName) throws SQLException {
        try {
            return wkbReader.read(rs.getBytes(columnName));
        } catch (ParseException e) {
            log.error("【GeometryTypeHandler】对象转换异常", e);
            return null;
        }
    }

    @Override
    public Geometry getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        try {
            return wkbReader.read(rs.getBytes(columnIndex));
        } catch (ParseException e) {
            log.error("【GeometryTypeHandler】对象转换异常", e);
            return null;
        }
    }

    @Override
    public Geometry getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        try {
            return wkbReader.read(cs.getBytes(columnIndex));
        } catch (ParseException e) {
            log.error("【GeometryTypeHandler】对象转换异常", e);
            return null;
        }
    }

3.xml中使用typeHandler引用转换,或者在yml文件中使用配置。

#查询: 
<resultMap id="BaseResultMap" type="com.xxx.check.model.CheckReport">

        <id column="rep_id" jdbcType="INTEGER" property="repId"/>
        <result column="geom" property="geom" jdbcType="OTHER"
                typeHandler="com.xxx.check.service.typehandler.TypeHandler"/>

............

</resultMap>
#插入
<insert id="insert" parameterType="com.ecarx.check.model.CheckReport">
        insert into ics_check_report (rep_id, geom)
        values (
        #{repId,jdbcType=INTEGER}, 
        #{geom,jdbcType=OTHER,typeHandler=com.xxx.check.service.typehandler.TypeHandler}
    </insert>

或者在yml文件中使用配置:

Spring BootMyBatis Plus一起使用时,可以使用TypeHandler来处理数据库中的自定义类型和Java对象之间的转换。 首先,您需要创建一个自定义的TypeHandler类来处理特定类型的转换。例如,假设您有一个名为CustomType的自定义类型,您可以创建一个CustomTypeHandler类来处理它的转换。 ```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; public class CustomTypeHandler extends BaseTypeHandler<CustomType> { @Override public void setNonNullParameter(PreparedStatement preparedStatement, int i, CustomType customType, JdbcType jdbcType) throws SQLException { preparedStatement.setString(i, customType.toString()); } @Override public CustomType getNullableResult(ResultSet resultSet, String s) throws SQLException { return CustomType.fromString(resultSet.getString(s)); } @Override public CustomType getNullableResult(ResultSet resultSet, int i) throws SQLException { return CustomType.fromString(resultSet.getString(i)); } @Override public CustomType getNullableResult(CallableStatement callableStatement, int i) throws SQLException { return CustomType.fromString(callableStatement.getString(i)); } } ``` 在这个例子中,CustomTypeHandler继承自MyBatis中的BaseTypeHandler,并重写了一些方法来实现类型转换。setNonNullParameter方法用于将Java对象转换为数据库中的值,getNullableResult方法用于将数据库中的值转换为Java对象。 接下来,在您的实体类中使用@TableField注解来指定字段使用自定义的TypeHandler。例如: ```java public class MyEntity { @TableField(typeHandler = CustomTypeHandler.class) private CustomType customType; // 其他字段和方法... } ``` 最后,您需要在MyBatis的配置文件中注册自定义的TypeHandler。在application.properties或application.yml中添加以下配置: ```yaml mybatis-plus: configuration: map-underscore-to-camel-case: true type-handlers-package: com.example.typehandler ``` 这里的`com.example.typehandler`是您自定义TypeHandler类的包路径。 通过这些步骤,您就可以在Spring Boot使用MyBatis Plus的TypeHandler来处理自定义类型和Java对象之间的转换了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值