mysql字段为json类型,mybatis load data导入怎么做
- 定义一个实体类来存储json数据。
public class JsonData {
private Integer id;
private JSONObject jsonData; // 使用JSONObject来存储json数据
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public JSONObject getJsonData() {
return jsonData;
}
public void setJsonData(JSONObject jsonData) {
this.jsonData = jsonData;
}
}
- 创建一个CSV文件,用于存储要导入的json数据。其中,一列是id,另一列是json数据。例如:
1,{"name": "张三", "age": 20, "gender": "male"}
2,{"name": "李四", "age": 25, "gender": "female"}
- 使用MyBatis提供的LOAD DATA语句来导入数据。例如:
<insert id="insertJsonData" parameterType="java.lang.String">
LOAD DATA LOCAL INFILE #{filePath}
INTO TABLE json_data
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(@id, @jsonData)
SET id = @id, json_data = @jsonData;
</insert>
- 在Java代码中调用Mapper来执行导入操作。例如:
@Autowired
private JsonDataMapper jsonDataMapper;
public void importJsonData(String filePath) {
jsonDataMapper.insertJsonData(filePath);
}
- 运行程序,即可将CSV文件中的json数据导入到MySQL的json字段中。
原文地址: https://www.cveoy.top/t/topic/zR4 著作权归作者所有。请勿转载和采集!