MyBatisPlus自定義JsonTypeHandler實(shí)現(xiàn)自動(dòng)轉(zhuǎn)化JSON問題
背景
在項(xiàng)目中使用了Mybatis-Plus框架,調(diào)用了Mapper層的 insert()
如下所示,DingRobotMsg對(duì)象 的屬性包含了其它的對(duì)象(Text、Content)
數(shù)據(jù)庫表字段里有與之對(duì)應(yīng)的字段,類型為json
@Service public class DingRobotMsgServiceImpl extends ServiceImpl<DingRobotMsgMapper, DingRobotMsg> implements IDingRobotMsgService { @Autowired private DingRobotMsgMapper dingRobotMsgMapper; @Override public void insertRobotMsg(DingRobotMsg dingRobotMsg) { // 新增 dingRobotMsg.setState("1"); if (dingRobotMsg.getMsgtype().equals("text") || dingRobotMsg.getMsgtype().equals("file")) { dingRobotMsgMapper.insert(dingRobotMsg); } else { // TODO: 類型錯(cuò)誤! } } }
@Data @TableName("t_dingtalk_recemsg") public class DingRobotMsg { @TableId(value = "id", type = IdType.AUTO) private Long id; @TableField(value = "msgtype") private String msgtype; private Content content; private Text text; // ... }
這種情況,我們?nèi)绾卧诓辉黾訕I(yè)務(wù)邏輯(數(shù)據(jù)處理)的情況下實(shí)現(xiàn)數(shù)據(jù)庫的插入操作呢?
JsonTypeHandler
有的對(duì)象字段需要存儲(chǔ)為Json,可以直接轉(zhuǎn)成Json賦值后再保存。
也可以通過Mybatis的TypeHandler自動(dòng)處理。
通用 JsonTypeHandler 工具類
/** * 對(duì)象字段轉(zhuǎn)存為Json類型 * @param <T> */ @MappedTypes({Text.class, Content.class}) public class JsonTypeHandler<T> extends BaseTypeHandler<T> { private final Class<T> type; public JsonTypeHandler(Class<T> type) { if (type == null) { throw new IllegalArgumentException("Type argument cannot be null"); } this.type = type; } @Override public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { // 將對(duì)象轉(zhuǎn)換為JSON字符串并設(shè)置到PreparedStatement中 ps.setString(i, JSON.toJSONString(parameter)); } @Override public T getNullableResult(ResultSet rs, String columnName) throws SQLException { // 從ResultSet中獲取JSON字符串并轉(zhuǎn)換為指定類型的對(duì)象 String jsonString = rs.getString(columnName); return JSON.parseObject(jsonString, type); } @Override public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 從ResultSet中獲取JSON字符串并轉(zhuǎn)換為指定類型的對(duì)象 String jsonString = rs.getString(columnIndex); return JSON.parseObject(jsonString, type); } @Override public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 從CallableStatement中獲取JSON字符串并轉(zhuǎn)換為指定類型的對(duì)象 String jsonString = cs.getString(columnIndex); return JSON.parseObject(jsonString, type); } }
JsonTypeHandler 的使用
在entry對(duì)象的字段上面加上下面的注解即可!
@TableField(typeHandler = JsonTypeHandler.class) private Content content; @TableField(typeHandler = JsonTypeHandler.class) private Text text;
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java兩種方式簡單實(shí)現(xiàn):爬取網(wǎng)頁并且保存
本篇文章主要介紹了Java兩種方式簡單實(shí)現(xiàn):爬取網(wǎng)頁并且保存 ,主要用UrlConnection、HttpClient爬取實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12SpringMVC運(yùn)行時(shí)出現(xiàn)404錯(cuò)誤的解決辦法匯總(基本包含所有錯(cuò)誤可能)
初學(xué)SpringMVC基本都會(huì)碰到404問題(確實(shí)也困擾了我好長時(shí)間),但出現(xiàn)404問題的原因有很多,如果確認(rèn)路徑,代碼沒問題,并且服務(wù)器可以正常啟動(dòng),依然出現(xiàn)404問題的話,就根據(jù)本篇步驟逐一排查,需要的朋友可以參考下2024-04-04淺談SpringBoot在使用測試的時(shí)候是否需要@RunWith
本文主要介紹了淺談SpringBoot在使用測試的時(shí)候是否需要@RunWith,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Java 內(nèi)存模型中的happen-before關(guān)系詳解
這篇文章主要為大家介紹了Java 內(nèi)存模型中的happen-before關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例
這篇文章主要介紹了基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02打包部署若依(RuoYi)SpringBoot后端和Vue前端圖文教程
若依是一個(gè)使用Spring Boot作為后端和Vue.js作為前端的全棧應(yīng)用開發(fā)平臺(tái),下面這篇文章主要給大家介紹了關(guān)于打包部署若依(RuoYi)SpringBoot后端和Vue前端的相關(guān)資料,需要的朋友可以參考下2024-05-05java后端如何實(shí)現(xiàn)防止接口重復(fù)提交
這篇文章主要介紹了java后端如何實(shí)現(xiàn)防止接口重復(fù)提交問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05java web服務(wù)器實(shí)現(xiàn)跨域訪問
這篇文章主要為大家詳細(xì)介紹了java web服務(wù)器實(shí)現(xiàn)跨域訪問,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08