• 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);
  • 相关阅读:
    cxVerticalGrid赋值是实时更新
    cxGrid类似pagecontrol的效果
    cxgrid属性说明,每次用的时候费时费力查找。
    做一个平均数,合计数的sql查询
    FDMemTable 提示操作速度尤其是循环
    手机号码验证
    取字符串长度
    DELPHI 获取本月 的第一天 和 最后一天
    SQL Server 断开某个数据库所有连接(还原的时候需要)转自用保留
    元宇宙六大核心技术
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6510377.html
Copyright © 2020-2023  润新知