java?Long類型轉(zhuǎn)為json后數(shù)據(jù)損失精度的處理方式
最近在項(xiàng)目開發(fā)中,使用spring boot+mybatis的架構(gòu),數(shù)據(jù)庫(kù)設(shè)計(jì)主鍵id時(shí)定義為bigint類型,使用mybatis的自動(dòng)生成代碼后沒注意,主鍵在pojo里的類型為L(zhǎng)ong。查詢時(shí)獲取的對(duì)象列表取出的數(shù)值沒有問題,但轉(zhuǎn)為json傳到前端后,id的數(shù)據(jù)始終不是數(shù)據(jù)庫(kù)查出來(lái)的那個(gè)。
數(shù)據(jù)庫(kù)表結(jié)構(gòu)設(shè)計(jì)

AbumTip類

根據(jù)外鍵abum_id在數(shù)據(jù)庫(kù)中查詢的結(jié)果

Controller查到的結(jié)果

chrome瀏覽器preview結(jié)果

可以看到abumId(對(duì)應(yīng)表abum_id)和tipId(對(duì)應(yīng)表中tip_id)查詢到的Long類型的數(shù)據(jù)都不對(duì)。
解決的方法
方法一
重新生成pojo對(duì)象,將所有數(shù)據(jù)庫(kù)類型為bigint都映射成String類型。
方法二
對(duì)于使用springboot,則增加配置代碼:
package com.gj.app.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.List;
@EnableWebMvc
@Configuration
public class WebDataConvertConfig extends WebMvcConfigurerAdapter {
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
/**
* 序列換成json時(shí),將所有的long變成string
* 因?yàn)閖s中得數(shù)字類型不能包含所有的java long值
*/
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
}
}方法三
在spring MVC中
增加類:
其中LongToStringJsonConverter為自定義轉(zhuǎn)換器
public class LongToStringJsonConverter extends ObjectMapper {
/**
*
*/
private static final long serialVersionUID = 1683531771040674386L;
@Override
public ObjectMapper registerModule(Module module) {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
return super.registerModule(simpleModule);
}
} <mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<!-- <bean class="com.fasterxml.jackson.databind.ObjectMapper"> -->
<bean class="mypackage.LongToStringAdapter">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java與C++實(shí)現(xiàn)相同的MD5加密算法簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇Java與C++實(shí)現(xiàn)相同的MD5加密算法簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
Java 如何解析key為動(dòng)態(tài)的json操作
這篇文章主要介紹了Java 如何解析key為動(dòng)態(tài)的json操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
關(guān)于logback日志級(jí)別動(dòng)態(tài)切換的四種方式
這篇文章主要介紹了關(guān)于logback日志級(jí)別動(dòng)態(tài)切換的四種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
關(guān)于Java實(shí)現(xiàn)HttpServer模擬前端接口調(diào)用
這篇文章主要介紹了關(guān)于Java實(shí)現(xiàn)Http?Server模擬前端接口調(diào)用,Http?協(xié)議是建立在?TCP?協(xié)議之上的協(xié)議,所以能用?TCP?來(lái)自己模擬一個(gè)簡(jiǎn)單的?Http?Server?當(dāng)然是可以的,需要的朋友可以參考下2023-04-04
Java?超詳細(xì)講解對(duì)象的構(gòu)造及初始化
面向?qū)ο竽耸荍ava語(yǔ)言的核心,是程序設(shè)計(jì)的思想。Java語(yǔ)言的面向?qū)ο蠹夹g(shù)包括了面向?qū)ο蠛兔嫦蜻^(guò)程的基本概念,面向?qū)ο蟮奶卣鳎琂ava語(yǔ)言的類,對(duì)象,修飾符,抽象類等一系列的知識(shí)點(diǎn)2022-03-03

