分析问题
- 在后端数据需要的循环插入数据库中时,不要在实现类ServiceImpl中使用foreach语句
- 这样会导致每次插入一条数据就会查询一次数据库,导致性能极低
解决方法
- 在MyBatis中的Mapper.xml使用insert foreach实现数据的循环插入
- 这样就可以查询一次数据库,将所有数据循环插入
<insertid="saveList">
insert into
table (
id ,
column
)values<foreachcollection="list"item="item"index="index"separator=",">
(
#{item.id},
#{item.column}
)</foreach></insert>
总结
- 研究MyBatis框架,熟悉相关便捷开发的语法以及操作