Java中的BaseTypeHandler自定義類型轉(zhuǎn)換器的使用
簡(jiǎn)述
mysq5.7之后新增了json類型,但是在使用的過(guò)程中,Json數(shù)組中的值小于Integer.MAX_VALUE,則反序列化時(shí)會(huì)轉(zhuǎn)成List類型,假設(shè)我們用的是List類型的話就會(huì)碰到類型轉(zhuǎn)換錯(cuò)誤的異常。
通用的類型轉(zhuǎn)換器(存在一些問(wèn)題)
此方法可以返回對(duì)象的calss,自動(dòng)轉(zhuǎn)成對(duì)應(yīng)的類型,但是在碰到List 類型的時(shí)候,由于calss只能獲取到j(luò)ava.util.List類型無(wú)法獲取List里對(duì)象的類型,就會(huì)導(dǎo)致會(huì)使用fastJson的默認(rèn)轉(zhuǎn)換類型,也就是當(dāng)Json數(shù)組中的值小于Integer.MAX_VALUE,則反序列化時(shí)會(huì)轉(zhuǎn)成List類型。所以當(dāng)碰到這種情況時(shí),可以使用以下方法。
@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
private static ObjectMapper mapper = new ObjectMapper();
private Class<T> clazz;
public JsonTypeHandler(Class<T> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.clazz = clazz;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, this.toJson(parameter));
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.toObject(rs.getString(columnName), clazz);
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.toObject(rs.getString(columnIndex), clazz);
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.toObject(cs.getString(columnIndex), clazz);
}
private String toJson(T object) {
try {
return mapper.writeValueAsString(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private T toObject(String content, Class<?> clazz) {
if (content != null && !content.isEmpty()) {
try {
return (T) mapper.readValue(content, clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
}定制類型轉(zhuǎn)換器
指定返回值只能是 List
@MappedJdbcTypes(value = JdbcType.VARCHAR)
@MappedTypes(List.class)
public class JsonListLongTypeHandler extends BaseTypeHandler<List<Long>> {
private static final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List<Long> parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, JSON.toJSONString(parameter));
}
@Override
public List<Long> getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.getLongs(rs.getString(columnName));
}
@Override
public List<Long> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.getLongs(rs.getString(columnIndex));
}
@Override
public List<Long> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.getLongs(cs.getString(columnIndex));
}
private List<Long> getLongs(String value) {
if (StringUtils.hasText(value)) {
try {
CollectionType type = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Long.class);
Object o1 = objectMapper.readValue(value, type); List<Long> o = objectMapper.readValue(value, type);
return o;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return null;
}
}到此這篇關(guān)于Java中的BaseTypeHandler自定義類型轉(zhuǎn)換器的使用的文章就介紹到這了,更多相關(guān)BaseTypeHandler內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot使用cache緩存過(guò)程代碼實(shí)例
這篇文章主要介紹了Springboot使用cache緩存過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringCloud微服務(wù)集成Dubbo的詳細(xì)過(guò)程
Apache?Dubbo?是一款易用、高性能的?WEB?和?RPC?框架,同時(shí)為構(gòu)建企業(yè)級(jí)微服務(wù)提供服務(wù)發(fā)現(xiàn)、流量治理、可觀測(cè)、認(rèn)證鑒權(quán)等能力、工具與最佳實(shí)踐,這篇文章主要介紹了SpringCloud微服務(wù)集成Dubbo,需要的朋友可以參考下2024-03-03
詳解Java Callable接口實(shí)現(xiàn)多線程的方式
這篇文章主要介紹了詳解Java Callable接口實(shí)現(xiàn)多線程的方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
SpringBoot 注解事務(wù)聲明式事務(wù)的方式
springboot使用上述注解的幾種方式開(kāi)啟事物,可以達(dá)到和xml中聲明的同樣效果,但是卻告別了xml,使你的代碼遠(yuǎn)離配置文件。今天就扒一扒springboot中事務(wù)使用注解的玩法,感興趣的朋友一起看看吧2017-09-09
Java反應(yīng)式框架Reactor中的Mono和Flux
這篇文章主要介紹了Java反應(yīng)式框架Reactor中的Mono和Flux,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Spring的異常處理@ExceptionHandler注解解析
這篇文章主要介紹了Spring的異常處理@ExceptionHandler注解解析,當(dāng)一個(gè)Controller中有方法加了@ExceptionHandler之后,這個(gè)Controller其他方法中沒(méi)有捕獲的異常就會(huì)以參數(shù)的形式傳入加了@ExceptionHandler注解的那個(gè)方法中,需要的朋友可以參考下2023-12-12
配置springboot項(xiàng)目動(dòng)靜分離打包分離lib方式
本文介紹了如何將Spring?Boot工程中的靜態(tài)資源和配置文件分離出來(lái),以減少jar包大小,方便修改配置文件,通過(guò)在jar包同級(jí)目錄創(chuàng)建config目錄,并在pom文件中進(jìn)行相應(yīng)配置,可以實(shí)現(xiàn)配置文件的外部化,打包后的jar包和lib文件夾中分別存放了第三方j(luò)ar包和靜態(tài)資源文件2025-02-02
SpringBoot之HandlerInterceptor攔截器的使用詳解
這篇文章主要介紹了SpringBoot之HandlerInterceptor攔截器的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

