mybatis-plus读取JSON类型的⽅法实现
摘要:mybatis-plus读取JSON类型。
本⽂总共三个步骤:
1、在数据库表定义JSON字段;
2、在实体类加上@TableName(autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = FastjsonTypeHandler.class);
3、建⼀些业务代码进⾏测试;
在数据库表定义JSON字段
CREATE TABLE `extra_info` (
`id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`extra_object` json NULL,
`extra_list` json NULL,
`extra_array` json NULL
);
INSERT INTO `extra_info` VALUES (1, '{\"id\": 1, \"name\": \"2\"}', '[{\"id\": 1, \"name\": \"2\"}]', '[{\"id\": 1, \"name\": \"2\"}]');
在实体类加上@TableName(autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = FastjsonTypeHandler.class)
import batisplus.annotation.IdType;
import batisplus.annotation.TableField;
import batisplus.annotation.TableId;
import batisplus.annotation.TableName;
import sion.handlers.FastjsonTypeHandler;
import java.io.Serializable;
import java.util.List;
@TableName(autoResultMap = true)
public class ExtraInfo implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
@TableField(typeHandler = FastjsonTypeHandler.class)
private ExtraNode extraObject;
@TableField(typeHandler = FastjsonTypeHandler.class)
private List<ExtraNode> extraList;
@TableField(typeHandler = FastjsonTypeHandler.class)
private ExtraNode[] extraArray;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public ExtraNode getExtraObject() {
return extraObject;
}
public void setExtraObject(ExtraNode extraObject) {
}
public List<ExtraNode> getExtraList() {
return extraList;
}
public void setExtraList(List<ExtraNode> extraList) {
}
public ExtraNode[] getExtraArray() {
return extraArray;
}
public void setExtraArray(ExtraNode[] extraArray) {
}
}
建⼀些业务代码进⾏测试
import java.io.Serializable;
public class ExtraNode implements Serializable {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import apper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface ExtraInfoMapper extends BaseMapper<ExtraInfo> {
}
import ditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private ExtraInfoMapper extraInfoMapper;
@GetMapping
public List<ExtraInfo> listAll() {
aInfoMapper.selectList(new LambdaQueryWrapper<>());
}
}
运⾏结果:
[
{
"id": 1,
"extraObject": { "id": 1, "name": "2" },
"extraList": [
{ "name": "2", "id": 1 }
],
"extraArray": [
{ "id": 1, "name": "2" }
fastjson常用方法]
}
]
到此这篇关于mybatis-plus读取JSON类型的⽅法实现的⽂章就介绍到这了,更多相关mybatis-plus读取JSON内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论