原 mybatis plus 新增json格式的数据
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1457
<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也可以自定义。
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: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
博主信息