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 {
            }
  • 相关阅读:
    一天进步一点点
    Flask
    Sqlalchemy 设置表编码及引擎
    threading.local
    xshell配置密码公钥登录
    linux 系统优化+定时任务
    linux命令
    xshell连接及优化
    linux前奏
    Vue Devtools--vue调式工具
  • 原文地址:https://www.cnblogs.com/liclBlog/p/15349562.html
Copyright © 2011-2022 走看看