Example usage for org.apache.ibatis.mapping MappedStatement getId

List of usage examples for org.apache.ibatis.mapping MappedStatement getId

Introduction

In this page you can find the example usage for org.apache.ibatis.mapping MappedStatement getId.

Prototype

public String getId() 

Source Link

Usage

From source file:SqlUtil.java

License:Open Source License

/**
 * ?ms - ms???//w  w w . j ava 2 s  .  co m
 *
 * @param ms
 * @param boundSql
 * @param suffix
 * @return
 */
private MappedStatement getMappedStatement(MappedStatement ms, BoundSql boundSql, String suffix) {
    MappedStatement qs = null;
    try {
        qs = ms.getConfiguration().getMappedStatement(ms.getId() + suffix);
    } catch (Exception e) {
        //ignore
    }
    if (qs == null) {
        //MappedStatement
        qs = newMappedStatement(ms, getNewSqlSource(ms, new BoundSqlSqlSource(boundSql), suffix), suffix);
        try {
            ms.getConfiguration().addMappedStatement(qs);
        } catch (Exception e) {
            //ignore
        }
    }
    return qs;
}

From source file:SqlUtil.java

License:Open Source License

/**
 * countMappedStatement/*from w ww.ja va 2 s .co m*/
 *
 * @param ms
 * @param newSqlSource
 * @param suffix
 * @return
 */
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource, String suffix) {
    String id = ms.getId() + suffix;
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, newSqlSource,
            ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
        StringBuilder keyProperties = new StringBuilder();
        for (String keyProperty : ms.getKeyProperties()) {
            keyProperties.append(keyProperty).append(",");
        }
        keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
        builder.keyProperty(keyProperties.toString());
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    if (suffix == SUFFIX_PAGE) {
        builder.resultMaps(ms.getResultMaps());
    } else {
        //countint
        List<ResultMap> resultMaps = new ArrayList<ResultMap>();
        ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), id, int.class, EMPTY_RESULTMAPPING)
                .build();
        resultMaps.add(resultMap);
        builder.resultMaps(resultMaps);
    }
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());

    return builder.build();
}

From source file:cc.oit.dao.impl.mybatis.session.Configuration.java

License:Apache License

public void addMappedStatement(MappedStatement ms) {
    mappedStatements.put(ms.getId(), ms);
}

From source file:cn.chenlichao.web.ssm.support.mybatis.providers.SSMMapperProvider.java

License:Apache License

private void processIndentity(Set<EntityColumn> columnList, StringBuilder sql, MappedStatement ms) {
    Class<?> entityClass = getEntityClass(ms);
    //Identity?//from w  w  w . j a  va 2  s. co m
    boolean hasIdentityKey = false;

    for (EntityColumn column : columnList) {
        if (!column.isInsertable()) {
            continue;
        }
        if (StringUtil.isNotEmpty(column.getSequenceName())) {
        } else if (column.isIdentity()) {
            //?,,??,?
            //bind
            sql.append(SqlHelper.getBindCache(column));
            //Identity??selectKey
            //?Identity
            if (hasIdentityKey) {
                //jdbc??
                if (column.getGenerator() != null && column.getGenerator().equals("JDBC")) {
                    continue;
                }
                throw new RuntimeException(ms.getId() + "" + entityClass.getCanonicalName()
                        + "?MySql,?!");
            }
            //?selectKey
            newSelectKeyMappedStatement(ms, column);
            hasIdentityKey = true;
        } else if (column.isUuid()) {
            //uuid?bind
            sql.append(SqlHelper.getBindValue(column, getUUID()));
        }
    }
}

From source file:cn.myblog.uitl.MybatisPageHelper.PageHelper.java

License:Open Source License

/**
 * MappedStatement?????//ww w.j  a  va2s  .c o m
 *
 * @param ms
 * @param newSqlSource
 * @return
 */
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId() + "_",
            newSqlSource, ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
        StringBuffer keyProperties = new StringBuffer();
        for (String keyProperty : ms.getKeyProperties()) {
            keyProperties.append(keyProperty).append(",");
        }
        keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
        builder.keyProperty(keyProperties.toString());
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    //resultMaps?int??resultMap - ?
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), int.class,
            EMPTY_RESULTMAPPING).build();
    resultMaps.add(resultMap);
    builder.resultMaps(resultMaps);
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());

    return builder.build();
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperHelper.java

License:Open Source License

/**
 * ?SqlSource//from w  w w . j  av a  2s  .  c o m
 * 
 * @param ms
 */
public void setSqlSource(MappedStatement ms) {
    MapperTemplate mapperTemplate = getMapperTemplate(ms.getId());
    try {
        if (mapperTemplate != null) {
            mapperTemplate.setSqlSource(ms);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(":" + e.getMessage());
    }
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperHelper.java

License:Open Source License

/**
 * ????? <br>/*w  w  w .  j a  va2s  .  com*/
 * ?configurationMappedStatement
 * 
 * @param configuration
 */
public void processConfiguration(Configuration configuration) {
    Collection<MappedStatement> collection = configuration.getMappedStatements();
    // ????
    if (collectionSet.contains(collection)) {
        return;
    } else {
        collectionSet.add(collection);
    }
    int size = collection.size();
    Iterator<?> iterator = collection.iterator();
    while (iterator.hasNext()) {
        Object object = iterator.next();
        if (object instanceof MappedStatement) {
            MappedStatement ms = (MappedStatement) object;
            if (isMapperMethod(ms.getId())) {
                if (ms.getSqlSource() instanceof ProviderSqlSource) {
                    setSqlSource(ms);
                }
            }
        }
        // ??selectKeyms??
        if (collection.size() != size) {
            size = collection.size();
            iterator = collection.iterator();
        }
    }
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] objects = invocation.getArgs();
    MappedStatement ms = (MappedStatement) objects[0];
    String msId = ms.getId();
    //??//from w  w w.j a  v a 2 s.c  o  m
    if (mapperHelper.isMapperMethod(msId)) {
        //????ProviderSqlSource??????
        if (ms.getSqlSource() instanceof ProviderSqlSource) {
            mapperHelper.setSqlSource(ms);
        }
    }
    Object result = invocation.proceed();
    return result;
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperTemplate.java

License:Open Source License

/**
 * ?SqlSource//from w  ww . jav a  2  s.  c o m
 * 
 * @param ms
 * @throws java.lang.reflect.InvocationTargetException
 * @throws IllegalAccessException
 */
public void setSqlSource(MappedStatement ms) throws Exception {
    if (this.mapperClass == getMapperClass(ms.getId())) {
        if (mapperHelper.isSpring4()) {
            return;
        } else if (mapperHelper.isSpring()) {
            throw new RuntimeException("Spring4.x.x ??,"
                    + "?Spring" + mapperHelper.getSpringVersion()
                    + ",?,"
                    + "?MapperScannerConfigurer,????Mapper?,"
                    + "??Mybatisxml?<mappers>Mapper?.");
        } else {
            throw new RuntimeException(
                    "??Mybatisxml?<mappers>Mapper?.");
        }
    }
    Method method = methodMap.get(getMethodName(ms));
    try {
        if (method.getReturnType() == Void.TYPE) {
            method.invoke(this, ms);
        } else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {
            SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
            DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
            setSqlSource(ms, dynamicSqlSource);
        } else {
            throw new RuntimeException(
                    "Mapper,?voidSqlNode!");
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getTargetException() != null ? e.getTargetException() : e);
    }
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperTemplate.java

License:Open Source License

/**
 * ? - //from   ww  w .j  av  a  2 s.  c om
 * 
 * @param ms
 * @return
 */
public Class<?> getSelectReturnType(MappedStatement ms) {
    String msId = ms.getId();
    Class<?> mapperClass = getMapperClass(msId);
    Type[] types = mapperClass.getGenericInterfaces();
    for (Type type : types) {
        if (type instanceof ParameterizedType) {
            ParameterizedType t = (ParameterizedType) type;
            if (t.getRawType() == this.mapperClass) {
                Class<?> returnType = (Class<?>) t.getActualTypeArguments()[0];
                return returnType;
            }
        }
    }
    throw new RuntimeException("?Mapper<T>:" + msId);
}