欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatisPlus自定義JsonTypeHandler實(shí)現(xiàn)自動(dòng)轉(zhuǎn)化JSON問(wèn)題

 更新時(shí)間:2023年12月07日 09:25:38   作者:金鱗踏雨  
這篇文章主要介紹了MyBatisPlus自定義JsonTypeHandler實(shí)現(xiàn)自動(dòng)轉(zhuǎn)化JSON問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景

在項(xiàng)目中使用了Mybatis-Plus框架,調(diào)用了Mapper層的 insert() 

如下所示,DingRobotMsg對(duì)象 的屬性包含了其它的對(duì)象(Text、Content)

數(shù)據(jù)庫(kù)表字段里有與之對(duì)應(yīng)的字段,類(lèi)型為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: 類(lèi)型錯(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ù)庫(kù)的插入操作呢?

JsonTypeHandler

有的對(duì)象字段需要存儲(chǔ)為Json,可以直接轉(zhuǎn)成Json賦值后再保存。

也可以通過(guò)Mybatis的TypeHandler自動(dòng)處理。

通用 JsonTypeHandler 工具類(lèi)

/**
 * 對(duì)象字段轉(zhuǎn)存為Json類(lèi)型
 * @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)換為指定類(lèi)型的對(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)換為指定類(lèi)型的對(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)換為指定類(lèi)型的對(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)文章

最新評(píng)論