当前位置: 代码网 > it编程>编程语言>Java > mybatisplus JSON类型处理器详解

mybatisplus JSON类型处理器详解

2025年10月13日 Java 我要评论
本篇文章参考官方文档:https://baomidou.com/guides/type-handler/应用场景当前有一个购物车功能,展示用户所选商品的信息,如:规格、数量、商品名、价格等,其中商品规

本篇文章参考官方文档:https://baomidou.com/guides/type-handler/

应用场景

当前有一个购物车功能,展示用户所选商品的信息,如:规格、数量、商品名、价格等,其中商品规格存储格式例如:{"尺寸": "xxl", "颜色": "黑色"}。

此处如果使用string类型存入数据库,每次获取数据以及保存数据都需要手动进行json格式和string类型的转换,为了避免这个情况,可以直接在数据库中设置该字段spdata为json类型,并且在项目中使用json处理器进行自动转换。

数据表结构

如下:

接下来在实体类中配置json处理器

package com.zxs.weixinapi.domain.entity;

import com.baomidou.mybatisplus.annotation.tablefield;
import com.baomidou.mybatisplus.annotation.tablename;
import com.baomidou.mybatisplus.annotation.idtype;
import com.baomidou.mybatisplus.annotation.tableid;
import java.io.serializable;
import java.math.bigdecimal;
import java.util.map;
import com.baomidou.mybatisplus.extension.handlers.jacksontypehandler;
import lombok.data;
import lombok.equalsandhashcode;
import lombok.experimental.accessors;

/**
 * <p>
 * 
 * </p>
 *
 * @author zxs
 * @since 2025-10-12
 */
@data
@equalsandhashcode(callsuper = false)
@accessors(chain = true)
@tablename(value = "ums_member_cart", autoresultmap = true)
public class membercart implements serializable {

    private static final long serialversionuid = 1l;

    @tableid(value = "id", type = idtype.auto)
    private integer id;

    /**
     * 商品名称
     */
    private string productname;

    /**
     * 用户id
     */
    private integer memberid;

    /**
     * 数量
     */
    private integer quantity;

    /**
     * 单价
     */
    private bigdecimal price;

    /**
     * 规格属性
     */
    @tablefield(typehandler = jacksontypehandler.class)
    private map<string, string> spdata;


}
  1. 首先需要在@tablename中配置属性autoresultmap = true
  2. 然后在需要进行json处理的属性上添加注解@tablefield(typehandler = jacksontypehandler.class)

完整代码

  • controller:
package com.zxs.weixinapi.controller;


import com.zxs.weixinapi.common.r;
import com.zxs.weixinapi.domain.dto.membercartdto;
import com.zxs.weixinapi.service.imembercartservice;
import lombok.requiredargsconstructor;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestbody;
import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.restcontroller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxs
 * @since 2025-10-12
 */
@restcontroller
@requestmapping("/member/cart")
@requiredargsconstructor
public class membercartcontroller {

    private final imembercartservice membercartservice;

    /**
     * 添加购物车
     * @param membercartdto
     * @return
     */
    @postmapping("/add")
    public r<void> add(@requestbody membercartdto membercartdto) {
        membercartservice.add(membercartdto);
        return r.success(null);
    }

}
  • mapper:
package com.zxs.weixinapi.mapper;

import com.zxs.weixinapi.domain.entity.membercart;
import com.baomidou.mybatisplus.core.mapper.basemapper;
import org.apache.ibatis.annotations.mapper;

/**
 * <p>
 *  mapper 接口
 * </p>
 *
 * @author zxs
 * @since 2025-10-12
 */
@mapper
public interface membercartmapper extends basemapper<membercart> {

}
  • service:
package com.zxs.weixinapi.service;

import com.zxs.weixinapi.domain.dto.membercartdto;
import com.zxs.weixinapi.domain.entity.membercart;
import com.baomidou.mybatisplus.extension.service.iservice;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author zxs
 * @since 2025-10-12
 */
public interface imembercartservice extends iservice<membercart> {

    /**
     * 添加购物车
     * @param membercartdto
     */
    void add(membercartdto membercartdto);
}
  • serviceimpl:
package com.zxs.weixinapi.service.impl;

import cn.hutool.core.bean.beanutil;
import com.zxs.weixinapi.domain.dto.membercartdto;
import com.zxs.weixinapi.domain.entity.membercart;
import com.zxs.weixinapi.mapper.membercartmapper;
import com.zxs.weixinapi.service.imembercartservice;
import com.baomidou.mybatisplus.extension.service.impl.serviceimpl;
import org.springframework.stereotype.service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxs
 * @since 2025-10-12
 */
@service
public class membercartserviceimpl extends serviceimpl<membercartmapper, membercart> implements imembercartservice {

    @override
    public void add(membercartdto membercartdto) {
        membercart membercart = beanutil.tobean(membercartdto, membercart.class);
        membercart.setmemberid(1);
        this.save(membercart);
    }
}
  • dto:
package com.zxs.weixinapi.domain.dto;

import lombok.data;

import java.math.bigdecimal;
import java.util.map;

@data
public class membercartdto {

    /**
     * 商品名称
     */
    private string productname;

    /**
     * 数量
     */
    private integer quantity;

    /**
     * 规格属性
     */
    private map<string, string> spdata;

    /**
     * 单价
     */
    private bigdecimal price;
}
  • entity:
package com.zxs.weixinapi.domain.entity;

import com.baomidou.mybatisplus.annotation.tablefield;
import com.baomidou.mybatisplus.annotation.tablename;
import com.baomidou.mybatisplus.annotation.idtype;
import com.baomidou.mybatisplus.annotation.tableid;
import java.io.serializable;
import java.math.bigdecimal;
import java.util.map;
import com.baomidou.mybatisplus.extension.handlers.jacksontypehandler;
import lombok.data;
import lombok.equalsandhashcode;
import lombok.experimental.accessors;

/**
 * <p>
 * 
 * </p>
 *
 * @author zxs
 * @since 2025-10-12
 */
@data
@equalsandhashcode(callsuper = false)
@accessors(chain = true)
@tablename(value = "ums_member_cart", autoresultmap = true)
public class membercart implements serializable {

    private static final long serialversionuid = 1l;

    @tableid(value = "id", type = idtype.auto)
    private integer id;

    /**
     * 商品名称
     */
    private string productname;

    /**
     * 用户id
     */
    private integer memberid;

    /**
     * 数量
     */
    private integer quantity;

    /**
     * 单价
     */
    private bigdecimal price;

    /**
     * 规格属性
     */
    @tablefield(typehandler = jacksontypehandler.class)
    private map<string, string> spdata;


}

body参数示例

运行结果:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com