1. 程式人生 > >mybatis源碼解析11---ParameterHandler解析

mybatis源碼解析11---ParameterHandler解析

ask 處理器 plugin ride tor iba tin sadd mapped

ParameterHandler接口是參數處理器,位於mybatis包的org.apache.ibatis.executor.parameter下,源碼如下:

1 public interface ParameterHandler {
2 
3 Object getParameterObject();//獲取參數
4 
5 void setParameters(PreparedStatement ps)//設置參數
6 throws SQLException;
7 
8 }

可見ParameterHandler接口只有簡單的兩個方法,一個是獲取參數一個是設置參數。ParameterHandler接口默認實現類是DefaultParameterHandler,主要源碼如下:

 1 public class DefaultParameterHandler implements ParameterHandler {
 2 
 3 private final TypeHandlerRegistry typeHandlerRegistry;
 4 
 5 private final MappedStatement mappedStatement;
 6 private final Object parameterObject;
 7 private BoundSql boundSql;
 8 private Configuration configuration;
9 10 public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { 11 this.mappedStatement = mappedStatement; 12 this.configuration = mappedStatement.getConfiguration(); 13 this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
14 this.parameterObject = parameterObject;//設置參數 15 this.boundSql = boundSql; 16 } 17 18 @Override 19 public Object getParameterObject() { 20 return parameterObject;//返回參數 21 } 22 23 @Override 24 public void setParameters(PreparedStatement ps) { 25 ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); 26 List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();//獲取所有參數,ParameterMapping是jdbc和java類型之間的對應關系 27 if (parameterMappings != null) { 28 //遍歷所有參數,將java 類型設置成jdbc類型 29 for (int i = 0; i < parameterMappings.size(); i++) { 30 ParameterMapping parameterMapping = parameterMappings.get(i); 31 if (parameterMapping.getMode() != ParameterMode.OUT) { 32 Object value; 33 String propertyName = parameterMapping.getProperty(); 34 if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params 35 value = boundSql.getAdditionalParameter(propertyName); 36 } else if (parameterObject == null) { 37 value = null; 38 } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { 39 value = parameterObject; 40 } else { 41 MetaObject metaObject = configuration.newMetaObject(parameterObject); 42 value = metaObject.getValue(propertyName); 43 } 44 TypeHandler typeHandler = parameterMapping.getTypeHandler(); 45 JdbcType jdbcType = parameterMapping.getJdbcType(); 46 if (value == null && jdbcType == null) { 47 jdbcType = configuration.getJdbcTypeForNull(); 48 } 49 try { 50 typeHandler.setParameter(ps, i + 1, value, jdbcType); 51 } catch (TypeException e) { 52 throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e); 53 } catch (SQLException e) { 54 throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e); 55 } 56 } 57 } 58 } 59 } 60 61 }

而ParameterHandler的初始化同樣也是在Configuration中實現的,代碼如下:

1   public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
2     ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
3     parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
4     return parameterHandler;
5   }

mybatis源碼解析11---ParameterHandler解析