当前位置: 代码网 > it编程>编程语言>Java > 使用mybatisplus接收mysql字段为json类型的数据方式

使用mybatisplus接收mysql字段为json类型的数据方式

2024年05月15日 Java 我要评论
一.数据库设计create table `inv_learning_examination_questions` ( `id` bigint(20) not null, `title` varc

一.数据库设计

create table `inv_learning_examination_questions`  (
  `id` bigint(20) not null,
  `title` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null comment '题目',
  `options` json null comment '选项',
  `standard_answer` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null comment '标准答案',
  `answer_analysis` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null comment '答案解析',
  `open_range` tinyint(4) null default null comment '开放范围',
  `business_area` tinyint(4) null default null comment '业务领域',
  `difficulty_level` tinyint(4) null default null comment '难度等级',
  `topic_type` tinyint(4) null default null comment '选题类型',
  `views` int(11) null default null comment '浏览量',
  `collect` int(11) null default null comment '收藏量',
  `status` tinyint(4) null default null comment '发布状态',
  `release_time` datetime(0) null default null comment '发布时间'
  primary key (`id`) using btree
) engine = innodb character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic;

二.实体类

(切记实体类@tablename一定要加上autoresultmap = true属性,否则查不出来该属性的值)

package com.innovation.desk.domain;
 
import com.baomidou.mybatisplus.annotation.tablefield;
import com.baomidou.mybatisplus.annotation.tablename;
import com.baomidou.mybatisplus.extension.handlers.fastjsontypehandler;
import com.fasterxml.jackson.annotation.jsonformat;
import com.innovation.common.base.basellentity;
import com.innovation.common.utils.dateutil;
import com.innovation.desk.handler.optionhandler;
import lombok.data;
import lombok.equalsandhashcode;
import io.swagger.annotations.apimodel;
import io.swagger.annotations.apimodelproperty;
import org.springframework.format.annotation.datetimeformat;
 
import java.util.date;
import java.util.list;
 
/**
 * learningexaminationquestions实体类
 *
 * @author admin
 * @since 2023/03/03
 */
@data
@tablename(value = "inv_learning_examination_questions",autoresultmap = true)
@equalsandhashcode(callsuper = true)
@apimodel(value = "learningexaminationquestions对象", description = "learningexaminationquestions对象")
public class learningexaminationquestions extends basellentity {
 
    private static final long serialversionuid = 1l;
 
    @apimodelproperty(value = "题目")
    private string title;
 
    @tablefield(typehandler = optionhandler.class)
    @apimodelproperty(value = "选项")
    private list<option> options;
 
    @tablefield(typehandler = fastjsontypehandler.class)
    @apimodelproperty(value = "标准答案")
    private list<string> standardanswer;
 
    @apimodelproperty(value = "答案解析")
    private string answeranalysis;
 
    @apimodelproperty(value = "开放范围")
    private integer openrange;
 
    @apimodelproperty(value = "业务领域")
    private integer businessarea;
 
    @apimodelproperty(value = "难度等级")
    private integer difficultylevel;
 
    @apimodelproperty(value = "选题类型")
    private integer topictype;
 
    @apimodelproperty(value = "浏览量")
    private integer views;
 
    @apimodelproperty(value = "收藏量")
    private integer collect;
 
    @apimodelproperty(value = "发布状态 0.待发布 1.已发布")
    private integer status;
 
    @apimodelproperty(value = "发布状态 0.待发布 1.已发布")
    @datetimeformat(pattern = dateutil.pattern_datetime)
    @jsonformat(pattern = dateutil.pattern_datetime, timezone = "gmt+8")
    private date releasetime;
}

1.如果接收参数是实体类或list<string>

可以直接使用mybatisplus自带的解析处理器即可

例如在属性上添加

@tablefield(typehandler = fastjsontypehandler.class)

此处fastjsontypehandler有多个类型可供选择:

比如abstractjsontypehandler,abstractsqlparserhandler,fastjsontypehandler,gsontypehandler,jacksontypehandler,mybatisenumtypehandler

2.如果接收参数是个list<实体类>

这里需要注意:

以上提供的解析器不能提供完全解析

这里你需要自定义解析器做定制化解析

以下是解析器的源码

原因:

type属性被注入进来的只是list的字节码文件,通过parse方法只能将json转化为list<jsonobject>对象,而jsonobject不能强转为相应的实体类,所以在获取到解析后的对象遍历的时候会报类型转换错误异常,这时可以重写此handler的parse方法来实现自己的目的,以下是将json转化为list<option>的处理器

/**
 *  自定义cardcontent转换处理类
 * @author administrator
 */
public class optionhandler extends fastjsontypehandler {
 
    public optionhandler(class<?> type) {
        super(type);
    }
 
    @override
    protected object parse(string json) {
        return json.parsearray(json, option.class);
    }
 
    @override
    protected string tojson(object obj) {
        return super.tojson(obj);
    }
}

添加完成后只需在实体类的对应属性上添加注解

@tablefield(typehandler = optionhandler.class)即可实现解析

如果使用了xml文件可参考以下方式

<result column="options" property="options" jdbctype="varchar"
                typehandler="com.innovation.desk.handler.optionhandler"/>

总结

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

(0)

相关文章:

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

发表评论

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