zoukankan      html  css  js  c++  java
  • sql 更新 批量更新 更新得到主键

    import org.springframework.dao.InvalidDataAccessApiUsageException;
    import org.springframework.jdbc.core.BatchPreparedStatementSetter;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.PreparedStatementCreator;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.jdbc.support.GeneratedKeyHolder;
    import org.springframework.jdbc.support.KeyHolder;
    
    
        public void updateMaterialForQM(final MaterialProxy materialProxy) throws Exception {
            jdbcTemplate.getJdbcOperations().update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                    StringBuilder sql = new StringBuilder("update riv_material set");
                    if (null != materialProxy.getRivMaterial().getMatCode()) {
                        sql.append(" mat_code = ?,");
                    }
                    if (null != materialProxy.getRivMaterial().getMatName()) {
                        sql.append(" mat_name = ?,");
                    }
                    
                    if (-1 == sql.indexOf("?")) {
                        return null;
                    }
                    String finalSql = sql.substring(0, sql.length() - 1) + " where mat_id = ?";
                    CommonMethodHelper.showDebugLogger(logger, finalSql);
                    PreparedStatement ps = con.prepareStatement(finalSql);
                    int i = 1;
                    if (StringUtil.isNotBlank(materialProxy.getRivMaterial().getMatCode())) {
                        ps.setString(i, materialProxy.getRivMaterial().getMatCode());
                        i++;
                    }
                    if (null != materialProxy.getRivMaterial().getMatName()) {
                        ps.setString(i, materialProxy.getRivMaterial().getMatName());
                        i++;
                    }
                    
                    ps.setInt(i, materialProxy.getRivMaterial().getMatId());
                    return ps;
                }
            });
    
        }
    import org.springframework.dao.InvalidDataAccessApiUsageException;
    import org.springframework.jdbc.core.BatchPreparedStatementSetter;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.PreparedStatementCreator;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.jdbc.support.GeneratedKeyHolder;
    import org.springframework.jdbc.support.KeyHolder;
    
    
        private void updateMaterialOPForQM(MaterialProxy materialProxy) throws Exception {
            if (CommonMethodHelper.isCollectionValid(materialProxy.getRivMaterialOptions())) {
                int size = materialProxy.getRivMaterialOptions().size();
                Object[] objects = new Object[size];
                for (int i = 0; i < size; i++) {
                    RivMaterialOption mto = materialProxy.getRivMaterialOptions().get(i);
                    objects[i] = new Object[] {
                            mto.getMtoOptionValue(), materialProxy.getRivMaterial().getMatId(), mto.getMtoCode()};
                }
                batchUpdateNotAutoCommit("update riv_material_option set mto_option_value = ? where mto_material_id = ? and mto_code = ?;", size, objects);
            }
        }
        /***
         * 批量更新 (不自动提交事务)
         * @param sql
         * @param updateCount
         * @param paramValue
         * @return
         * @throws Exception
         */
        @Override
        public int[] batchUpdateNotAutoCommit(String sql, final int updateCount, final Object... paramValue) throws Exception {
            BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
                @Override
                public int getBatchSize() {
                    return updateCount;
                }
    
                @Override
                public void setValues(PreparedStatement ps, int index) throws SQLException {
                    // 设置参数值
                    Object[] object = (Object[]) paramValue[index];
                    for (int j = 0; j < object.length; j++) {
                        ps.setObject(j + 1, object[j]);
                    }
                }
            };
            int[] results = jdbcTemplate.getJdbcOperations().batchUpdate(sql, setter);
            return results;
        }
    import org.springframework.jdbc.core.PreparedStatementCreator;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.jdbc.support.GeneratedKeyHolder;
    
    
    KeyHolder keyHolder = new GeneratedKeyHolder();
            jdbcTemplate.getJdbcOperations().update(new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                    return buildOutNotceHeaderPS(onh, con);
                }
            }, keyHolder);
  • 相关阅读:
    从jdbc到分层的飞跃
    第二章 变量和数据类型
    s1300新学期伊始的我们
    选择结构总结
    第四章 选择结构(二) Switch结构
    第三章 选择结构(一)
    第二章 变量、数据类型和运算符
    使用Java理解程序逻辑 第1章 初识Java 基本部分
    ES命令基础
    Spring MVC拦截器
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6510377.html
Copyright © 2011-2022 走看看