欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > mybatis+postgresql,无感读写json字段

mybatis+postgresql,无感读写json字段

2025/5/12 18:37:08 来源:https://blog.csdn.net/woai671/article/details/143628597  浏览:    关键词:mybatis+postgresql,无感读写json字段

1.实体类中字段类型

import com.alibaba.fastjson.JSONObject;@Data
public class TestDto {private String name;//对应数据库的JSON字段private JSONObject page_detail;}

2.自定义实现typeHandler

package base.utils;import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.*;import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;@MappedTypes(JSONObject.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class JsonTypeHandler extends BaseTypeHandler<JSONObject>{/*** 设置非空参数* @param ps* @param i* @param parameter* @param jdbcType* @throws SQLException*/@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {ps.setString(i,String.valueOf(parameter.toJSONString()));}/*** 根据列名,获取可以为空的结果* @param rs* @param columnName* @return* @throws SQLException*/@Overridepublic JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {String sqlJson = rs.getString(columnName);if (null != sqlJson) {return JSONObject.parseObject(sqlJson);}return null;}/*** 根据列索引,获取可以为内控的接口* @param rs* @param columnIndex* @return* @throws SQLException*/@Overridepublic JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {String sqlJson = rs.getString(columnIndex);if (null != sqlJson) {return JSONObject.parseObject(sqlJson);}return null;}/**** @param cs* @param columnIndex* @return* @throws SQLException*/@Overridepublic JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {String sqlJson = cs.getNString(columnIndex);if (null != sqlJson) {return JSONObject.parseObject(sqlJson);}return null;}}

3.SQL文件

<insert id="addTest">insert into test (name,page_detail)values (#{name},#{page_detail})
</insert>

4.修改配置文件

4.1 mybatis-spring-boot-starter

由于mybatis-spring-boot-starter可无需Mybatis配置文件,可直接在项目配置文件application.properties中进行配置

#指定类型处理器的所在包的路径
mybatis.type-handlers-package=base.utils

4.1 mybatis-spring

mybatis-spring则在mybatis-config.xml中增加typeHandler 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings>//略...</settings><typeAliases>//略...</typeAliases><typeHandlers><typeHandler handler="base.utils.JsonTypeHandler"/></typeHandlers>
</configuration>

5.JDBC URL 增加配置

#jdbc增加配置stringtype=unspecified
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/test?stringtype=unspecified

6.附加说明

jdbc url 使用stringtype=unspecified 的话,字符串字段会报无法判断数据类型,都需要增加类型转换,比较麻烦。

还是建议jdbc url不使用stringtype=unspecified,在json字段的SQL中,为json字段的写入增加::json的转型,如:

<insert id="addTest">insert into test (name,page_detail)values (#{name},#{page_detail}::json)
</insert>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词