mybatisplus将存储的json数组(或对象)字符串通过自定义sql语句转成json对象传给前端

1.设置数据库字段和数据

在这里插入图片描述

[{“id”: 101, “key”: “品牌”, “value”: “Apple/苹果”},
{“id”: 102, “key”: “型号”, “value”: “iphone 14 Pro Max”},
{“id”: 103, “key”: “版本”, “value”: “国行”}]
此为保存在数据库的json数据

2.定义对应的实体类

(1)定义json数组中的对象实体类

@Data
public class CategoryChildrenDto {

    private String id;

    private String pid;

    private String name;

}

(2) 定义数据库映射实体类

	private List<GoodsAttributes> attributes;//存储的是json数组,用list接收

3.在mapper.xml中定义resultMap

<resultMap id="GoodsDetailMap" type="com.tminto.domain.dto.GoodsDetailDto">
        <id column="id" property="id"/>
        <result property="sellerId" column="user_Id"/>
        <result property="sellerName" column="username"/>
        <result property="avatar" column="avatar"/>
        <result property="goodsName" column="name"/>
        <result property="price" column="price"/>
        <result property="description" column="description"/>
        <!--指定字段类型处理器为JacksonTypeHandler-->
        <result property="attributes" column="attributes" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result property="image" column="image"/>
        <result property="number" column="number"/>
        <result property="viewCount" column="view_count"/>
    </resultMap>

4.自定义sql语句

指定resultMap

<select id="selectGoodsDetailById" resultMap="GoodsDetailMap">
        select
            g.id,
            g.user_Id,
            u.username,
            u.avatar,
            g.name,
            g.price,
            g.description,
            g.attributes,<!--json字段-->
            g.image,
            g.number,
            g.view_count
        from goods g
        join user u on g.user_id = u.id
        where g.id = #{id}
</select>

5.调用方法

(1)调用接口

public GoodsDetailDto getGoodsInfo(String id) {
        return baseMapper.selectGoodsDetailById(id);
    }

(2)swagger测试结果

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值