欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis-Plus?中?typeHandler?的使用實(shí)例詳解

 更新時(shí)間:2024年10月09日 10:32:05   作者:小裕哥略帥  
本文介紹了在MyBatis-Plus中如何使用typeHandler處理json格式字段和自定義typeHandler,通過(guò)使用JacksonTypeHandler,可以簡(jiǎn)單實(shí)現(xiàn)將實(shí)體類(lèi)字段轉(zhuǎn)換為json格式存儲(chǔ),感興趣的朋友跟隨小編一起看看吧

一、typeHandler 的使用

 1、存儲(chǔ)json格式字段

        如果字段需要存儲(chǔ)為json格式,可以使用JacksonTypeHandler處理器。使用方式非常簡(jiǎn)單,如下所示:

在domain實(shí)體類(lèi)里面要加上,兩個(gè)注解

  @TableName(autoResultMap = true) 表示自動(dòng)映射resultMap

@TableField(typeHandler = JacksonTypeHandler.class)表示將UserInfo對(duì)象轉(zhuǎn)為json對(duì)象入庫(kù)

    /**
     * 物料分類(lèi)
     */
    @TableField(typeHandler = SupplierMaterialCategoryTypeHandler.class)
    private List<MaterialCategory> materialCategory;

2、自定義 typeHandler 實(shí)現(xiàn)類(lèi)

        例如當(dāng)我們 某個(gè)字段存儲(chǔ)的類(lèi)型為L(zhǎng)ist或者M(jìn)ap時(shí),我們可以自定義一個(gè)TypeHandler,以 list 為例,我們想存儲(chǔ)一個(gè)字段類(lèi)型為 list ,在數(shù)據(jù)庫(kù)中的存儲(chǔ)的格式是 多條數(shù)據(jù)以逗號(hào)分割,當(dāng)查詢時(shí)會(huì)自動(dòng)根據(jù)逗號(hào)分割成列表格式。

        需要實(shí)現(xiàn)BaseTypeHandler抽象類(lèi):

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> {
    @Override
    protected SupplierDO.MaterialCategory specificType() {
        return new SupplierDO.MaterialCategory();
    }
}

在mybaits中的xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//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 count
        FROM `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類(lèi)里面加上虛擬字段

    @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>

到此這篇關(guān)于MyBatis-Plus 之 typeHandler 的使用的文章就介紹到這了,更多相關(guān)MyBatis-Plus typeHandler 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫(kù)

    SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫(kù)

    本文主要介紹了SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • Java controller接口出入?yún)r(shí)間序列化轉(zhuǎn)換操作方法(兩種)

    Java controller接口出入?yún)r(shí)間序列化轉(zhuǎn)換操作方法(兩種)

    這篇文章主要介紹了Java controller接口出入?yún)r(shí)間序列化轉(zhuǎn)換操作方法,本文給大家列舉兩種簡(jiǎn)單方法,感興趣的朋友一起看看吧
    2025-04-04
  • Mybatis判斷空字符串的問(wèn)題

    Mybatis判斷空字符串的問(wèn)題

    這篇文章主要介紹了Mybatis判斷空字符串的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java判斷字符串是否在List中的方案詳解(忽略大小寫(xiě))

    Java判斷字符串是否在List中的方案詳解(忽略大小寫(xiě))

    對(duì)于需要頻繁調(diào)用且數(shù)據(jù)量大的情況,有幾種優(yōu)化方案可以選擇,下面給大家分享三種方案給大家詳細(xì)介紹java字符串判斷是否在list中,感興趣的朋友一起看看吧
    2025-05-05
  • Java while和do...while循環(huán)的使用方式

    Java while和do...while循環(huán)的使用方式

    文章對(duì)比Java while與do-while循環(huán)的核心機(jī)制、語(yǔ)法差異、適用場(chǎng)景及錯(cuò)誤防范,強(qiáng)調(diào)do-while至少執(zhí)行一次的特點(diǎn),指出語(yǔ)法規(guī)范(如結(jié)尾分號(hào))和性能優(yōu)化要點(diǎn),并提及Java17模式匹配對(duì)循環(huán)的擴(kuò)展支持
    2025-07-07
  • java LeetCode題解不同路徑

    java LeetCode題解不同路徑

    這篇文章主要為大家介紹了java LeetCode題解不同路徑示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Java網(wǎng)絡(luò)編程TCP實(shí)現(xiàn)聊天功能

    Java網(wǎng)絡(luò)編程TCP實(shí)現(xiàn)聊天功能

    這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程TCP實(shí)現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Ribbon核心原理與架構(gòu)深度詳解

    Ribbon核心原理與架構(gòu)深度詳解

    Ribbon是Netflix開(kāi)源的客戶端負(fù)載均衡器,支持服務(wù)發(fā)現(xiàn)、多種負(fù)載策略、健康檢查及與SpringCloud集成,適用于微服務(wù)架構(gòu)優(yōu)化調(diào)用性能,未來(lái)將與ServiceMesh協(xié)同演進(jìn),接下來(lái)通過(guò)本文給大家介紹Ribbon核心原理與架構(gòu),感興趣的朋友一起看看吧
    2025-08-08
  • 詳解SpringBoot如何自定義Starter

    詳解SpringBoot如何自定義Starter

    Starter是Spring Boot中的一個(gè)非常重要的概念,Starter相當(dāng)于模塊,它能將模塊所需的依賴整合起來(lái)并對(duì)模塊內(nèi)的Bean根據(jù)環(huán)境( 條件)進(jìn)行自動(dòng)配置。本文將介紹SpringBoot如何自定義Starter,感興趣的可以學(xué)習(xí)一下
    2021-12-12
  • tomcat啟動(dòng)完成執(zhí)行 某個(gè)方法 定時(shí)任務(wù)(Spring)操作

    tomcat啟動(dòng)完成執(zhí)行 某個(gè)方法 定時(shí)任務(wù)(Spring)操作

    這篇文章主要介紹了tomcat啟動(dòng)完成執(zhí)行 某個(gè)方法 定時(shí)任務(wù)(Spring)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09

最新評(píng)論