DROP TABLE IF EXISTS user;CREATE TABLE user
(id BIGINT(20) NOT NULL COMMENT '主键ID',name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',age INT(11) NULL DEFAULT NULL COMMENT '年龄',email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',wallets VARCHAR(3000) NULL DEFAULT NULL COMMENT '钱包',other_info VARCHAR(3000) NULL DEFAULT NULL COMMENT '其他信息',PRIMARY KEY (id)
);
DELETE FROM user;INSERT INTO user (id, name, age, email, wallets, other_info) VALUES
(1, 'Jone', 18, 'test1@baomidou.com', '[{"name": "支付宝钱包","currencyList": [{"type": "USD","amount": 999.19},{"type": "RMB","amount": 1000.19}]
},{"name": "微信钱包","currencyList": [{"type": "USD","amount": 888.18},{"type": "RMB","amount": 1000.18}]
}]', '{"sex": "男","city": "南昌"
}'),
(2, 'Jack', 20, 'test2@baomidou.com', '[{"name": "银联钱包","currencyList": [{"type": "USD","amount": 888.18},{"type": "RMB","amount": 1000.18}]
}]', '{"sex": "男","city": "青岛"
}');
package com.zs.testmybatisplus;import java.io.IOException;
import java.util.List;import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.zs.testmybatisplus.entity.Wallet;/*** 自定义复杂类型处理器<br/>* 不要问我为什么要重写 parse 因为顶层父类是无法获取到准确的待转换复杂返回类型数据*/
public class WalletListTypeHandler extends JacksonTypeHandler {public WalletListTypeHandler(Class<?> type) {super(type);}@Overrideprotected Object parse(String json) {try {/*** [{*     "name": "银联钱包",*     "currencyList": [*     {*         "type": "USD",*         "amount": 888.18*     },*     {*         "type": "RMB",*         "amount": 1000.18*     }*     ]* }]*/return getObjectMapper().readValue(json, new TypeReference<List<Wallet>>() {});} catch (IOException e) {throw new RuntimeException(e);}}
}
package com.zs.testmybatisplus.config;import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import com.baomidou.mybatisplus.extension.handlers.GsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;/*** @author miemie* @since 2019-11-28*/
@Component
public class MpJsonConfig implements CommandLineRunner {/*** 可以set进去自己的*/@Overridepublic void run(String... args) throws Exception {JacksonTypeHandler.setObjectMapper(new ObjectMapper());GsonTypeHandler.setGson(new Gson());}
}

1实体类

package com.zs.testmybatisplus.entity;import java.util.List;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.zs.testmybatisplus.WalletListTypeHandler;import lombok.Data;
import lombok.experimental.Accessors;/*** <p>* 用户实体对应表 user* </p>** @author hubin* @since 2018-08-11*/
@Data
@Accessors(chain = true)
@TableName(autoResultMap = true)
public class User {private Long id;private String name;private Integer age;private String email;/*** 注意!! 必须开启映射注解** @TableName(autoResultMap = true)* <p>* 以下两种类型处理器,二选一 也可以同时存在* <p>* 注意!!选择对应的 JSON 处理器也必须存在对应依赖包*/@TableField(typeHandler = WalletListTypeHandler.class)private List<Wallet> wallets;@TableField(typeHandler = FastjsonTypeHandler.class)private OtherInfo otherInfo;
}
package com.zs.testmybatisplus.entity;import java.util.List;import lombok.Data;/*** 钱包*/
@Data
public class Wallet {/*** 名称*/private String name;/*** 各种货币*/private List<Currency> currencyList;
}
package com.zs.testmybatisplus.entity;import lombok.Data;/*** 其他信息*/
@Data
public class OtherInfo {/*** 性别*/private String sex;/*** 居住城市*/private String city;
}
package com.zs.testmybatisplus.entity;import lombok.Data;/*** 货币*/
@Data
public class Currency {/*** 类型: 人民币 RMB , 美元 USD*/private String type;/*** 金额*/private Double amount;
}

2mapper

package com.zs.testmybatisplus.mapper;import org.apache.ibatis.annotations.Mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zs.testmybatisplus.entity.User;/*** <p>* MP 支持不需要 UserMapper.xml 这个模块演示内置 CRUD 咱们就不要 XML 部分了* </p>** @author hubin* @since 2018-08-11*/
@Mapper
public interface UserMapper extends BaseMapper<User> {}

3测试类

package com.zs.testmybatisplus;import javax.annotation.Resource;import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import com.zs.testmybatisplus.entity.User;
import com.zs.testmybatisplus.mapper.UserMapper;/*** <p>* 内置 类型处理器 演示* </p>** @author hubin* @since 2018-08-11*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {@Resourceprivate UserMapper userMapper;/*** 自定义类型处理器演示*/@Testpublic void test(){/*** SELECT id,name,age,email,wallets,other_info FROM user WHERE id=?* <==    Columns: id, name, age, email, wallets, other_info* <==        Row: 1, Jone, 18, test1@baomidou.com, [{*     "name": "支付宝钱包",*     "currencyList": [{*         "type": "USD",*         "amount": 999.19*     },{*         "type": "RMB",*         "amount": 1000.19*     }]* },{*     "name": "微信钱包",*     "currencyList": [{*         "type": "USD",*         "amount": 888.18*     },{*         "type": "RMB",*         "amount": 1000.18*     }]* }], {*         "sex": "男",*         "city": "南昌"* }*//*** [{*     "name": "银联钱包",*     "currencyList": [*     {*         "type": "USD",*         "amount": 888.18*     },*     {*         "type": "RMB",*         "amount": 1000.18*     }*     ]* }]*//*** {*         "sex": "男",*         "city": "南昌"* }*/User Jone = userMapper.selectById(1);Assert.assertEquals("Jone", Jone.getName());Assert.assertEquals(2, Jone.getWallets().size());Assert.assertEquals("微信钱包", Jone.getWallets().get(1).getName());User Jack = userMapper.selectById(2);Assert.assertEquals("Jack", Jack.getName());Assert.assertEquals(1, Jack.getWallets().size());Assert.assertEquals("银联钱包", Jack.getWallets().get(0).getName());}
}

09_Mybatis-plus类型处理器示例,例如 json 字段对象转换相关推荐

  1. Flutter实战——Map Json Object对象转换

    最近在写Flutter代码,发现iOS侧使用的是字典类型(Map),而我们Android使用的是对象类型. 数据类型完全不同,这可咋整? 于是研究了一下Map相关的Json转换,希望能帮助到大家 本篇 ...

  2. fastJson API 最快的Json和对象转换工具

    FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能. 实际上其他的json处理工具都和它差 ...

  3. 2.FastJson公司--阿里巴巴开源的速度最快的Json和对象转换工具

    转自:https://blog.csdn.net/gongpulin/article/details/52062532 这是关于FastJson的一个使用Demo,在Java环境下验证的 这是关于Fa ...

  4. Jackson使用示例:将Java对象转换成Map

  5. scala:json4s库—Json与对象转换

    github: https://github.com/json4s/json4s 使用 json4s库 依赖: val json4sNative = "org.json4s" %% ...

  6. MyBatis自定义类型处理器 TypeHandler

    在项目开发中经常会遇到一个问题: 当我们在javabean中自定义了枚举类型或者其它某个类型,但是在数据库中存储时往往需要转换成数据库对应的类型,并且在从数据库中取出来时也需要将数据库类型转换为jav ...

  7. Spring Restful Web服务示例 - 使用JSON/Jackson和客户端程序

    Spring Restful Web服务示例 - 使用JSON/Jackson和客户端程序 Spring是最广泛使用的Java EE框架之一.我们之前已经看到了如何使用Spring MVC来创建基于J ...

  8. Spring Restful Web服务示例 - 使用JSON,Jackson和客户端程序

    Spring Restful Web服务示例 - 使用JSON,Jackson和客户端程序 Spring是最广泛使用的Java EE框架之一.我们之前已经看到了如何使用Spring MVC来创建基于J ...

  9. desc 枚举类型id_想让代码更优雅?Mybatis类型处理器了解一下!

    明确需求 在设计之初,sys_role表的enabled字段有2个可选值,其中0 代表禁用,1代表启用,而且实体类中我们使用的是Interger类型: 源码展示 /** * 有效标志 */ priva ...

最新文章

  1. 张文宏在人工智能大会上“泼冷水”:国内疫情基本结束,防控一开始用的全是“人工”!
  2. 在MAC下安装redis以及其PHP扩展
  3. 用一条sql取得第10到第20条的记录-Mssql数据库
  4. zillow房价预测比赛_Kaggle竞赛 —— 房价预测 (House Prices)
  5. Android之选项卡
  6. 明天回湖北!今天要开始收拾烂摊子了
  7. mysql数据库 integer_MySQL数据库中,常用的数据类型
  8. CO_P0(logisim)
  9. 海康威视录像机(DS-8632N-E8)开机卡LOGO
  10. 屏幕坐标转换世界坐标
  11. matlab mri的k空间,【基础理论】磁共振成像中K空间概念及其应用
  12. 使用美图秀秀批量处理照片
  13. Pandas知识点-绘制统计图
  14. 基因调控分析之转录因子结合位点分析
  15. 若依前后端分离版本集成CAS Server5.3
  16. RabbitMQ环境的搭建和报错
  17. 企业内部文档共享平台-MM-WiKi
  18. 每日小技巧——教你用一行Python代码去除照片背景
  19. 解决IntelliJIdea许可证过期,输入许可证认证不成功
  20. openEuler ceph mgr dashboard 无法登陆 报错 401 Unauthorized You are not authorized to access that resource

热门文章

  1. 教你作一份高水准的简历
  2. OpenGL颜色渐变
  3. ​​​​​​​墨画子卿第三章:初心第2节:回家
  4. 计算机存储容量1tb等于多少,1tb等于多少mb(1tb等于多少gb)
  5. 安卓Vitamio播放课程视频
  6. 树莓派4B官方说明文档
  7. 想去看演唱却总是抢不到票?教你用Python制作一个自动抢票脚本
  8. rpm mysql nokey_rpm包时遇到Header V3 DSA signature: NOKEY时解决办法
  9. 微信小程序开发13 云开发:云原生一体化应用开发平台
  10. 使用GerberTools的Gerber Panelizer工具进行gerber文件拼板的方法