springboot读取json文件并存入数据库
·
1、json格式形如
[
{
"word": "嗄",
"oldword": "嗄",
"strokes": "13",
"pinyin": "á",
"radicals": "口",
"explanation": "嗄〈叹〉\n\n 同啊",
"more": "嗄 ga、a 部首 口 部首笔画 03 总笔画 13"
},
{
"word": "吖",
"oldword": "吖",
"strokes": "6",
"pinyin": "ā",
"radicals": "口",
"explanation": "喊叫天~地。\n 形容喊叫的声音高声叫~~。",
"more": "吖 a 部首 口 部首笔画 03 总笔画 06 吖2\nyā\n喊,呼喊"
}
]
2、创建对应实体类
public class Chinese {
private Integer id;
private String word;
private String oldword;
private String strokes;
private String pinyin;
private String radicals;
private String explanation;
private String more;
//getter、setter方法省略
}
3、Controller方法
@GetMapping("/initChinese")
public String initChinese(){
try {
String filePathh = "D:/word.json";//json文件地址
InputStream inputStream = new FileInputStream(filePathh);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer sb = new StringBuffer();
String line;
while ((line=br.readLine())!=null){
sb.append(line);
}
List<Chinese> poets = JSON.parseArray(sb.toString(), Chinese.class);
for (Chinese poet : poets) {
chineseMapper.insert(poet);//调用mapper接口
}
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐

所有评论(0)