MyBatis中Bigint丢失精度问题

问题简述

  • 在MyBatis中,如果使用Map对象接收值为bigint类型的数据时,可能会遇到数据后几位变为0的情况。
    这使得当你的id为bigint时,前端接收到的id可能与数据库存储的id不一致。
  • 传统的解决方法是将entity设置为String类型即可。但这只局限于resultType为entity的情况。
    如果resultType为Map呢。

解决方法

  1. 在sql语句中额外查询一个id,并将其类型转化为CHAR(64)重命名为iid。
    <select id="test" resultType="java.util.Map">
        select CONVERT(id, CHAR(64)) AS iid, ${tableName}.* from ${tableName} where is_deleted = '0'
    select>
  1. 在调用该Map方法后,在业务层重新将iid映射回id即可。
List<Map<String, Object>> dbData= Tool.TransHump(perfTtTeacherInfoMapper.test(tableName));
listByPerfType.forEach(item -> {
            item.remove("id");
            item.put("id",item.get("iid"));
            item.remove("iid");
        }
);
  1. 这样id字段就会变为String类型,也就避免了BigInt的精度问题。

你可能感兴趣的:(mybatis,java-ee,spring,boot,MyBatis-plus)