一、typeHandler 的使用
1、存储json格式字段
如果字段需要存储为json格式,可以使用JacksonTypeHandler处理器。使用方式非常简单,如下所示:
在domain实体类里面要加上,两个注解
@TableName(autoResultMap = true)
表示自动映射resultMap
@TableField(typeHandler = JacksonTypeHandler.class)
表示将UserInfo对象转为json对象入库
/*** 物料分类*/@TableField(typeHandler = SupplierMaterialCategoryTypeHandler.class)private List<MaterialCategory> materialCategory;
2、自定义 typeHandler 实现类
例如当我们 某个字段存储的类型为List或者Map时,我们可以自定义一个TypeHandler,以 list 为例,我们想存储一个字段类型为 list ,在数据库中的存储的格式是 多条数据以逗号分割,当查询时会自动根据逗号分割成列表格式。
需要实现BaseTypeHandler抽象类:
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类里面加上虚拟字段
@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>