SpringBoot与MybatisPlus3.X整合之字段类型处理器(八)
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整合之字段类型处理器(八)的更多相关文章
- SpringBoot与MybatisPlus3.X整合之通用枚举(十二)
一 通用枚举 解决了繁琐的配置,让 mybatis 优雅的使用枚举属性! 自3.1.0开始,可配置默认枚举处理类来省略扫描通用枚举配置 默认枚举配置 升级说明: 3.1.0 以下版本改变了原生默认行为 ...
- SpringBoot与MybatisPlus3.X整合示例(十六)
包含 分页.逻辑删除.自定义全局操作 等绝大部分常用功能的使用示例,相当于大整合的完整示例 pom.xml <dependencies> <dependency> <gr ...
- SpringBoot与MybatisPlus3.X整合之动态表名 SQL 解析器(七)
pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...
- (九) SpringBoot起飞之路-整合/集成Swagger 2 And 3
兴趣的朋友可以去了解一下其他几篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Spri ...
- postgreSQL-如何查数据库表、字段以及字段类型、注释等信息?
之前从网上也搜索了一些关于postgreSQL的系统表含义以及如何查表相关信息,但是都没有一个完整的内容,所以自己将找到的一些内容作了下整合,大家可以根据自己需要再对sql进行调整. --1.查询对象 ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- 实例讲解Springboot以Template方式整合Redis及序列化问题
1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...
- (七) SpringBoot起飞之路-整合SpringSecurity(Mybatis、JDBC、内存)
兴趣的朋友可以去了解一下前五篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Sprin ...
- (八) SpringBoot起飞之路-整合Shiro详细教程(MyBatis、Thymeleaf)
兴趣的朋友可以去了解一下前几篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Sprin ...
随机推荐
- Spring 梳理-跨重定向请求传递数据-Flash
Spring MVC Flash Attribute 的讲解与使用示例 1. Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/R ...
- MongoDB的全文索引
Table of Contents 背景 如何使用 准备工作:插入数据 建立全局索引 查询结果 使用中存在哪些问题? 英文存在停止词 中文无法采用全文索引 前面了解了多种索引方式,比如单键索引,多 ...
- Java 学习笔记之 Daemon线程
Daemon线程: 线程: 用户线程 守护线程 守护线程是一种特殊的线程,在进程中不存在非守护线程了,则守护线程自动销毁. public class DaemonThread extends Thre ...
- 快学Scala 第十四课 (读取行,读取字符, 控制台读取)
读取行: import scala.io.Source object FileReader { def main(args: Array[String]): Unit = { val source = ...
- 一次Commons-HttpClient的BindException排查
线上有个老应用,在流量增长的时候,HttpClient抛出了BindException.部分的StackTrace信息如下: java.net.BindException: Address alrea ...
- Web性能优化:雅虎35条
对web性能优化,一直知道是个很重要的方面,平时有注意到,但是对于雅虎35条是第一次听说,查了一下,发现平时都有用过,只是没有总结到一块,今天就总结一下吧. 雅虎35条: 1.[内容]尽量减少HTTP ...
- http服务端架构演进
摘要 在详解http报文相关文章中我们介绍了http协议是如何工作的,那么构建一个真实的网站还需要引入组件呢?一些常见的名词到底是什么含义呢? 什么叫正向代理,什么叫反向代理 服务代理与负载均衡的差别 ...
- 记录JS如何使用广度遍历找到节点的所有父节点
我们在实际的工作业务场景中经常遇到这样的场景,求取树数据中某个节点的父亲节点以及所有的父亲节点,这样的场景下不建议使用深度遍历,使用广度遍历可以更快找到. 1.案例解说 比如树的长相是这样的: 树的数 ...
- HTML5远程工具
因为有从网页直接远程其他windows电脑的需求,于是通过网上搜索找到下面几个解决方案,分享一下: 1.windows的远程桌面web连接tsweb 下载地址https://www.microsoft ...
- Cymothoa后门工具
Cymothoa是一款隐秘的后门工具. 发现网上对于Cymothoa的文章并不是很多,可是Cymothoa又是一款非常强大的后门工具,这里记录下Cymothoa的使用笔记. Cymothoa 是一款可 ...