hibernate批量插入

版权声明:本文为博主原创文章,转载请注明出处!!! https://blog.csdn.net/qq_32711309/article/details/83793071
  // 从hibernate获取连接,并用doWork进行原声jdbc操作,这样事务管理机制就是使用的一个Transaction
        Transaction transaction = null;
        Session currentSession = null;
        try {
            currentSession = this.getSessionFactory().getCurrentSession();
            transaction = currentSession.getTransaction();
            currentSession.doWork(new Work() {
                @Override
                public void execute(Connection connection) throws SQLException {
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
//业务逻辑处理
                    for (Integer carId : carIds) {
                        preparedStatement.setInt(1,primaryKeyId);
                        preparedStatement.setInt(2,carId);
                        preparedStatement.addBatch();
                    }
                    preparedStatement.executeBatch();
                }
            });
//            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
            throw new SQLException();
        }finally {
        }

猜你喜欢

转载自blog.csdn.net/qq_32711309/article/details/83793071