mybatis plus实体类中字段映射mysql中的json格式方式


package com.example.springbootstudy.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data;

import java.util.List;

@Data
@TableName(value = "test_json", autoResultMap = true)
public class JSONEntity {

@TableId
private Long id;

@TableField(value = "name")
private String name;

@TableField(value = "age")
private Integer age;

@TableField(typeHandler = FastjsonTypeHandler.class)
private List<String> testJsonArray;

@TableField(typeHandler = FastjsonTypeHandler.class)
private HeightWeight testJsonObject;

@TableField(typeHandler = FastjsonTypeHandler.class)
private List<GroupHobby> testJsonArrayObject;

@TableField(typeHandler = FastjsonTypeHandler.class)
private String custinfo;

}

@Data
class HeightWeight{
private String height;
private String weight;
}

@Data
class GroupHobby{
private String group;
private String hobby;
}

@TableField(typeHandler = JacksonTypeHandler.class)
这样在存入是就可以把对象自动转换为json格式;

使用MyBatis-Plus的字段类型处理器,只需一个注解,就可以很方便的将数组、对象等数据直接映射到实体类中。

参考文档:https://www.uoften.com/article/212699.html

 

posted @ 2023-05-19 18:07  郭慕荣  阅读(2117)  评论(0编辑  收藏  举报