MyBatis-Plus 之 typeHandler 的使用

devtools/2024/10/9 13:25:24/
一、typeHandler 的使用
   1、存储json格式字段

        如果字段需要存储为json格式,可以使用JacksonTypeHandler处理器。使用方式非常简单,如下所示:
        

在domain实体类里面要加上,两个注解

  @TableName(autoResultMap = true) 表示自动映射resultMap


         @TableField(typeHandler = JacksonTypeHandler.class)表示将UserInfo对象转为json对象入库

java">    /*** 物料分类*/@TableField(typeHandler = SupplierMaterialCategoryTypeHandler.class)private List<MaterialCategory> materialCategory;

   2、自定义 typeHandler 实现类
        例如当我们 某个字段存储的类型为List或者Map时,我们可以自定义一个TypeHandler,以 list 为例,我们想存储一个字段类型为 list ,在数据库中的存储的格式是 多条数据以逗号分割,当查询时会自动根据逗号分割成列表格式。

        需要实现BaseTypeHandler抽象类:

java">package com.mdgyl.hussar.basic.handler;import com.mdgyl.common.util.vo.ChangeInfoVO;
import com.mdgyl.data.mybatis.plus.typehandler.ListTypeHandler;
import com.mdgyl.hussar.basic.supplier.masterdata.domain.SupplierDO;/*** @author shuquanlin*/
public class SupplierMaterialCategoryTypeHandler extends ListTypeHandler<SupplierDO.MaterialCategory> {@Overrideprotected SupplierDO.MaterialCategory specificType() {return new SupplierDO.MaterialCategory();}
}

在mybaits中的xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mdgyl.hussar.basic.supplier.changeorder.dao.ChangeOrderMapper"><resultMap id="BaseResultMap" type="com.mdgyl.hussar.basic.supplier.changeorder.domain.SupplierChangeOrderDo"><id column="id" property="id" jdbcType="BIGINT" /><result column="tenant_id" property="tenantId" jdbcType="BIGINT" /><result column="supplier_id" property="supplierId" jdbcType="BIGINT" /><result column="code" property="code" jdbcType="BIGINT" /><result column="els_account" property="elsAccount" jdbcType="VARCHAR" /><result column="name" property="name" jdbcType="VARCHAR" /><result column="external_code" property="externalCode" jdbcType="VARCHAR" /><result column="material_category" property="materialCategory" typeHandler="com.mdgyl.hussar.basic.handler.ChangeOrderMaterialCategoryTypeHandler" /><result column="status" property="status" jdbcType="VARCHAR" /><result column="audit_status" property="auditStatus" jdbcType="VARCHAR" /><result column="source_scene" property="sourceScene" jdbcType="VARCHAR" /><result column="score" property="score" jdbcType="DECIMAL" /><result column="contact_information" property="contactInformation" jdbcType="VARCHAR" /><result column="inquiry_type" property="inquiryType" jdbcType="VARCHAR" /><result column="route" property="route" jdbcType="VARCHAR" /><result column="transaction_currency" property="transactionCurrency" jdbcType="VARCHAR" /><result column="payment_currency" property="paymentCurrency" jdbcType="VARCHAR" /><result column="payment_condition" property="paymentCondition" jdbcType="VARCHAR" /><result column="open_account_condition" property="openAccountCondition" jdbcType="VARCHAR" /><result column="operating_start_time" property="operatingStartTime" jdbcType="DATE" /><result column="taxpayer_identity_number" property="taxpayerIdentityNumber" jdbcType="VARCHAR" /><result column="unified_social_credit_code" property="unifiedSocialCreditCode" jdbcType="VARCHAR" /><result column="belong_company_id" property="belongCompanyId" jdbcType="BIGINT" /><result column="purchaser_head_id" property="purchaserHeadId" jdbcType="BIGINT" /><result column="create_user_id" property="createUserId" jdbcType="BIGINT" /><result column="create_user_name" property="createUserName" jdbcType="VARCHAR" /><result column="update_user_id" property="updateUserId" jdbcType="BIGINT" /><result column="update_user_name" property="updateUserName" jdbcType="VARCHAR" /><result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /><result column="create_time" property="createTime" jdbcType="TIMESTAMP" /><result column="delete_flag" property="deleteFlag" jdbcType="VARCHAR" /><result column="legal_representative" property="legalRepresentative" jdbcType="VARCHAR" /><result column="supplier_data_id" property="supplierDataId" jdbcType="BIGINT" /></resultMap><select id="groupByStatus" parameterType="com.mdgyl.hussar.basic.param.changeOrder.SupplierChangeOrderPageQueryParam" resultType="com.mdgyl.hussar.basic.model.common.StatusGroupModel">SELECT status AS status,count(id) AS countFROM `t_supplier_change_order`WHERE tenant_id = #{tenantId} AND delete_flag = '0'<if test="code != null and code != ''">AND code = #{code}</if><if test="elsAccount != null and elsAccount != ''">AND els_account = #{elsAccount}</if><if test="name != null and name != ''">AND name = #{name}</if><if test="supplierId != null and supplierId != ''">AND supplier_id = #{supplierId}</if>GROUP BY status</select>
</mapper>

如果要查找json中的字段,可以在domain类里面加上虚拟字段

java">@TableField(exist = false)public static final String materialCategoryUserIdQuery = "JSON_CONTAINS(material_category, JSON_OBJECT('materialCategoryUserId', {0}))";

在xml文件中加上

       <if test="name != null and name != ''">AND JSON_CONTAINS(material_category, JSON_OBJECT('materialCategoryName', #{name}))</if>


http://www.ppmy.cn/devtools/123397.html

相关文章

【WebGis开发 - Cesium】如何确保Cesium场景加载完毕

目录 引言一、监听场景加载进度1. 基础代码2. 加工代码 二、进一步封装代码1. 已知存在的弊端2. 封装hooks函数 三、使用hooks方法1. 先看下效果2. 如何使用该hooks方法 三、总结 引言 本篇为Cesium开发的一些小技巧。 判断Cesium场景是否加载完毕这件事是非常有意义的。 加载…

【瑞昱RTL8763E】音频

1 音乐播放控制 1.1 播放列表更新 文件系统在sd卡中保存header.bin及name.bin两份文件用于歌曲名称的存储。为方便应用层进行歌曲显示及列表管理&#xff0c;可将这两个bin文件信息读取并保存到nor flash中。需要播放指定名称的歌曲时&#xff0c;将对于歌曲名称传递给文件系…

甲虫身体图像分割系统源码&数据集分享

甲虫身体图像分割系统源码&#xff06;数据集分享 [yolov8-seg-EfficientRepBiPAN&#xff06;yolov8-seg-C2f-FocusedLinearAttention等50全套改进创新点发刊_一键训练教程_Web前端展示] 1.研究背景与意义 项目参考ILSVRC ImageNet Large Scale Visual Recognition Challen…

Redis中GEO数据结构实现附近商户搜索

Redis的版本必须是6.2以上 在测试类中将数据导入Redis Testvoid loadShopData(){//1.查询店铺信息List<Shop> list shopService.list();//2.把店铺分组&#xff0c;按照typeId分组&#xff0c;typeId一致的放到一个集合Map<Long, List<Shop>> map list.s…

高等数学 4.2 换元积分法(二)第二类换元法

第二类换元法是&#xff1a;适当选择变量代换 x ψ ( t ) x \psi(t) xψ(t) &#xff0c;将积分 ∫ f ( x ) d x \int f(x) \mathrm{d}x ∫f(x)dx 化为积分 ∫ f [ ψ ( t ) ] ψ ′ ( t ) d t \int f[\psi(t)]\psi(t)\mathrm{d}t ∫f[ψ(t)]ψ′(t)dt .这是另一种形式的变…

pod管理及优化

一、k8s中的资源 1、资源介绍 [rootk8s-master ~]# kubectl --namespace timinglee get po No resources found in timinglee namespace. [rootk8s-master ~]# kubectl run testpod --image timinglee/nginx [rootk8s-master ~]# kubectl get pods -w NAME READY STATU…

2410git,git合并pr

git remote add dy https://github.com/dlang/dmd //添加远程,可以多添加几个,作为基础. git remote update //远程更新,全部拉进本地 git remote -v git branch -r //查看 git checkout -b pt dd/private-this //从某个分支拉到本地为pt分支 git checkout master //切换到主分…

Spring Boot ⽇志

目录 1.⽇志使⽤ 2.⽇志级别 3.⽇志配置 3.1配置⽇志级别 3.2⽇志持久化 3.3配置⽇志⽂件分割 4.更简单的⽇志输出 1.⽇志使⽤ 在使用之前我们先来了解一下为什么要使用&#xff1f; ⽇志的⽤途 1.系统监控 我们可以通过⽇志记录这个系统的运⾏状态&#xff0c;对数…