• 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);
  • 相关阅读:
    Portal 并查集(去重)
    Constructing Roads 最小生成树(prime()),注意边的处理方式
    More is better 并查集最原始 最有效的形式,用rank数组,(结构体)
    【集训试题】SiriusRen的卡牌 set
    【集训试题】exam 信心考 最小割
    [leetcode] Binary Tree Pruning
    [leetcode] Daily Temperatures
    [leetcode] Sort Characters By Frequency
    [leetcode] Subdomain Visit Count
    [leetcode] Number of Boomerangs
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6510377.html
Copyright © 2020-2023  润新知