mybatis-plus讀取JSON類型的方法實現(xiàn)
摘要:mybatis-plus讀取JSON類型。
本文總共三個步驟:
1、在數(shù)據(jù)庫表定義JSON字段;
2、在實體類加上@TableName(autoResultMap = true)、在JSON字段映射的屬性加上@TableField(typeHandler = FastjsonTypeHandler.class);
3、建一些業(yè)務(wù)代碼進行測試;
在數(shù)據(jù)庫表定義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 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; } }
建一些業(yè)務(wù)代碼進行測試
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 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<>()); } }
運行結(jié)果:
[
{
"id": 1,
"extraObject": { "id": 1, "name": "2" },
"extraList": [
{ "name": "2", "id": 1 }
],
"extraArray": [
{ "id": 1, "name": "2" }
]
}
]
到此這篇關(guān)于mybatis-plus讀取JSON類型的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)mybatis-plus讀取JSON內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中Shiro緩存使用Redis、Ehcache的方法
這篇文章主要介紹了SpringBoot中Shiro緩存使用Redis、Ehcache的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09MyBatis通用Mapper中的通用example(排序)詳解
這篇文章主要介紹了MyBatis通用Mapper中的通用example(排序)詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12