摘要:mybatis-plus读取JSON类型。
本文总共三个步骤:
1、在数据库表定义JSON字段;
2、在实体类加上@TableName(autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = FastjsonTypeHandler.class);
3、建一些业务代码进行测试;
在数据库表定义JSON字段
1
2
3
4
5
6
7
8
|
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.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) { this .extraObject = extraObject; } public List<ExtraNode> getExtraList() { return extraList; } public void setExtraList(List<ExtraNode> extraList) { this .extraList = extraList; } public ExtraNode[] getExtraArray() { return extraArray; } public void setExtraArray(ExtraNode[] extraArray) { this .extraArray = extraArray; } } |
建一些业务代码进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.springframework.stereotype.Repository; @Repository public interface ExtraInfoMapper extends BaseMapper<ExtraInfo> { } import com.baomidou.mybatisplus.core.conditions.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() { return this .extraInfoMapper.selectList( new LambdaQueryWrapper<>()); } } |
运行结果:
[
{
"id": 1,
"extraObject": { "id": 1, "name": "2" },
"extraList": [
{ "name": "2", "id": 1 }
],
"extraArray": [
{ "id": 1, "name": "2" }
]
}
]
到此这篇关于mybatis-plus读取JSON类型的方法实现的文章就介绍到这了,更多相关mybatis-plus读取JSON内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/liangweiping/p/12835377.html