JSON格式的文件转换对象存入数据库

JSON文件中的格式

[
	{
        "word": "嗄",
        "oldword": "嗄",
        "strokes": "13",
        "pinyin": "á",
        "radicals": "口",
        "explanation": "嗄〈叹〉\n\n 同啊”。表示省悟或惊奇",
        "more": "嗄 ga、a 部首 口 部首笔画 03 总笔画 13  "
 	 },
	 {
        "word": "吖",
        "oldword": "吖",
        "strokes": "6",
        "pinyin": "ā",
        "radicals": "口",
        "explanation": "喊叫天~地。",
        "more": "吖 a 部首 口 部首笔画 03 总笔画 06  "
	 }
]

读取JSON格式的文件,返回字符串

//从给定位置读取Json文件
public String readJson(String path){
    //从给定位置获取文件
    File file = new File(path);
    BufferedReader reader = null;
    //返回值,使用StringBuffer
    StringBuffer data = new StringBuffer();
    //
    try {
        reader = new BufferedReader(new FileReader(file));
        //每次读取文件的缓存
        String temp = null;
        while((temp = reader.readLine()) != null){
            data.append(temp);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        //关闭文件流
        if (reader != null){
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data.toString();//返回字符串
}

//插入方法
public voidinsert() throws IOException {
	
	List<WordModel> wordModelList = new ArrayList<>();
	// 开始时间
    Long begin = new Date().getTime();
	Integer result = 0;
	String word = readJson("C:\\Users\\zhaohua\\Downloads\\word.json");
	wordModelList = JSONObject.parseArray(word, WordModel.class);//把字符串形式的数组转换成jsonArray 然后转换为List
	for (WordModel wordModel : wordModelList) {//增强for
		wordModel.getWord().replace(" ", "");//去除string中的空字符串
		wordModel.getOldword().replace(" ", "");
		wordModel.getStrokes().replace(" ", "");
		wordModel.getPinyin().replace(" ", "");
		wordModel.getRadicals().replace(" ", "");
	}
	wxdictionaryMapper.insert(wordModelList);//插入数据库
	 // 结束时间
    Long end = new Date().getTime();
    // 耗时
    System.out.println("数据插入花费时间 : " + (end - begin) / 1000 + " s");
    System.out.println("插入完成");
}

链接奉上: 深入浅出mybatis之useGeneratedKeys参数用法

useGeneratedKeys :允许JDBC支持自动生成主键,需要驱动兼容。
如果设置为true则这个设置强制使用自动生成主键,尽管一些驱动不能兼容但仍可正常工作(比如 Derby)。
keyProperty :返回数据库自动生成的记录主键id 用 主键对应字段接收

链接奉上:mybatis之foreach用法

	<insert id="insert" useGeneratedKeys="true" keyProperty="wordId" parameterType="java.util.List">
		INSERT INTO word(
			word
			,oldword
			,strokes
			,pinyin
			,radicals
			,explanation
			,more
			,created_by
			,created_date
			,last_updated_by
			,last_updated_date
		)VALUES
		<foreach collection="list" item="item" index="index" separator=",">
		(
			#{item.word}
			,#{item.oldword}
			,#{item.strokes}
			,#{item.pinyin}
			,#{item.radicals}
			,#{item.explanation}
			,#{item.more}
			,'admin'
			,NOW()
			,'admin'
			,NOW()
		)
		</foreach>
	</insert>

请添加图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值