解决 MyBatis Plus 在 PostgreSQL 中 BigDecimal 精度丢失的问题

前言

在使用 MyBatis Plus 操作 PostgreSQL 数据库时,我们可能会遇到一个棘手的问题:BigDecimal类型的数据在存储或查询时精度丢失。这不仅会影响数据的准确性,还可能导致一些难以察觉的错误。本文将详细介绍这个问题的背景、排查过程以及最终的解决方案。

1.项目场景

在我的项目中,使用了 PostgreSQL 数据库,并且在表中定义了 NUMERIC 类型的字段来存储高精度数值。例如,我们有一个 data_cover 表,其中的 xy 字段被定义为 NUMERIC 类型,用于存储坐标值:

sql 复制代码
CREATE TABLE data_cover (
    id BIGINT NOT NULL,
    parking_id BIGINT,
    timestamp BIGINT,
    x NUMERIC,
    y NUMERIC,
    floor VARCHAR(255)
);

Java 实体类中,我们使用 BigDecimal 来映射这些字段:

java 复制代码
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("data_cover")
public class DataCover {
    @TableField("id")
    private Long id;

    @TableField("parking_id")
    private Long parkingId;

    @TableField("timestamp")
    private Long timestamp;

    @TableField("x")
    private BigDecimal x;

    @TableField("y")
    private BigDecimal y;

    @TableField("floor")
    private String floor;
}

然而,在插入或查询数据时,我们发现 xy 字段的精度丢失了,只保留了整数部分。这显然不符合我们的预期。

2.排查过程

  • 检查数据库字段定义 没问题
  • 检查 SQL 插入语句 没问题
  • 检查 MyBatis Plus 配置

在插入数据之前打印了 BigDecimal 的值,结果显示,BigDecimal的值在插入之前是正确的,但插入数据库后精度丢失。这表明问题可能出现在 MyBatis Plus 的类型处理逻辑中

3.原因分析

最终排查到,MyBatis Plus 默认的类型处理器可能没有正确处理 BigDecimal 的精度。默认情况下,MyBatis Plus 使用 JdbcType.NUMERIC 来处理 BigDecimal,但可能在某些场景下(如批量操作)导致精度丢失。

4.解决方案

创建了一个自定义的 BigDecimalTypeHandler 类,覆盖了 MyBatis 的默认行为:

java 复制代码
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

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

@MappedTypes(BigDecimal.class)
@MappedJdbcTypes(JdbcType.NUMERIC)
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) throws SQLException {
        ps.setBigDecimal(i, parameter); // 直接设置,避免精度丢失
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return rs.getBigDecimal(columnName);
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return rs.getBigDecimal(columnIndex);
    }

    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getBigDecimal(columnIndex);
    }
}

然后,我们在 MyBatis Plus 的配置中注册了这个自定义类型处理器:

java 复制代码
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> {
            configuration.getTypeHandlerRegistry().register(BigDecimal.class, BigDecimalTypeHandler.class);
        };
    }
}

通过自定义类型处理器,成功解决了 BigDecimal 精度丢失的问题。自定义类型处理器确保了在插入和查询时,BigDecimal 的精度不会被截断或修改。

5.总结

在使用 MyBatis Plus 操作 PostgreSQL 数据库时,BigDecimal 精度丢失的问题可能是由于默认的类型处理器没有正确处理高精度数值。通过自定义类型处理器,我们可以覆盖默认行为,确保精度不会丢失。

如果你在项目中遇到类似的问题,可以尝试以下步骤:

复制代码
1.检查数据库字段定义,确保字段类型和精度设置正确。
2.检查 SQL 插入语句,确保 BigDecimal 的值在插入之前是正确的。
3.检查 MyBatis Plus 的配置,确保没有对 BigDecimal 进行不必要的处理。
4.使用自定义类型处理器来确保精度不会丢失。

希望这篇文章能帮助你解决类似的问题。如果你有任何疑问或建议,欢迎在评论区留言!

相关推荐
咩咩啃树皮7 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚7 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
广州灵眸科技有限公司8 小时前
xfce桌面触摸校准:基于灵眸科技EASY-EAl-Orin-Nano
数据库·windows·科技
大模型码小白9 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司9 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地10 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
憧憬成为web高手10 小时前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
wuqingshun31415910 小时前
TCP超时重传机制是为了解决什么问题?
java
段一凡-华北理工大学11 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
oradh13 小时前
Oracle XTTS实现跨版本迁移和升级(Oracle 11g单库升级至19C RAC集群)
数据库·oracle·11g升级19c·xtts跨版本迁移和升级