zoukankan      html  css  js  c++  java
  • hibernate批量插入

      // 从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 {
            }
  • 相关阅读:
    MySQL系列(二)
    MySQL系列(一)
    RabbitMQ的安装部署
    RabbitMQ原理介绍
    消息中间件metaq
    消息中间件之zookper安装部署
    ZooKeeper基本原理
    消息中间件剖析
    了解Node.js
    windows下使用Git
  • 原文地址:https://www.cnblogs.com/liclBlog/p/15349562.html
Copyright © 2011-2022 走看看