mybatis建立插入postgresql的json類型數(shù)據(jù)的方法
一、前言
最近在開發(fā)需求時(shí),需要將json類型的表字段插入數(shù)據(jù)庫,而針對(duì)postgresql數(shù)據(jù)庫,如果直接插入json字符串會(huì)報(bào)錯(cuò),那么針對(duì)這種情況,在不使用xml語句使用函數(shù)CAST強(qiáng)轉(zhuǎn)情況下,想要實(shí)現(xiàn)插入json類型的數(shù)據(jù),那么就可以在java 字段上加入自定義json處理器,就可以實(shí)現(xiàn)了。
二、如何實(shí)現(xiàn)?
1.在定義的entity對(duì)象中,針對(duì)json類型字段添加注解實(shí)現(xiàn)自定義json處理器,例如:
@Data
public class SyncLog implements Serializable {
@TableId(type= IdType.INPUT)
private Long id;
@TableField(value = "content",typeHandler = JsonTypeHandler.class)
private Object content;
/**
* 創(chuàng)建時(shí)間
*/
private Date createTime;
private static final long serialVersionUID = 1L;
}其中content在數(shù)據(jù)庫中是json類型。
2.JsonTypeHandler類實(shí)現(xiàn)如下:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.postgresql.util.PGobject;
import java.sql.*;
public class JsonTypeHandler extends BaseTypeHandler {
private static final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
String jsonString;
try {
if(parameter instanceof String){
jsonString = (String) parameter;
}else {
jsonString = objectMapper.writeValueAsString(parameter);
}
} catch (JsonProcessingException e) {
throw new SQLException("JsonTypeHandler JSON serialization error", e);
}
// 獲取數(shù)據(jù)庫類型
Connection connection = ps.getConnection();
DatabaseMetaData metaData = connection.getMetaData();
String databaseProductName = metaData.getDatabaseProductName().toLowerCase();
if (databaseProductName.contains("postgresql")) {
PGobject pgObject = new PGobject();
pgObject.setType("json"); // 或者 "jsonb" 根據(jù)需求
pgObject.setValue(jsonString);
ps.setObject(i, pgObject);
} else {
//其他數(shù)據(jù)庫處理邏輯
ps.setString(i, jsonString);
}
}
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
return parseJson(rs.getString(columnName));
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return parseJson(rs.getString(columnIndex));
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return parseJson(cs.getString(columnIndex));
}
private Object parseJson(String json) throws SQLException {
if (json == null) {
return null;
}
try {
return objectMapper.readTree(json);
} catch (Exception e) {
throw new SQLException("JsonTypeHandler JSON deserialization error", e);
}
}
}3.接下來定義一個(gè)DAO。
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface SyncLogDao extends BaseMapper {
}4.然后插入一條數(shù)據(jù),如果可以插入這條json數(shù)據(jù),表明自定義的JsonTypeHandler已生效。
@RunWith(SpringRunner.class)
@SpringBootTest(classes= AIAssetExternalApplication.class)
public class IntegrationTest {
@Autowired
SyncLogDao syncLogDao;
@Test
public void sync() throws Exception {
SyncLog syncLog = new SyncLog();
syncLog.setId(1L);
String jsonStr="{\"size\": 3474046}";
syncLog.setContent(jsonStr);
syncLogDao.insert(syncLog);
}
}結(jié)果插入成功,自定義的JsonTypeHandler生效。
到此這篇關(guān)于mybatis建立插入postgresql的json類型數(shù)據(jù)的文章就介紹到這了,更多相關(guān)mybatis json類型數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Swagger實(shí)現(xiàn)接口版本號(hào)管理方式
這篇文章主要介紹了使用Swagger實(shí)現(xiàn)接口版本號(hào)管理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringCloud入門實(shí)驗(yàn)環(huán)境搭建
這篇文章主要介紹了SpringCloud入門實(shí)驗(yàn)環(huán)境搭建的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下2021-04-04
使用@Service注解出現(xiàn)No bean named 'xxxx'&
這篇文章主要介紹了使用@Service注解出現(xiàn)No bean named 'xxxx' available]錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
使用SpringBoot+nmap4j獲取端口信息的代碼詳解
這篇文章主要介紹了使用 SpringBoot + nmap4j 獲取端口信息,包括需求背景、nmap4j 的相關(guān)介紹、代碼說明(含測試代碼、改造后的代碼及參數(shù)說明),還提到了文件讀取方式和依賴引入方式,最終請(qǐng)求能獲取到數(shù)據(jù),需要的朋友可以參考下2025-01-01
Java攔截器Interceptor實(shí)現(xiàn)原理及代碼示例
本文詳細(xì)講解了Java攔截器Interceptor實(shí)現(xiàn)原理及代碼示例,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

