-
开发环境
- mybatis版本3.2.5
- jdk 1.8
List importShootingList =new ArrayList();
testScoreSer.inserBatchTestScore(importShootingList);
service层
@Override
public void inserBatchTestScore(List list) throws SQLException {
shootingTestScoresDao.inserBatchTestScore(list);
}
dao层代码
传入一个list集合
void inserBatchTestScore(List list) throws SQLException;
mapper文件代码
其中parameterType填写java.util.List,代表一个list集合 insert into table (数据库字段名) values 为正常的sql文件的写法 之后写一个foreach 标签来进行批量的插入,foreach标签中的含义如下:
- collection代表遍历的集合
- item代表集合中每一个元素进行迭代时的别名,注意要和后面的#{}里面要一致,
- index指定一个名字,用于表示在迭代过程中,每次迭代到的位置.
- separator 指定遍历的元素之间使用的分隔符
- #{} 内为遍历的项目,注意item.的是pojo的属性,而不是数据库中的字段
- jdbcType为对应的数据库中的类型,注意后面的=号的内容必须为大写,否则无法插入成功
insert into t_shooting_test_scores (score_id, shooting_test_id, shooter_alarm,
shooter_name, shooting_test_time, shooting_test_place,
score, is_qualified,shooter_project,shooter_on_count)
values
(#{item.scoreId,jdbcType=VARCHAR}, #{item.shootingTestId,jdbcType=VARCHAR}, #{item.shooterAlarm,jdbcType=VARCHAR},
#{item.shooterName,jdbcType=VARCHAR}, #{item.shootingTestTime,jdbcType=TIMESTAMP}, #{item.shootingTestPlace,jdbcType=VARCHAR},
#{item.score,jdbcType=DOUBLE}, #{item.isQualified,jdbcType=INTEGER},#{item.shooterProject,jdbcType=VARCHAR},#{item.shooterOnCount,jdbcType=INTEGER})