MyBatis中Bigint丢失精度问题

问题简述

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

解决方法

  1. 在sql语句中额外查询一个id,并将其类型转化为CHAR(64)重命名为iid。
xml 复制代码
    <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即可。
java 复制代码
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的精度问题。
相关推荐
swg32132121 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
gelald21 小时前
SpringBoot - 自动配置原理
java·spring boot·后端
@yanyu66621 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
那个失眠的夜1 天前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis
程序猿_极客1 天前
SpringBoot 三大参数注解详解:@RequestParam @RequestBody @PathVariable 区别及常用开发注解
java·spring boot·后端·面试八股文·springboot注释
小胖java1 天前
校园通衢公告枢纽系统
java·spring boot
身如柳絮随风扬1 天前
MyBatis 插件原理详解:从拦截器到动态代理,手写一个分页插件
java·mybatis
Hadoop_Liang1 天前
构建Spring Boot项目Docker镜像
spring boot·后端·docker
Flittly1 天前
【SpringAIAlibaba新手村系列】(14)MCP 本地服务与工具集成
java·spring boot·笔记·spring·ai
Flittly1 天前
【SpringAIAlibaba新手村系列】(13)Tool Calling 函数工具调用技术
java·spring boot·spring·ai