Hibernate、JDBC之批量插入

<!- Hibernate 批量插入 -->

public void batchAddSendCus(final List<ShpSendVouchDto> shpSendVouchs) throws Exception {
        super.getHibernateTemplate().execute(new HibernateCallback(){
            public Object doInHibernate(Session session){
                int rows = 0;

                  for(ShpSendVouchDto dto: shpSendVouchs)
                  {
                       session.save(dto);
                       rows++;
                       if ( rows  % 10000 == 0 ) {                   

                        //flush a batch of inserts and release memory:
                         //将本批插入的对象立即写入数据库并释放内存
                       session.flush();
                       session.clear();
                       }
                  }

           return new Integer(rows);
            }
          });

   }

ShpSendVouchDto为要插入的对向

List<ShpSendVouchDto> shpSendVouchs插入的对向的集合

 

 

------------------------------------------------------------------------------

<!-JDBC批量插入-->

 

public void batchAddSendCus(final List<ShpSendVouchDto> shpSendVouchs) throws Exception {

       
   if (shpSendVouchs != null) {


(数据库针对MySql)

             String sql = " insert into SHPSENDVOUCH(vouchid,vouchmodelid,Cusid,shopid) values(?,?,?,?) ";

(数据库针对Oracle,SEQ_SHP_SENDVOUCH.NEXTVAL为ORACLE中的序列)

    //      String sql = " insert into shpsendvouch(id,vouchid,vouchmodelid,Cusid,shopid)values(SEQ_SHP_SENDVOUCH.NEXTVAL,?,?,?,?) ";


            this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    String vouchid = shpSendVouchs.get(i).getVouchid();
                    Long vouchmodelid = shpSendVouchs.get(i).getVouchmodelid();
                    Long cusid = shpSendVouchs.get(i).getCusid();
                    Long shopid = shpSendVouchs.get(i).getShopid();
                    Long difstatus = shpSendVouchs.get(i).getDifstatus();
                    String vouchpassword = shpSendVouchs.get(i).getVouchpassword();
                    Date gettime = shpSendVouchs.get(i).getGettime();
                    Long status = shpSendVouchs.get(i).getStatus();
                    ps.setString(1, vouchid);
                    ps.setLong(2, vouchmodelid);
                    ps.setLong(3, cusid);
                    ps.setLong(4, shopid);
                }

                public int getBatchSize() {
                    return shpSendVouchs.size();
                }
            });
        }

ShpSendVouchDto为要插入的对向

List<ShpSendVouchDto> shpSendVouchs插入的对向的集合

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值