• pom.xml

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
    <dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.8.0</version>
    </dependency>
    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    </dependency>

    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.49</version>
    <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.0</version>
    </dependency>

    <!-- for testing -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>
  • 配置类

    @Configuration
    @MapperScan("com.mp.typehandler.mapper")
    public class MybatisPlusConfig {

    }
  • application.yml

    spring:
    datasource:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:h2:tcp://192.168.180.115:19200/~/mem/test
    username: root
    password: test
  • 实体类

     @Data
    public class Currency {
    /**
    * 类型: 人民币 RMB , 美元 USD
    */
    private String type;
    /**
    * 金额
    */
    private Double amount;

    }
    @Data
    public class OtherInfo {
    /**
    * 性别
    */
    private String sex;
    /**
    * 居住城市
    */
    private String city;

    }

    Data
    @Accessors(chain = true)
    @TableName(value="user",autoResultMap = true)
    public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    /**
    * 注意!! 必须开启映射注解
    *
    * @TableName(autoResultMap = true)
    *
    * 以下两种类型处理器,二选一 也可以同时存在
    *
    * 注意!!选择对应的 JSON 处理器也必须存在对应依赖包
    */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private Wallet wallet;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private OtherInfo otherInfo;

    }
    @Data
    public class Wallet {
    /**
    * 名称
    */
    private String name;
    /**
    * 各种货币
    */
    private List<Currency> currencyList;

    }
  • mapper

    public interface UserMapper extends BaseMapper<User> {

    }
  • 数据库脚本

     DELETE FROM user;

    INSERT INTO user (id, name, age, email, wallet, other_info) VALUES
    (1, 'Jone', 18, 'test1@baomidou.com', '{
    "name": "支付宝钱包",
    "currencyList": [{
    "type": "USD",
    "amount": 999.19
    },{
    "type": "RMB",
    "amount": 1000.19
    }]
    }', '{
    "sex": "男",
    "city": "南昌"
    }'),
    (2, 'Jack', 20, 'test2@baomidou.com', '{
    "name": "微信钱包",
    "currencyList": [{
    "type": "USD",
    "amount": 888.18
    },{
    "type": "RMB",
    "amount": 1000.18
    }]
    }', '{
    "sex": "男",
    "city": "青岛"
    }');

    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 '邮箱',
    wallet VARCHAR(3000) NULL DEFAULT NULL COMMENT '钱包',
    other_info VARCHAR(3000) NULL DEFAULT NULL COMMENT '其他信息',
    PRIMARY KEY (id)
    );
  • 测试类

    @SpringBootTest
    public class TypehandlerApplicationTests {


    @Autowired
    private UserMapper userMapper;
    ​ @Test
    public void test() {
    User Jone = userMapper.selectById(1);
    System.err.println(Jone.getName());
    System.err.println(Jone.getOtherInfo().getSex());

    User Jack = userMapper.selectById(1);
    System.err.println(Jack.getName());
    }

    }
  • 测试结果

     Consume Time:7 ms 2019-10-30 20:13:03
    Execute SQL:SELECT * FROM user WHERE id=1

    Jone

    Consume Time:0 ms 2019-10-30 20:13:03
    Execute SQL:SELECT * FROM user WHERE id=1

    Jone

SpringBoot与MybatisPlus3.X整合之字段类型处理器(八)的更多相关文章

  1. SpringBoot与MybatisPlus3.X整合之通用枚举(十二)

    一 通用枚举 解决了繁琐的配置,让 mybatis 优雅的使用枚举属性! 自3.1.0开始,可配置默认枚举处理类来省略扫描通用枚举配置 默认枚举配置 升级说明: 3.1.0 以下版本改变了原生默认行为 ...

  2. SpringBoot与MybatisPlus3.X整合示例(十六)

    包含 分页.逻辑删除.自定义全局操作 等绝大部分常用功能的使用示例,相当于大整合的完整示例 pom.xml <dependencies> <dependency> <gr ...

  3. SpringBoot与MybatisPlus3.X整合之动态表名 SQL 解析器(七)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  4. (九) SpringBoot起飞之路-整合/集成Swagger 2 And 3

    兴趣的朋友可以去了解一下其他几篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Spri ...

  5. postgreSQL-如何查数据库表、字段以及字段类型、注释等信息?

    之前从网上也搜索了一些关于postgreSQL的系统表含义以及如何查表相关信息,但是都没有一个完整的内容,所以自己将找到的一些内容作了下整合,大家可以根据自己需要再对sql进行调整. --1.查询对象 ...

  6. SpringBoot学习- 3、整合MyBatis

    SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...

  7. 实例讲解Springboot以Template方式整合Redis及序列化问题

    1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...

  8. (七) SpringBoot起飞之路-整合SpringSecurity(Mybatis、JDBC、内存)

    兴趣的朋友可以去了解一下前五篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Sprin ...

  9. (八) SpringBoot起飞之路-整合Shiro详细教程(MyBatis、Thymeleaf)

    兴趣的朋友可以去了解一下前几篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Sprin ...

随机推荐

  1. Mycat 配置文件server.xml

    server.xml 几乎保存了所有 mycat 需要的系统配置信息. 1.system 标签: 该标签内嵌套的所有 property 标签都与系统配置有关. charset 属性: 该属性用于字符集 ...

  2. win7远程连接全屏和窗口模式切换

    最近开发需要win7远程连接,我知道在连接的时候可以设置全屏模式 但是进去之后想要切换就只能通过快捷键了上网查了一下是ctrl+alt+break.网上说的没有错.我查官方文档也是这样.但是我按的时候 ...

  3. 关于Python selenium实现类似比价软件的功能

    偶然间想实现比价的功能,正常requests途径比较难实现,于是乎想到可以selenium可以简易实现,下面是代码. import requests from selenium import webd ...

  4. 防DOS攻击-网络连接法

    #!/bin/bash netstat -antup | grep SYN_RECV | awk '{print $5}' |awk -F: '{print $1}'|sort|uniq -c > ...

  5. js数组和表的基本操作

    数组 var v = [3, 6, "hello"]; console.log(v.length); 数组的遍历1 function ss() { for (var i = 0; ...

  6. 浅谈MVC&MTV设计模式

    在目前基于Python语言的几十个Web开发框架中,几乎所有的全栈框架都强制或引导开发者使用MVC设计模式.所谓全栈框架,是指除了封装网络和线程操作,还提供HTTP.数据库读写管理.HTML模板引擎等 ...

  7. AlexNet模型的解析及tensorflow实现

    AlexNet是ImageNet LSVRC 2012比赛中分类效果第一的深度神经网络模型,点击链接下载论文http://papers.nips.cc/paper/4824-imagenet-clas ...

  8. Java运算符及优先级(全)

    运算符是一种特殊的符号,用以表示数据的运算.赋值和比较等. Java运算符分为以下几种: • 算术运算符:+,-,*,/,%,++,-- • 赋值运算符:= • 扩展后的赋值运算符:+=,-=,*=, ...

  9. 详解计算机中的Byte、bit、字、字长、字节

    最近突然有同事问我,关于计算机中的计量单位大B和小b的区别,以及KB到GB之间的换算问题,我当时觉得这问题简单,大B是 byte,小b是bit,但是想到他俩之间的换算时,一时有些想不起来具体是1Byt ...

  10. 在我的新书里,尝试着用股票案例讲述Python爬虫大数据可视化等知识

    我的新书,<基于股票大数据分析的Python入门实战>,预计将于2019年底在清华出版社出版. 如果大家对大数据分析有兴趣,又想学习Python,这本书是一本不错的选择.从知识体系上来看, ...