您当前的位置: 首页 >  mybatis

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Mybatis批量插入

java持续实践 发布时间:2018-09-07 21:44:08 ,浏览量:2

  • 开发环境

    • mybatis版本3.2.5
    • jdk 1.8
使用mybatis批量插入list集合数据 Controller层
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})
    
    

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0397s