• 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. Python学习笔记整理总结【Memcache & Redis(基础+主从架构)】

    一.Memcached1.简介Memcached 是一个高性能的分布式内存对象缓存系统,一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度.提高可扩展性.用来存储 ...

  2. opencv霍夫变换

    霍夫变换不仅可以找出图片中的直线,也可以找出圆,椭圆,三角形等等,只要你能定义出直线方程,圆形的方程等等. 不得不说,现在网上的各种博客质量真的不行,网上一堆文章,乱TM瞎写,误人子弟.本身自己就没有 ...

  3. IDEA新建一个多maven模块工程(有图)

    对于一些大型的项目来说,将项目的各个模块理清并进行管理,便于后续项目的维护,使用maven管理是很方便的,它可以很好的构建模块来设计项目的整体结构,对一些小型的项目不建议使用 1.新建父maven模块 ...

  4. Eureka实战-4【开启http basic权限认证】

    在我们实际生产环境中,都需要考虑到一个安全问题,比如用户登录,又或者是eureka server,它对外暴露的有自己的rest API,如果没有安全认证,也就意味着别人可以通过rest API随意修改 ...

  5. MongoDB 学习笔记之 地理空间索引入门

    地理空间索引: 地理空间索引,可用于处理基于地理位置的查询. Point:用于指定所在的具体位置,我们以restaurants为例: db.restaurants.insert({name: &quo ...

  6. C语言I博客作业03

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/8717 我在这个课程的目 ...

  7. wampserver 运行橙色,80端口没有被占用,查看错误日志方法

    wampserver运行时橙色,经检查80端口并没有被占用,试了很多种方法都无效,去查看错误日志吧 1.以管理员身份打开CMD 注意这里必须是管理员身份的CMD ,powershell不行的 进入wa ...

  8. Jenkins节点配置

    1.系统管理---configure Global Security(全局安全设置)---Tcp port for inbound agents---指定端口---服务器防火墙中开放此端口 点击 ag ...

  9. lua行为树设计与实现

    项目需要,之前行为树用的是behaviorDesigner,要改成纯lua的 我先做了一版用递归实现,代码可读性高但是中断机制实现起来比较复杂,而且创建自定义action重写方法时需要调用父类的方法, ...

  10. Redis 哨兵机制以及灾难演练

    #### 哨兵都采用这个配置即可 ##### 1.修改sentinel.conf配置文件 ![image](https://img2018.cnblogs.com/blog/1334966/20191 ...