1、插入一条记录返回刚插入记录的id

public int addBean(final Bean b){  final String strSql = "insert into buy(id,c,s,remark,line,cdatetime," +  "c_id,a_id,count,type) values(null,?,?,?,?,?,?,?,?,?)";  KeyHolder keyHolder = new GeneratedKeyHolder();  this.getJdbcTemplate().update(  new PreparedStatementCreator(){  public java.sql.PreparedStatement createPreparedStatement(Connection conn) throws SQLException{  int i = 0;  java.sql.PreparedStatement ps = conn.prepareStatement(strSql);   ps = conn.prepareStatement(strSql, Statement.RETURN_GENERATED_KEYS);  ps.setString(++i, b.getC());  ps.setInt(++i,b.getS() );  ps.setString(++i,b.getR() );  ps.setString(++i,b.getline() );  ps.setString(++i,b.getCDatetime() );  ps.setInt(++i,b.getCId() );  ps.setInt(++i,b.getAId());  ps.setInt(++i,b.getCount());  ps.setInt(++i,b.getType());  return ps;  }  },  keyHolder);  return keyHolder.getKey().intValue();  }  

2、批量插入数据:

public void addBuyBean(List<BuyBean> list)   {   final List<BuyBean> tempBpplist = list;   String sql="insert into buy_bean(id,bid,pid,s,datetime,mark,count)" +  " values(null,?,?,?,?,?,?)";   this.getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter() {  @Override  public int getBatchSize() {  return tempBpplist.size();   }  @Override  public void setValues(PreparedStatement ps, int i)  throws SQLException {  ps.setInt(1, tempBpplist.get(i).getBId());   ps.setInt(2, tempBpplist.get(i).getPId());   ps.setInt(3, tempBpplist.get(i).getS());   ps.setString(4, tempBpplist.get(i).getDatetime());   ps.setString(5, tempBpplist.get(i).getMark());                   ps.setInt(6, tempBpplist.get(i).getCount());  }   });   }  
public static void batchInsert() throws ClassNotFoundException, SQLException{long start = System.currentTimeMillis();Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/kxh?useServerPrepStmts=false&rewriteBatchedStatements=true","root", "root");connection.setAutoCommit(false);PreparedStatement cmd = connection.prepareStatement("insert into test1 values(?,?)");for (int i = 0; i < 1000000; i++) {//100万条数据cmd.setInt(1, i);cmd.setString(2, "test");cmd.addBatch();if(i%1000==0){cmd.executeBatch();}}cmd.executeBatch();connection.commit();cmd.close();connection.close();long end = System.currentTimeMillis();System.out.println("批量插入需要时间:"+(end - start)); //批量插入需要时间:24675}

3、批量插入并返回批量id(由于JDBCTemplate不支持批量插入后返回批量id,所以此处使用jdbc原生的方法实现此功能)

public List<Integer> addProduct(List<ProductBean> expList) throws SQLException {  final List<ProductBean> tempexpList = expList;  String sql="insert into product(id,s_id,status,datetime,"  + " count,o_id,reasons"  + " values(null,?,?,?,?,?,?)";  DbOperation dbOp = new DbOperation();  dbOp.init();  Connection con = dbOp.getConn();  con.setAutoCommit(false);  PreparedStatement pstmt = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);  for (ProductBean n : tempexpList) {  pstmt.setInt(1,n.getSId());     pstmt.setInt(2,n.getStatus());   pstmt.setString(3,n.getDatetime());   pstmt.setInt(4,n.getCount());  pstmt.setInt(5,n.getOId());  pstmt.setInt(6,n.getReasons());  pstmt.addBatch();  }  pstmt.executeBatch();   con.commit();     ResultSet rs = pstmt.getGeneratedKeys(); //获取结果  List<Integer> list = new ArrayList<Integer>();   while(rs.next()) {  list.add(rs.getInt(1));//取得ID  }  con.close();  pstmt.close();  rs.close();  return list;  }  

spring JDBCTemplate实现批量插入及返回id相关推荐

  1. Spring利用JDBCTemplate实现批量插入和返回id

    1.先介绍一下java.sql.Connection接口提供的三个在执行插入语句后可取的自动生成的主键的方法: //第一个是 PreparedStatement prepareStatement(St ...

  2. mybatis的插入与批量插入的返回ID的原理

    文章目录 背景 底层调用方法 单个对象插入 直接保存实体的对象作为参数传入(给伪代码示例) 多个对象,实体对象作为其中一个对象传入 列表批量插入 直接保存实体的对象作为参数传入(给伪代码示例) 多个对 ...

  3. Mybatis批量插入,返回主键ID不成功,巨坑

    一.场景说明 批量插入,返回主键ID报错 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibati ...

  4. 继解决Spring data jpa 批量插入重写saveAll()后遇到符号不兼容问题

    问题描述 问题: 之前为解决Spring data jpa 批量插入/删除(saveAll()/deleteAll())速度慢的问题 重写了saveAll()方法,用自定义拼接sql的方法组装sql, ...

  5. mysql批量插入没有返回主键id

    在大批量新增数据的时候往往会采用批量插入来提高效率,但是经常遇到没有返回主键的情况. 一般会有以下几种可能: 1.升级Mybatis版本到3.3.1.官方在这个版本中加入了批量新增返回主键id的功能 ...

  6. 解决Spring data jpa 批量插入/删除(saveAll()/deleteAll())速度慢的问题

    问题描述: 项目中使用到了Spring data jpa技术,调用 JpaRepository.saveAll()/deleteAll()方法对list中的数据进行插入/删除时,发现速度特别慢,数据量 ...

  7. oracle批量插入并且返回自增主键_mybatis + (oracle)实现主键自增 + 插入数据并返回主键...

    一.实现主键自增 在oracle数据库中,主键并没有办法自动增长,无法使用insert对应的useGeneratedKeys和keyProperty属性自动返回增加的主键. 要实现自增需要修改 ID列 ...

  8. oracle批量插入并且返回自增主键_oracle 自增主键实现批量更新和增加sql

    增加: 方案1 INSERT INTO WF_TASKTEMP_DEALSTAFF (DEAL_ID, TEMP_ID, STAFF_ID,DEAL_TYPE, STATUS) SELECT WF_T ...

  9. Mybatis Mysql 批量插入返回id

    1. 目标: 使用Mybatis 批量插入数据返回自增的id 2. 具体实现 注意: MyBatis版本3.3.1或者以上 <insert id="save" useGene ...

最新文章

  1. 【号外号外:微软收购 .NET 的开源实现 Xamarin 项目的公司】
  2. 赛门铁克公布Q3财报 亏损68亿美元
  3. 【Linux】10.安装和开启ftp服务
  4. 机器学习导论(张志华):概率PCA
  5. C语言经典算法100例
  6. html页面中Location对象跳转页面用法
  7. 【ECharts系列|02可视化大屏】 舆情分析,人口分析及警情警力活动情况的实现【上篇】
  8. vue怎么获取用户的位置经纬度_vue 实现Web端的定位功能 获取经纬度
  9. XShell笔记-XShell登录脚本的使用
  10. JAVA 版本微信公众管理开源项目招募伙伴
  11. FilterAttribute过滤器的 执行顺序
  12. Linux运维第一课----服务器硬件
  13. 5.Linux/Unix 系统编程手册(上) -- 深入探究文件IO
  14. Android拍照返回图片
  15. FAT32 文件系统详解
  16. 3西格玛计算公式_六西格玛相关参数及计算公式
  17. 直线绘制算法-中点画线法
  18. 音频变声原理 附简单示例代码
  19. 我的IOS端SIP电话开发历程
  20. 区分QA和QC, Verification和Validation

热门文章

  1. 《数据结构》实验报告七:查找
  2. WinForm(八)窗体,窗体
  3. RedHat7.4安装
  4. 学习产品基础知识学习沟通-每日打卡
  5. (8)Artemis检测(僵尸连接、慢消费者、代理异常)
  6. 自动查券找券搜券返利机器人实现方法分享
  7. 从顺丰到菜鸟,洋女婿“爆改”俄罗斯邮政
  8. 9.String类 StringBuffer/StringBuilder 的定义
  9. DotProject首页、文档和下载 - 项目管理工具 - 开源中国社区
  10. 2022高处安装、维护、拆除考试模拟100题及模拟考试