MybatisPlus怎么自定义TypeHandler映射JSON类型为List
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章将为大家详细讲解有关MybatisPlus怎么自定义TypeHandler映射JSON类型为List,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。自定义Ty
千家信息网最后更新 2024年09月22日MybatisPlus怎么自定义TypeHandler映射JSON类型为List
这篇文章将为大家详细讲解有关MybatisPlus怎么自定义TypeHandler映射JSON类型为List,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
自定义TypeHandler映射JSON类型为List
1. 实体类
这里只展示需要映射的字段,分别在所需映射的字段和实体类上添加注解。
@Data@TableName(value = "report", autoResultMap = true)public class Report { private static final long serialVersionUID = 1L; @ApiModelProperty("id") @TableId(value = "id", type = IdType.AUTO) private Integer id; @ApiModelProperty("报名信息") @TableField(typeHandler = ReportUserListTypeHandler.class) private ListreportInfo; }
2. ListTypeHandler
提供一个 JSONArray 转换为 Java List集合的处理器
import cn.hutool.core.collection.CollUtil;import cn.hutool.core.util.StrUtil;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.TypeReference;import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.MappedJdbcTypes;import org.apache.ibatis.type.MappedTypes; import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List; @MappedJdbcTypes(JdbcType.VARBINARY)@MappedTypes({List.class})public abstract class ListTypeHandlerextends BaseTypeHandler > { @Override public void setNonNullParameter(PreparedStatement ps, int i, List
parameter, JdbcType jdbcType) throws SQLException { String content = CollUtil.isEmpty(parameter) ? null : JSON.toJSONString(parameter); ps.setString(i, content); } @Override public List getNullableResult(ResultSet rs, String columnName) throws SQLException { return this.getListByJsonArrayString(rs.getString(columnName)); } @Override public List getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return this.getListByJsonArrayString(rs.getString(columnIndex)); } @Override public List getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return this.getListByJsonArrayString(cs.getString(columnIndex)); } private List getListByJsonArrayString(String content) { return StrUtil.isBlank(content) ? new ArrayList<>() : JSON.parseObject(content, this.specificType()); } /** * 具体类型,由子类提供 * * @return 具体类型 */ protected abstract TypeReference > specificType(); }
3. ReportUserListTypeHandler
由具体的子类提供List集合泛型类型
import com.alibaba.fastjson.TypeReference;import com.hanku.business.model.ReportUser; import java.util.List; public class ReportUserListTypeHandler extends ListTypeHandler{ @Override protected TypeReference > specificType() { return new TypeReference
>() { }; } }
4. Java 泛型
如果在 ListTypeHandler 类中直接提供 TypeReference> 这种类型,那就等效于TypeReference
> 这种类型,后续 fastjson 在转换时无法确定具体的 Java 类型,转换后的类型最终就会是 List
自定义TypeHandler的使用笔记
可通过自定义的TypeHandler实现某个属性在插入数据库以及查询时的自动转换,本例中是要将Map类型的属性转化成CLOB,然后存入数据库。由于是复杂的Map,mp自带的json转换器会丢失部分信息。
类型转换器还可以通过注解配置 java类型和jdbc类型
@MappedTypes
:注解配置 java 类型@MappedJdbcTypes
:注解配置 jdbc 类型
定义
@Slf4j@MappedTypes({Object.class})@MappedJdbcTypes(JdbcType.VARCHAR)public class WeightListTypeHandler extends AbstractJsonTypeHandler
使用
注意@TableName 注解 autoResultMap 属性
@Data@NoArgsConstructor@TableName(value = "mix_target",autoResultMap = true)public class MixTarget extends Model{ @TableId(value = "id", type = IdType.AUTO) private Long id; /** *指标描述 */ @TableField("description") private String description; /** * 指标名 */ @TableField("name") private String name; /** * 对应属性名 */ @TableField("property_name") private String propertyName; /** * 起始点类型 */ @TableField("source_type") private String sourceType; /** * 属性对应权值列表 * key 属性名 value指定条件下的权值 */ @TableField(value = "weight_list",typeHandler = WeightListTypeHandler.class,jdbcType = JdbcType.CLOB) private Map > weightList; /** * 运行状态 * 0 新建未运行 * 1 运行中 * 2 已运行 成功 * 3 已运行 失败 */ @TableField("status") private Integer status; /** * 是否可用 * 1 true * 0 false */ @TableField("enable") private Integer enable; @TableField("create_time") private LocalDateTime createTime;}
关于"MybatisPlus怎么自定义TypeHandler映射JSON类型为List"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
类型
属性
注解
运行
篇文章
配置
信息
子类
字段
实体
数据
数据库
更多
转换器
不错
复杂
实用
成功
内容
可以通过
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小学生网络安全知识有哪些
东莞万户网络技术有限公司
人生订票软件开发
数字怎么和数据库的数字比对
杭州志业网络技术有限公司
网站服务器选择的主要原则
微信网络安全教育平台
健康管理软件开发公司
浙江众橙网络技术有限
宁夏诚信积分管理软件开发
怀技网络技术
机电学校网络技术好吗
腾讯qq服务器有多少
数据库 恢复删除命令
软件开发 增值税专票
上海必游网络技术有限公司
网络安全中的社会工程是什么
安卓手机系统软件开发
浙江机电软件开发试验设备
培训软件开发多少
人生订票软件开发
滁州设备软件开发平台
网络安全宣传动画四川
国家网络安全周时间节点
带显卡的服务器照片
微信网络安全教育平台
大学生网络安全的实践
软件开发的两种基本架构
网络安全方案设计与实施
网络安全培训的建议