|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2014 [email protected] |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.github.abel533.mapperhelper; |
| 26 | + |
| 27 | +import org.apache.ibatis.executor.Executor; |
| 28 | +import org.apache.ibatis.executor.ExecutorException; |
| 29 | +import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; |
| 30 | +import org.apache.ibatis.mapping.MappedStatement; |
| 31 | +import org.apache.ibatis.reflection.MetaObject; |
| 32 | +import org.apache.ibatis.session.Configuration; |
| 33 | +import org.apache.ibatis.type.TypeHandler; |
| 34 | +import org.apache.ibatis.type.TypeHandlerRegistry; |
| 35 | + |
| 36 | +import java.sql.ResultSet; |
| 37 | +import java.sql.ResultSetMetaData; |
| 38 | +import java.sql.SQLException; |
| 39 | +import java.sql.Statement; |
| 40 | +import java.util.*; |
| 41 | + |
| 42 | +/** |
| 43 | + * 实现批量插入ID回写 |
| 44 | + */ |
| 45 | +public class MultipleJdbc3KeyGenerator extends Jdbc3KeyGenerator { |
| 46 | + @Override |
| 47 | + public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) { |
| 48 | + processBatch(ms, stmt, getParameters(parameter)); |
| 49 | + } |
| 50 | + |
| 51 | + public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) { |
| 52 | + ResultSet rs = null; |
| 53 | + try { |
| 54 | + rs = stmt.getGeneratedKeys(); |
| 55 | + final Configuration configuration = ms.getConfiguration(); |
| 56 | + final TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); |
| 57 | + final String[] keyProperties = ms.getKeyProperties(); |
| 58 | + final ResultSetMetaData rsmd = rs.getMetaData(); |
| 59 | + TypeHandler<?>[] typeHandlers = null; |
| 60 | + if (keyProperties != null && rsmd.getColumnCount() >= keyProperties.length) { |
| 61 | + for (Object parameter : parameters) { |
| 62 | + // there should be one row for each statement (also one for each parameter) |
| 63 | + if (!rs.next()) { |
| 64 | + break; |
| 65 | + } |
| 66 | + final MetaObject metaParam = configuration.newMetaObject(parameter); |
| 67 | + if (typeHandlers == null) { |
| 68 | + typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties); |
| 69 | + } |
| 70 | + populateKeys(rs, metaParam, keyProperties, typeHandlers); |
| 71 | + } |
| 72 | + } |
| 73 | + } catch (Exception e) { |
| 74 | + throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e); |
| 75 | + } finally { |
| 76 | + if (rs != null) { |
| 77 | + try { |
| 78 | + rs.close(); |
| 79 | + } catch (Exception e) { |
| 80 | + // ignore |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private Collection<Object> getParameters(Object parameter) { |
| 87 | + Collection<Object> parameters = null; |
| 88 | + if (parameter instanceof Collection) { |
| 89 | + parameters = (Collection) parameter; |
| 90 | + } else if (parameter instanceof Map) { |
| 91 | + Map parameterMap = (Map) parameter; |
| 92 | + if (parameterMap.containsKey("collection")) { |
| 93 | + parameters = (Collection) parameterMap.get("collection"); |
| 94 | + } else if (parameterMap.containsKey("list")) { |
| 95 | + parameters = (List) parameterMap.get("list"); |
| 96 | + } else if (parameterMap.containsKey("array")) { |
| 97 | + parameters = Arrays.asList((Object[]) parameterMap.get("array")); |
| 98 | + } |
| 99 | + } |
| 100 | + if (parameters == null) { |
| 101 | + parameters = new ArrayList<Object>(); |
| 102 | + parameters.add(parameter); |
| 103 | + } |
| 104 | + return parameters; |
| 105 | + } |
| 106 | + |
| 107 | + private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties) { |
| 108 | + TypeHandler<?>[] typeHandlers = new TypeHandler<?>[keyProperties.length]; |
| 109 | + for (int i = 0; i < keyProperties.length; i++) { |
| 110 | + if (metaParam.hasSetter(keyProperties[i])) { |
| 111 | + Class<?> keyPropertyType = metaParam.getSetterType(keyProperties[i]); |
| 112 | + TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType); |
| 113 | + typeHandlers[i] = th; |
| 114 | + } |
| 115 | + } |
| 116 | + return typeHandlers; |
| 117 | + } |
| 118 | + |
| 119 | + private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException { |
| 120 | + for (int i = 0; i < keyProperties.length; i++) { |
| 121 | + TypeHandler<?> th = typeHandlers[i]; |
| 122 | + if (th != null) { |
| 123 | + Object value = th.getResult(rs, i + 1); |
| 124 | + metaParam.setValue(keyProperties[i], value); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments