/** * Create a new MapSqlParameterSource based on a Map. * @param values a Map holding existing parameter values (can be {@code null}) */ public MapSqlParameterSource(@Nullable Map<String, ?> values) { addValues(values); }
@Test public void sqlParameterValueRegistersSqlType() throws Exception { MapSqlParameterSource msps = new MapSqlParameterSource("FOO", new SqlParameterValue(2, "Foo")); assertEquals("Correct SQL Type not registered", 2, msps.getSqlType("FOO")); MapSqlParameterSource msps2 = new MapSqlParameterSource(); msps2.addValues(msps.getValues()); assertEquals("Correct SQL Type not registered", 2, msps2.getSqlType("FOO")); }
/** * Create a new MapSqlParameterSource based on a Map. * @param values a Map holding existing parameter values (can be {@code null}) */ public MapSqlParameterSource(Map<String, ?> values) { addValues(values); }
/** * Create a new MapSqlParameterSource based on a Map. * @param values a Map holding existing parameter values (can be {@code null}) */ public MapSqlParameterSource(@Nullable Map<String, ?> values) { addValues(values); }
/** * 通过多个Map添加命名参数 * * @param mapParameter 多个Map参数 */ public SQL mapParameter(Map<String, Object>... mapParameter) { MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(); for (Map<String, Object> map : mapParameter) { mapSqlParameterSource.addValues(map); } this.sqlParameterSource = mapSqlParameterSource; return this; }
@Override public String generateQuery(String procName, Map<String, Object> args) { logger.info("Preparing query of proc {} with args {}", procName, args); String sql = sqlMap.get(procName); if (sql == null) { return null; } if (args != null) { MapSqlParameterSource msp = new MapSqlParameterSource(); msp.addValues(args); sql = super.generateQuery(sql, msp); } logger.info("Prepared query of proc {} with args {}: {}", new Object[] {procName, args, sql}); return sql; } }
public int update(String table, MapData entity, MapData keyAttributes, UpdateListener<MapData> listener){ if(listener != null) listener.before(entity); if(emptyStringAsNull) MapDataSQLKit.emptyStringAsNull(entity); //空串转为空(NULL) String sql = MapDataSQLKit.genUpdateSql(table,entity,keyAttributes,optimisticLockProperty,getNameConverter()); MapSqlParameterSource ps=new MapSqlParameterSource(entity); ps.addValues(keyAttributes); //执行 long startTime = System.currentTimeMillis(); int ret = jdbcTemplate.update(sql,ps); //使用了乐观锁,但是更新不到数据的情况下 if(StringKit.isNotBlank(optimisticLockProperty)&& ret==0){ throw new OptimisticLockException("SQL[{0}],Parameter:{1}",sql, JSONKit.toJsonString(entity)); } long endTime = System.currentTimeMillis(); logSQL("Update",sql,entity,1,ret,endTime-startTime); if(listener != null) listener.after(entity); return ret; } public int update(String table, MapData entity, MapData keyAttributes){
private <S, T> MapSqlParameterSource getPropertyMap(final S instance, RelationalPersistentEntity<S> persistentEntity, String prefix) { MapSqlParameterSource parameters = new MapSqlParameterSource(); PersistentPropertyAccessor<S> propertyAccessor = persistentEntity.getPropertyAccessor(instance); persistentEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> { if (property.isEntity() && !property.isEmbedded()) { return; } if(property.isEmbedded()){ Object value = propertyAccessor.getProperty(property); final RelationalPersistentEntity<?> embeddedEntity = context.getPersistentEntity(property.getType()); final MapSqlParameterSource additionalParameters = getPropertyMap((T)value, (RelationalPersistentEntity<T>) embeddedEntity, prefix + property.getEmbeddedPrefix()); parameters.addValues(additionalParameters.getValues()); } else { Object value = propertyAccessor.getProperty(property); Object convertedValue = converter.writeValue(value, ClassTypeInformation.from(property.getColumnType())); parameters.addValue(prefix + property.getColumnName(), convertedValue, JdbcUtil.sqlTypeFor(property.getColumnType())); } }); return parameters; }