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 ...
随机推荐
- Hadoop点滴-外围概念
有句话说的好“大数据胜于好算法” 硬盘存储容量在不断提升的同时,访问速度(硬盘数据读取速度)却没有同步增长:1990年,访问全盘需要5分钟,20年后,需要2.5小时 不同的业务大数据,存储在一套HDF ...
- Android NDK(二) CMake构建工具进行NDK开发
本文目录 一Androidstudio中需要的插件 二项目配置 ①build.gardle配置 ②CMakeLists.txt ③Android和Cpp的代码 ④so文件生成 ⑤so文件的位置 一.A ...
- 【SpingBoot】spring静态工具类注入问题
package cn.zwqh.action; import javax.annotation.PostConstruct; import javax.annotation.Resource; imp ...
- angular之路由
一.核心问题 路由要解决的核心问题是通过建立url和页面之间的对应的关系,使不同的页面可以通过不用的url来展示. 首先,当用户在浏览器上输入URL后,Angular将获取该URL并将其解析生成一个U ...
- OEMCC 13.3 主机agent部署问题排查
部署安装 具体的安装过程可参考,Alfred Zhao的文章,非常详细,文章是OEMCC13.2的部署过程.OEMCC13.3没有太大差别. https://www.cnblogs.com/jyzha ...
- .NET Core 读取配置文件方式总结
基于.NET Core的跨平台开发,配置文件与之前.NET Framework采用xml的config文件不同,目前主要是采用json文件键值对配置方式读取. 参考网上相关资料总结如下: 一.引入扩展 ...
- bugku color
下载打开压缩包是七张图片,分别是七个颜色,使用stegsolve打开发现了异常. 七张图片拼起来是 make me tall,看来是要修改图片高度. 我们使用winhex打开图片并在十六进制中修改图片 ...
- Java 语言的发展史
维基百科引入 早期的Java 语言最开始只是Sun计算机(Sun MicroSystems)公司在1990年12月开始研究的一个内部项目.Sun计算机公司的一个叫做帕特里克·诺顿的工程师被公司自己开发 ...
- OSX 10.14.2 安装Cocoapods 出现问题的解决方法
今天尝试用 Cocoapods安装个第三方库.. 输入pod install, 发现 command not find. WTF! 估计是升级10.11后Cocoapods被干掉了. 我输入 sudo ...
- Tomcat基本知识(一)
顶层架构先上一张Tomcat的顶层结构图(图A),如下: Tomcat中最顶层的容器是Server,代表着整个服务器,从上图中可以看出,一个Server可以包含至少一个Service,用于具体提供服务 ...