mybatis plus實體類中字段映射mysql中的json格式方式
mybatis plus實體類中字段映射mysql中的json格式
1.實體類中有個屬性是其他對象
或者是List;在數(shù)據(jù)庫中存儲時使用的是mysql的json格式,此時可以用mybatis plus的一個注解
@TableField(typeHandler = JacksonTypeHandler.class)

@TableField(typeHandler = JacksonTypeHandler.class)
這樣在存入是就可以把對象自動轉(zhuǎn)換為json格式,
2.那么取出時怎么進行映射呢,有分為兩種情況
- a:當(dāng)沒有使用到xml時:

@Data @Accessors(chain = true) @TableName(value = "wx_user",autoResultMap = true)
- b:當(dāng)使用了xml文件時:

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" }
]
}
]
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaEE開發(fā)基于Eclipse的環(huán)境搭建以及Maven Web App的創(chuàng)建
本文主要介紹了如何在Eclipse中創(chuàng)建的Maven Project,本文是JavaEE開發(fā)的開篇,也是基礎(chǔ)。下面內(nèi)容主要包括了JDK1.8的安裝、JavaEE版本的Eclipse的安裝、Maven的安裝、Tomcat 9.0的配置、Eclipse上的M2Eclipse插件以及STS插件的安裝。2017-03-03
Java調(diào)用Python代碼的幾種方法小結(jié)
Python語言有豐富的系統(tǒng)管理、數(shù)據(jù)處理、統(tǒng)計類軟件包,因此從java應(yīng)用中調(diào)用Python代碼的需求很常見、實用,本文介紹幾種方法從java調(diào)用Python代碼,從而最大化利用兩個語言的優(yōu)勢,需要的朋友可以參考下2025-01-01
SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問題
Java直接內(nèi)存和堆內(nèi)存的關(guān)系

