Java Code Examples for org.apache.ibatis.reflection.MetaObject#setValue()
The following examples show how to use
org.apache.ibatis.reflection.MetaObject#setValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultResultSetHandler.java From mybatis with Apache License 2.0 | 6 votes |
private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException { final List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix); boolean foundValues = false; final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings(); for (ResultMapping propertyMapping : propertyMappings) { final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix); if (propertyMapping.isCompositeResult() || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) || propertyMapping.getResultSet() != null) { Object value = getPropertyMappingValue(rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix); // issue #541 make property optional final String property = propertyMapping.getProperty(); // issue #377, call setter on nulls if (value != NO_VALUE && property != null && (value != null || configuration.isCallSettersOnNulls())) { if (value != null || !metaObject.getSetterType(property).isPrimitive()) { metaObject.setValue(property, value); } foundValues = true; } } } return foundValues; }
Example 2
Source File: BaseExecutor.java From mybaties with Apache License 2.0 | 6 votes |
private void handleLocallyCachedOutputParameters(MappedStatement ms, CacheKey key, Object parameter, BoundSql boundSql) { //处理存储过程的OUT参数 if (ms.getStatementType() == StatementType.CALLABLE) { final Object cachedParameter = localOutputParameterCache.getObject(key); if (cachedParameter != null && parameter != null) { final MetaObject metaCachedParameter = configuration.newMetaObject(cachedParameter); final MetaObject metaParameter = configuration.newMetaObject(parameter); for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) { if (parameterMapping.getMode() != ParameterMode.IN) { final String parameterName = parameterMapping.getProperty(); final Object cachedValue = metaCachedParameter.getValue(parameterName); metaParameter.setValue(parameterName, cachedValue); } } } } }
Example 3
Source File: UpdateEnhancement.java From mybatis-boost with MIT License | 6 votes |
@Override public void replace(Connection connection, MetaObject metaObject, MappedStatement mappedStatement, BoundSql boundSql) { String sql = boundSql.getSql(); if (mappedStatement.getSqlCommandType() == SqlCommandType.UPDATE && sql.toUpperCase().startsWith("UPDATE SET ")) { String[] split = splitSql(sql); // split[0] = columns, split[1] = conditions(if there were) Class<?> entityType = MapperUtils.getEntityTypeFromMapper (mappedStatement.getId().substring(0, mappedStatement.getId().lastIndexOf('.'))); boolean mapUnderscoreToCamelCase = (boolean) metaObject.getValue("delegate.configuration.mapUnderscoreToCamelCase"); BinaryTuple<List<String>, List<String>> propertiesAndColumns = SqlUtils.getPropertiesAndColumnsFromLiteralColumns(split[0], entityType, mapUnderscoreToCamelCase); List<String> conditionProperties = getConditionProperties(entityType, boundSql.getParameterMappings()); propertiesAndColumns.first().removeAll(conditionProperties); propertiesAndColumns.second().removeAll(conditionProperties.stream() .map(it -> SqlUtils.normalizeColumn(it, mapUnderscoreToCamelCase)).collect(Collectors.toList())); metaObject.setValue("delegate.boundSql.sql", buildSQL(sql, entityType, propertiesAndColumns.second(), split)); metaObject.setValue("delegate.boundSql.parameterMappings", getParameterMappings(metaObject, boundSql, propertiesAndColumns.first())); } }
Example 4
Source File: PageInterceptor.java From QuickProject with Apache License 2.0 | 6 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); BoundSql boundSql = statementHandler.getBoundSql(); MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory()); RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds"); if ((rowBounds != null) && (rowBounds != RowBounds.DEFAULT)) { Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration"); Dialect dialect = DialectParser.parse(configuration); String sql = (String) metaStatementHandler.getValue("delegate.boundSql.sql"); sql = dialect.addLimitString(sql, rowBounds.getOffset(), rowBounds.getLimit()); metaStatementHandler.setValue("delegate.boundSql.sql", sql); metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET); metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT); } log.debug("SQL : " + boundSql.getSql()); return invocation.proceed(); }
Example 5
Source File: BaseExecutor.java From mybatis with Apache License 2.0 | 6 votes |
private void handleLocallyCachedOutputParameters(MappedStatement ms, CacheKey key, Object parameter, BoundSql boundSql) { //处理存储过程的OUT参数 if (ms.getStatementType() == StatementType.CALLABLE) { final Object cachedParameter = localOutputParameterCache.getObject(key); if (cachedParameter != null && parameter != null) { final MetaObject metaCachedParameter = configuration.newMetaObject(cachedParameter); final MetaObject metaParameter = configuration.newMetaObject(parameter); for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) { if (parameterMapping.getMode() != ParameterMode.IN) { final String parameterName = parameterMapping.getProperty(); final Object cachedValue = metaCachedParameter.getValue(parameterName); metaParameter.setValue(parameterName, cachedValue); } } } } }
Example 6
Source File: InformixDialect.java From Mybatis-PageHelper with MIT License | 6 votes |
@Override public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) { paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow()); paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize()); //处理pageKey pageKey.update(page.getStartRow()); pageKey.update(page.getPageSize()); //处理参数配置 if (boundSql.getParameterMappings() != null) { List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(); if (page.getStartRow() > 0) { newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build()); } if (page.getPageSize() > 0) { newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build()); } newParameterMappings.addAll(boundSql.getParameterMappings()); MetaObject metaObject = MetaObjectUtil.forObject(boundSql); metaObject.setValue("parameterMappings", newParameterMappings); } return paramMap; }
Example 7
Source File: DefaultResultSetHandler.java From mybatis with Apache License 2.0 | 6 votes |
private Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping resultMapping, Class<?> parameterType, String columnPrefix) throws SQLException { final Object parameterObject = instantiateParameterObject(parameterType); final MetaObject metaObject = configuration.newMetaObject(parameterObject); boolean foundValues = false; for (ResultMapping innerResultMapping : resultMapping.getComposites()) { final Class<?> propType = metaObject.getSetterType(innerResultMapping.getProperty()); final TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(propType); final Object propValue = typeHandler.getResult(rs, prependPrefix(innerResultMapping.getColumn(), columnPrefix)); // issue #353 & #560 do not execute nested query if key is null if (propValue != null) { metaObject.setValue(innerResultMapping.getProperty(), propValue); foundValues = true; } } return foundValues ? parameterObject : null; }
Example 8
Source File: MySqlDialect.java From Mybatis-PageHelper with MIT License | 6 votes |
@Override public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) { paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow()); paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize()); //处理pageKey pageKey.update(page.getStartRow()); pageKey.update(page.getPageSize()); //处理参数配置 if (boundSql.getParameterMappings() != null) { List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings()); if (page.getStartRow() == 0) { newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build()); } else { newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build()); newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build()); } MetaObject metaObject = MetaObjectUtil.forObject(boundSql); metaObject.setValue("parameterMappings", newParameterMappings); } return paramMap; }
Example 9
Source File: Jdbc3KeyGenerator.java From mybaties with Apache License 2.0 | 5 votes |
private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException { for (int i = 0; i < keyProperties.length; i++) { TypeHandler<?> th = typeHandlers[i]; if (th != null) { Object value = th.getResult(rs, i + 1); metaParam.setValue(keyProperties[i], value); } } }
Example 10
Source File: MultipleJdbc3KeyGenerator.java From tk-mybatis with MIT License | 5 votes |
private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException { for (int i = 0; i < keyProperties.length; i++) { TypeHandler<?> th = typeHandlers[i]; if (th != null) { Object value = th.getResult(rs, i + 1); metaParam.setValue(keyProperties[i], value); } } }
Example 11
Source File: AbstractSelectMethodBuilder.java From jeesuite-libs with Apache License 2.0 | 5 votes |
@Override void setResultType(Configuration configuration, MappedStatement ms, Class<?> entityClass) { List<ResultMap> resultMaps = new ArrayList<ResultMap>(); resultMaps.add(getResultMap(configuration,entityClass)); MetaObject metaObject = SystemMetaObject.forObject(ms); metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps)); }
Example 12
Source File: Delete.java From mybatis-boost with MIT License | 5 votes |
@Override public void replace(Connection connection, MetaObject metaObject, MappedStatement mappedStatement, BoundSql boundSql) { Class<?> entityType = MapperUtils.getEntityTypeFromMapper (mappedStatement.getId().substring(0, mappedStatement.getId().lastIndexOf('.'))); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("DELETE FROM ").append(EntityUtils.getTableName(entityType, configuration.getNameAdaptor())); Map<?, ?> parameterMap = (Map<?, ?>) boundSql.getParameterObject(); Object entity = parameterMap.get("param1"); List<String> properties; String[] conditionalProperties = (String[]) parameterMap.get("param2"); if (conditionalProperties.length == 0) { properties = EntityUtils.getProperties(entity, true); } else { properties = Arrays.stream(conditionalProperties).map(PropertyUtils::normalizeProperty).collect(Collectors.toList()); } if (!properties.isEmpty()) { boolean mapUnderscoreToCamelCase = (boolean) metaObject.getValue("delegate.configuration.mapUnderscoreToCamelCase"); List<String> columns = properties.stream() .map(it -> SqlUtils.normalizeColumn(it, mapUnderscoreToCamelCase)).collect(Collectors.toList()); SqlUtils.appendWhere(sqlBuilder, columns.stream()); } List<ParameterMapping> parameterMappings = MyBatisUtils.getParameterMappings ((org.apache.ibatis.session.Configuration) metaObject.getValue("delegate.configuration"), properties); MyBatisUtils.getMetaObject(metaObject.getValue("delegate.parameterHandler")) .setValue("parameterObject", entity); metaObject.setValue("delegate.boundSql.parameterObject", entity); metaObject.setValue("delegate.boundSql.parameterMappings", parameterMappings); metaObject.setValue("delegate.boundSql.sql", sqlBuilder.toString()); }
Example 13
Source File: MybatisSqlInterceptor.java From taoshop with Apache License 2.0 | 5 votes |
/** * 包装sql后,重置到invocation中 * @param invocation * @param sql * @throws SQLException */ private void resetSql2Invocation(Invocation invocation, String sql) throws SQLException { final Object[] args = invocation.getArgs(); MappedStatement statement = (MappedStatement) args[0]; Object parameterObject = args[1]; BoundSql boundSql = statement.getBoundSql(parameterObject); MappedStatement newStatement = newMappedStatement(statement, new BoundSqlSqlSource(boundSql)); MetaObject msObject = MetaObject.forObject(newStatement, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),new DefaultReflectorFactory()); msObject.setValue("sqlSource.boundSql.sql", sql); args[0] = newStatement; }
Example 14
Source File: MybatisConfig.java From SpringBoot2.0 with Apache License 2.0 | 5 votes |
/** * 公用字段配置 */ @Override public void insertFill(MetaObject metaObject) { if (metaObject.hasSetter("creatTime")) { metaObject.setValue("creatTime", new Date()); } if (metaObject.hasSetter("updateTime")) { metaObject.setValue("updateTime", new Date()); } }
Example 15
Source File: PagingInterceptor.java From Aooms with Apache License 2.0 | 5 votes |
public Object intercept(Invocation invocation) throws Throwable { /*MappedStatement mappedStatement2 = (MappedStatement)invocation.getArgs()[0]; if(MyBatisConst.MS_RECORD_FIND_BY_PK.equals(mappedStatement2.getId())){ System.err.println("invocation = " + invocation); }else{ return invocation.proceed(); }*/ StatementHandler target = MetaObjectAssistant.getTarget(invocation,StatementHandler.class); MetaObject metaObject = MetaObjectAssistant.getMetaObject(target); PreparedStatementHandler preparedStatementHandler = (PreparedStatementHandler) metaObject.getValue("delegate"); Map para = (Map) preparedStatementHandler.getBoundSql().getParameterObject(); if(para == null) return invocation.proceed(); Object isCount = para.get(MyBatisConst.CRUD_QUERY_COUNT_PLACEHOLDER); Object isPaging = para.get(MyBatisConst.CRUD_QUERY_PAGING_PLACEHOLDER); if(isCount != null){ String countsql = "select count(*) count from (" + preparedStatementHandler.getBoundSql().getSql() + ") _table"; metaObject.setValue("delegate.boundSql.sql",countsql); } if(isPaging != null){ String ds = DynamicDataSourceHolder.getDataSource(); String driveClass = Aooms.self().getDynamicDataSource().getDriveName(ds == null ? AoomsVar.DEFAULT_DATASOURCE : ds); //(String)metaObject.getValue("delegate.configuration.environment.dataSource.driverClass"); RowBounds rowBounds = (RowBounds) metaObject.getValue("delegate.rowBounds"); String sql = preparedStatementHandler.getBoundSql().getSql(); String pagingSql = dialectSelector.selector(driveClass).pagingQuery(sql,rowBounds); metaObject.setValue("delegate.boundSql.sql",pagingSql); metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET); // 默认分页不生效 metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT); // 默认分页不生效 } Object value = invocation.proceed(); return value; }
Example 16
Source File: IdentityKeyGenerator.java From mybatis-jpa with Apache License 2.0 | 5 votes |
private void setValue(MetaObject metaParam, String property, Object value) { if (metaParam.hasSetter(property)) { metaParam.setValue(property, value); } else { throw new ExecutorException( "No setter found for the keyProperty '" + property + "' in " + metaParam .getOriginalObject().getClass().getName() + "."); } }
Example 17
Source File: GetByPrimaryKeyBuilder.java From azeroth with Apache License 2.0 | 5 votes |
/** * 设置返回值类型 * * @param ms * @param entityClass */ private static void setResultType(Configuration configuration, MappedStatement ms, Class<?> entityClass) { List<ResultMap> resultMaps = new ArrayList<ResultMap>(); resultMaps.add(getResultMap(configuration, entityClass)); MetaObject metaObject = SystemMetaObject.forObject(ms); metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps)); }
Example 18
Source File: AbstractHelperDialect.java From Mybatis-PageHelper with MIT License | 5 votes |
protected void handleParameter(BoundSql boundSql, MappedStatement ms){ if (boundSql.getParameterMappings() != null) { List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings()); newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, Integer.class).build()); newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, Integer.class).build()); MetaObject metaObject = MetaObjectUtil.forObject(boundSql); metaObject.setValue("parameterMappings", newParameterMappings); } }
Example 19
Source File: Jdbc3KeyGenerator.java From mybatis with Apache License 2.0 | 5 votes |
private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException { for (int i = 0; i < keyProperties.length; i++) { TypeHandler<?> th = typeHandlers[i]; if (th != null) { Object value = th.getResult(rs, i + 1); metaParam.setValue(keyProperties[i], value); } } }
Example 20
Source File: MetaObjectAssistant.java From Aooms with Apache License 2.0 | 4 votes |
public static void setDelegateBoundSql(MetaObject metaObject, BoundSql boundSql) { metaObject.setValue("delegate.boundSql", boundSql); }