@Options(useGeneratedKeys = true, keyProperty = "id") @Insert("insert into country (countryname,countrycode) values (#countryname,#countrycode)") int insertBean(Country country);
@Options(useGeneratedKeys = true, keyProperty = "id") @Insert("insert into country (countryname,countrycode) values (#country.countryname,#country.countrycode)") int insertNamedBean(@Param("country") Country country);
<2> 处,如下可以符合这个条件。代码如下:
@Options(useGeneratedKeys = true, keyProperty = "country.id") @Insert("insert into country (countryname, countrycode) values (#country.countryname, #country.countrycode)") int insertMultiParams_keyPropertyWithWrongParamName2(@Param("country") Country country, @Param("someId") Integer someId);
如果是这个情况,获得的主键,会设置回 country 的 id 属性,因为注解上的 keyProperty = "country.id" 配置。
?? 此处比较绕,也相对用的少。
<3> 处,如下可以符合这个条件。代码如下:
@Options(useGeneratedKeys = true, keyProperty = "id") @Insert("insert into country (countryname, countrycode) values (#country.countryname, #country.countrycode)") int insertMultiParams_keyPropertyWithWrongParamName3(@Param("country") Country country);
// Jdbc3KeyGenerator.java protected void assignKeysToOneOfParams(final Configuration configuration, ResultSet rs, final String[] keyProperties, Map<?, ?> paramMap) throws SQLException // Assuming ‘keyProperty‘ includes the parameter name. e.g. ‘param.id‘. // <1> 需要有 `.` 。 int firstDot = keyProperties[0].indexOf(‘.‘); if (firstDot == -1) throw new ExecutorException( "Could not determine which parameter to assign generated keys to. " + "Note that when there are multiple parameters, ‘keyProperty‘ must include the parameter name (e.g. ‘param.id‘). " + "Specified key properties are " + ArrayUtil.toString(keyProperties) + " and available parameters are " + paramMap.keySet()); // 获得真正的参数值 String paramName = keyProperties[0].substring(0, firstDot); Object param; if (paramMap.containsKey(paramName)) param = paramMap.get(paramName); else throw new ExecutorException("Could not find parameter ‘" + paramName + "‘. " + "Note that when there are multiple parameters, ‘keyProperty‘ must include the parameter name (e.g. ‘param.id‘). " + "Specified key properties are " + ArrayUtil.toString(keyProperties) + " and available parameters are " + paramMap.keySet()); // Remove param name from ‘keyProperty‘ string. e.g. ‘param.id‘ -> ‘id‘ // 获得主键的属性的配置 String[] modifiedKeyProperties = new String[keyProperties.length]; for (int i = 0; i < keyProperties.length; i++) if (keyProperties[i].charAt(firstDot) == ‘.‘ && keyProperties[i].startsWith(paramName)) modifiedKeyProperties[i] = keyProperties[i].substring(firstDot + 1); else throw new ExecutorException("Assigning generated keys to multiple parameters is not supported. " + "Note that when there are multiple parameters, ‘keyProperty‘ must include the parameter name (e.g. ‘param.id‘). " + "Specified key properties are " + ArrayUtil.toString(keyProperties) + " and available parameters are " + paramMap.keySet()); // 设置主键们,到参数 param 中 assignKeysToParam(configuration, rs, modifiedKeyProperties, param);
// SelectKeyGenerator.java private void handleMultipleProperties(String[] keyProperties, MetaObject metaParam, MetaObject metaResult) String[] keyColumns = keyStatement.getKeyColumns(); // 遍历,进行赋值 if (keyColumns == null || keyColumns.length == 0) // no key columns specified, just use the property names for (String keyProperty : keyProperties) setValue(metaParam, keyProperty, metaResult.getValue(keyProperty)); else if (keyColumns.length != keyProperties.length) throw new ExecutorException("If SelectKey has key columns, the number must match the number of key properties."); for (int i = 0; i < keyProperties.length; i++) setValue(metaParam, keyProperties[i], metaResult.getValue(keyColumns[i]));