예제 #1
0
파일: SQLHelp.java 프로젝트: ptzhuf/commons
 /**
  * 对SQL参数(?)设值, 参考org.apache.ibatis.executor.parameter.DefaultParameterHandler。
  *
  * @param ps 表示预编译的 SQL 语句的对象。
  * @param mappedStatement MappedStatement
  * @param boundSql SQL
  * @param parameterObject 参数对象
  * @throws java.sql.SQLException 数据库异常
  */
 @SuppressWarnings("unchecked")
 public static void setParameters(
     PreparedStatement ps,
     MappedStatement mappedStatement,
     BoundSql boundSql,
     Object parameterObject)
     throws SQLException {
   ErrorContext.instance()
       .activity("setting parameters")
       .object(mappedStatement.getParameterMap().getId());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     Configuration configuration = mappedStatement.getConfiguration();
     TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
     MetaObject metaObject =
         parameterObject == null ? null : configuration.newMetaObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         PropertyTokenizer prop = new PropertyTokenizer(propertyName);
         if (parameterObject == null) {
           value = null;
         } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else if (boundSql.hasAdditionalParameter(propertyName)) {
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
             && boundSql.hasAdditionalParameter(prop.getName())) {
           value = boundSql.getAdditionalParameter(prop.getName());
           if (value != null) {
             value =
                 configuration
                     .newMetaObject(value)
                     .getValue(propertyName.substring(prop.getName().length()));
           }
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         @SuppressWarnings("rawtypes")
         TypeHandler typeHandler = parameterMapping.getTypeHandler();
         if (typeHandler == null) {
           throw new ExecutorException(
               "There was no TypeHandler found for parameter "
                   + propertyName
                   + " of statement "
                   + mappedStatement.getId());
         }
         typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
       }
     }
   }
 }
예제 #2
0
 public CacheKey createCacheKey(
     MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
   if (closed) throw new ExecutorException("Executor was closed.");
   CacheKey cacheKey = new CacheKey();
   cacheKey.update(ms.getId());
   cacheKey.update(rowBounds.getOffset());
   cacheKey.update(rowBounds.getLimit());
   cacheKey.update(boundSql.getSql());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings.size() > 0 && parameterObject != null) {
     TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
     if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
       cacheKey.update(parameterObject);
     } else {
       MetaObject metaObject = configuration.newMetaObject(parameterObject);
       for (ParameterMapping parameterMapping : parameterMappings) {
         String propertyName = parameterMapping.getProperty();
         if (metaObject.hasGetter(propertyName)) {
           cacheKey.update(metaObject.getValue(propertyName));
         } else if (boundSql.hasAdditionalParameter(propertyName)) {
           cacheKey.update(boundSql.getAdditionalParameter(propertyName));
         }
       }
     }
   }
   return cacheKey;
 }
예제 #3
0
 /**
  * 获取一个Mybatis运行时调用的SQL和参数
  *
  * @param ms
  * @param parameterObject
  * @return
  */
 public static MyBatisSql getMyBatisSql(MappedStatement ms, Object parameterObject) {
   MyBatisSql ibatisSql = new MyBatisSql();
   BoundSql boundSql = ms.getBoundSql(parameterObject);
   ibatisSql.setSql(boundSql.getSql());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     Object[] parameterArray = new Object[parameterMappings.size()];
     MetaObject metaObject =
         parameterObject == null ? null : MetaObject.forObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         PropertyTokenizer prop = new PropertyTokenizer(propertyName);
         if (parameterObject == null) {
           value = null;
         } else if (ms.getConfiguration()
             .getTypeHandlerRegistry()
             .hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else if (boundSql.hasAdditionalParameter(propertyName)) {
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
             && boundSql.hasAdditionalParameter(prop.getName())) {
           value = boundSql.getAdditionalParameter(prop.getName());
           if (value != null) {
             value =
                 MetaObject.forObject(value)
                     .getValue(propertyName.substring(prop.getName().length()));
           }
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         parameterArray[i] = value;
       }
     }
     ibatisSql.setParameters(parameterArray);
   }
   return ibatisSql;
 }
예제 #4
0
 /** 复制BoundSql对象 */
 private BoundSql copyFromBoundSql(MappedStatement ms, BoundSql boundSql, String sql) {
   BoundSql newBoundSql =
       new BoundSql(
           ms.getConfiguration(),
           sql,
           boundSql.getParameterMappings(),
           boundSql.getParameterObject());
   for (ParameterMapping mapping : boundSql.getParameterMappings()) {
     String prop = mapping.getProperty();
     if (boundSql.hasAdditionalParameter(prop)) {
       newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
     }
   }
   return newBoundSql;
 }
 public void setParameters(PreparedStatement ps) throws SQLException {
   ErrorContext.instance()
       .activity("setting parameters")
       .object(mappedStatement.getParameterMap().getId());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     MetaObject metaObject =
         parameterObject == null ? null : configuration.newMetaObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         if (boundSql.hasAdditionalParameter(
             propertyName)) { // issue #448 ask first for additional params
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (parameterObject == null) {
           value = null;
         } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         TypeHandler typeHandler = parameterMapping.getTypeHandler();
         if (typeHandler == null) {
           throw new ExecutorException(
               "There was no TypeHandler found for parameter "
                   + propertyName
                   + " of statement "
                   + mappedStatement.getId());
         }
         JdbcType jdbcType = parameterMapping.getJdbcType();
         if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();
         typeHandler.setParameter(ps, i + 1, value, jdbcType);
       }
     }
   }
 }