一、背景
在Java中,无法直接映射数据库中的JSON字段,只能通过字符串的方式进行映射,如果我们想把字符串数据转化为JSON数据,需要进行类型的转换,所以我们可以使用MyBatisPlus提供的JSON序列化器,可以直接将数据库中的JSON字段映射到实体类中。
二、准备工作
2.1 SpringBoot集成MyBatisPlus
2.2 创建数据库
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80013
Source Host : localhost:3306
Source Schema : spingboot-mybatis-demo
Target Server Type : MySQL
Target Server Version : 80013
File Encoding : 65001
Date: 18/01/2024 10:42:20
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for common
-- ----------------------------
DROP TABLE IF EXISTS `common`;
CREATE TABLE `common` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名',
`password` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
`phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号',
`info` json NULL COMMENT '详细信息',
`status` int(11) NULL DEFAULT NULL COMMENT '状态:0 停止 1开启',
`balance` int(11) NULL DEFAULT NULL COMMENT '账户余额',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of common
-- ----------------------------
INSERT INTO `common` VALUES (1, '张三', '123456', '18000000000', '{\"age\": 20, \"intro\": \"佛系青年\", \"gender\": \"male\"}', 1, 10000, '2024-01-18 10:24:47', '2024-01-18 10:24:47');
SET FOREIGN_KEY_CHECKS = 1;
二、定义实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class CommonInfo {
private Integer age;
private String intro;
private String gender;
}
@Data
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@TableName(value = "common", autoResultMap = true) // 开启结果集映射
public class Common {
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
private String password;
private String phone;
/**
* 设置JSON处理器
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private CommonInfo info;
private Integer status;
private Integer balance;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
注意:MyBatisPlus中没有在YML文件中配置全局JSON序列化器的方法,所以需要要使用的话,需要在指定字段上增加
typeHandler
进行类型转换,MyBatisPlus默认使用的是JacksonTypeHandler
序列化器,步骤如下:
- 第一步:在数据库中定义JSON类型的字段。
- 第二步:在指定的字段上增加
@TableField(typeHandler = JacksonTypeHandler.class)
注解进行类型的转换。- 第三步:在实体类上增加
autoResultMap = true
属性开始自动映射。
三、测试
@Slf4j
@SpringBootTest
public class JsonTypeTest {
@Autowired
private ICommonService commonService;
// 插入数据
@Test
public void insertTest() {
Common common = new Common();
common.setUsername("张三");
common.setPassword("123456");
common.setPhone("18000000000");
common.setInfo(CommonInfo.of(20, "佛系青年", "male"));
common.setStatus(1);
common.setBalance(10000);
common.setCreateTime(LocalDateTime.now());
common.setUpdateTime(LocalDateTime.now());
commonService.save(common);
}
// 查询数据
@Test
public void queryTest() {
Common common = commonService.getById(1);
Integer age = common.getInfo().getAge();
String gender = common.getInfo().getGender();
String intro = common.getInfo().getIntro();
log.info("年龄:{}", age);
log.info("gender:{}", gender);
log.info("intro:{}", intro);
}
}