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 ...
随机推荐
- Vue Element 导航子路由不选中问题
首先说下遇到的问题 1.进入嵌套路由,当前父导航无法高亮显示 2.页面刷新后导航重置问题 3.在嵌套路由刷新页面也会导致导航重置问题 接下来是解决方案: elementUI 里面有个属性 defaul ...
- centos7 远程连接其他服务器mysql
在本地远程连接 在终端输入: mysql -h 服务器ip地址 -P 端口 -u 用户名 -p 然后输入密码即可.
- .net core 3.0 Signalr - 07 业务实现-服务端 自定义管理组、用户、连接
Hub的管理 重写OnConnectedAsync 从连接信息中获取UserId.Groups,ConnectId,并实现这三者的关系,存放于redis中 代码请查看 using CTS.Signal ...
- 如何评价一个VR体验设计?
如何评价一个VR系统的体验是好是坏?或者说,哪些因素会破坏一个VR的体验? Kruij和Riecke教授在IEEE VR会议上提到了四个角度:Congnition,Game User Experien ...
- linux分析工具之vmstat详解
一.概述 vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.首先我们查看下帮助.如下图所 ...
- 快学Scala 第十六课 (shell调用,正则表达式,正则表达式组,stripMargin妙用)
shell调用:(管道符前加#号,执行shell用!) import sys.process._ "ls -al" #| "grep x" ! 正则表达式:(r ...
- HTML input 文本框输入中文逗号自动转换为英文逗号
input 标签中增加 onkeyup.onafterpaste 属性: <input type="text"placeholder="中文逗号自动转换为英文逗号& ...
- 设置VMWare CentOS7虚拟机上网(配置静态地址)
针对CentOS安装后设置虚拟机上网,参考网上相关资料和实际操作经验总结如下.本人亲测有效,进入主题. 设置虚拟机上网步骤 1.虚拟机设置->网络适配器 2.编辑->虚拟机网络编辑器-&g ...
- Cutting Sticks UVA - 10003
题文: 见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- WPF编程,C#中对话框自动关闭的一种方法(转载)
本文原文链接:https://blog.csdn.net/qq_43307934/article/details/84933196———————————————— MessageBoxTimeout是 ...