[
{
"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文件
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 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("插入完成");
}
useGeneratedKeys :允许JDBC支持自动生成主键,需要驱动兼容。
如果设置为true则这个设置强制使用自动生成主键,尽管一些驱动不能兼容但仍可正常工作(比如 Derby)。
keyProperty :返回数据库自动生成的记录主键id 用 主键对应字段接收
INSERT INTO word(
word
,oldword
,strokes
,pinyin
,radicals
,explanation
,more
,created_by
,created_date
,last_updated_by
,last_updated_date
)VALUES
(
#{item.word}
,#{item.oldword}
,#{item.strokes}
,#{item.pinyin}
,#{item.radicals}
,#{item.explanation}
,#{item.more}
,'admin'
,NOW()
,'admin'
,NOW()
)