91名师指路-头部
91名师指路

mybatis plus 新增json格式的数据

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:xml

<insert id="batchInsert" parameterType="java.util.ArrayList">
insert into retail.sjzc_business_region_area (id, gd_boundar_json, create_time) values
<foreach collection="list" item="item" separator=",">
(
#{item.id,jdbcType=BIGINT},
#{item.gdBoundarJson,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},
#{item.createTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>


备注:如果你不想使用自带的 com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler也可以自定义。


二:自定义JacksonTypeHandler

package com.mszl.business.common.utils;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes({Object.class})
public class CustomerJsonTypeHandler extends BaseTypeHandler<Object> {

@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
if (preparedStatement != null) {

preparedStatement.setObject(i, o);
}
}

@Override
public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
return JSON.parse(resultSet.getString(s));
}

@Override
public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
return JSON.parse(resultSet.getString(i));
}

@Override
public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return JSON.parse(callableStatement.getString(i));
}


}


三:在jdbc链接上添加  &stringtype=unspecified,此配置用于数据支持特殊类型的转换

jdbc:postgresql://127.0.0.1:5432/BUSINESS-TEST?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&stringtype=unspecified





2023-10-26 09:09:51     阅读(375)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部