Example usage for org.apache.ibatis.scripting.xmltags DynamicSqlSource DynamicSqlSource

List of usage examples for org.apache.ibatis.scripting.xmltags DynamicSqlSource DynamicSqlSource

Introduction

In this page you can find the example usage for org.apache.ibatis.scripting.xmltags DynamicSqlSource DynamicSqlSource.

Prototype

public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) 

Source Link

Usage

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

License:Open Source License

/**
 * ?SqlSource// w  w w. j  a  va 2s . com
 * 
 * @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:com.cheyipai.platformservice.thirdparty.core.pager.PageHelper.java

License:Open Source License

/**
 * ?sqlSource/*from w ww .  j  a v a2 s .  com*/
 *
 * @param ms
 * @param newSqlSource
 * @param suffix
 * @return
 */
private SqlSource getNewSqlSource(MappedStatement ms, BoundSqlSqlSource newSqlSource, String suffix) {
    SqlSource sqlSource = ms.getSqlSource();
    //XMLLanguageDriver.javaXMLScriptBuilder.java???SqlSource
    if (sqlSource instanceof DynamicSqlSource) {
        MetaObject msObject = forObject(ms);
        List<SqlNode> contents = (List<SqlNode>) msObject.getValue(SQL_NODES);
        List<SqlNode> newSqlNodes = new ArrayList<SqlNode>(contents.size() + 2);
        //?
        if (suffix == SUFFIX_PAGE) {
            newSqlNodes.add(new TextSqlNode(getPageSqlBefore()));
            newSqlNodes.addAll(contents);
            newSqlNodes.add(new TextSqlNode(getPageSqlAfter()));
            return new MyDynamicSqlSource(ms.getConfiguration(), new MixedSqlNode(newSqlNodes));
        } else {
            newSqlNodes.add(new TextSqlNode(getCountSqlBefore()));
            newSqlNodes.addAll(contents);
            newSqlNodes.add(new TextSqlNode(getCountSqlAfter()));
            return new DynamicSqlSource(ms.getConfiguration(), new MixedSqlNode(newSqlNodes));
        }
    } else {
        //RawSqlSource
        //?
        if (suffix == SUFFIX_PAGE) {
            //sql
            MetaObject sqlObject = forObject(newSqlSource);
            sqlObject.setValue(BOUND_SQL, getPageSql((String) sqlObject.getValue(BOUND_SQL)));
            //?
            List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>();
            newParameterMappings.addAll(newSqlSource.getBoundSql().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());
            sqlObject.setValue("boundSql.parameterMappings", newParameterMappings);
        } else {
            //count sql
            MetaObject sqlObject = forObject(newSqlSource);
            sqlObject.setValue(BOUND_SQL, getCountSql((String) sqlObject.getValue(BOUND_SQL)));
        }
        return newSqlSource;
    }
}

From source file:com.github.abel533.mapper.MapperTemplate.java

License:Open Source License

/**
 * ?SqlSource/*from  w  w w.j  av a  2  s .  c  o  m*/
 *
 * @param ms
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 */
public void setSqlSource(MappedStatement ms) throws Exception {
    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:com.hand.hap.mybatis.mapperhelper.MapperTemplate.java

License:Open Source License

/**
 * ?SqlSource//  w w w .  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())) {
        throw new RuntimeException("?????Mapper?" + this.mapperClass);
    }
    Method method = methodMap.get(getMethodName(ms));
    try {
        //??ms??
        if (method.getReturnType() == Void.TYPE) {
            method.invoke(this, ms);
        }
        //?SqlNode
        else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {
            SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
            DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
            setSqlSource(ms, dynamicSqlSource);
        }
        //?xml?sql
        else if (String.class.equals(method.getReturnType())) {
            String xmlSql = (String) method.invoke(this, ms);
            SqlSource sqlSource = createSqlSource(ms, xmlSql);
            //?SqlSource
            setSqlSource(ms, sqlSource);
        } else {
            throw new RuntimeException(
                    "Mapper,?void,SqlNode,String?!");
        }
        //cache
        checkCache(ms);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getTargetException() != null ? e.getTargetException() : e);
    }
}

From source file:com.sinotopia.mybatis.mapper.mapperhelper.MapperTemplate.java

License:Open Source License

/**
 * ?SqlSource/*w  ww  .  j  av a2 s.c om*/
 *
 * @param ms
 * @throws java.lang.reflect.InvocationTargetException
 * @throws IllegalAccessException
 */
public void setSqlSource(MappedStatement ms) throws Exception {
    if (this.mapperClass == getMapperClass(ms.getId())) {
        throw new MapperException("?????Mapper?" + this.mapperClass);
    }
    Method method = methodMap.get(getMethodName(ms));
    try {
        //??ms??
        if (method.getReturnType() == Void.TYPE) {
            method.invoke(this, ms);
        }
        //?SqlNode
        else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {
            SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
            DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
            setSqlSource(ms, dynamicSqlSource);
        }
        //?xml?sql
        else if (String.class.equals(method.getReturnType())) {
            String xmlSql = (String) method.invoke(this, ms);
            SqlSource sqlSource = createSqlSource(ms, xmlSql);
            //?SqlSource
            setSqlSource(ms, sqlSource);
        } else {
            throw new MapperException(
                    "Mapper,?void,SqlNode,String?!");
        }
        //cache
        checkCache(ms);
    } catch (IllegalAccessException e) {
        throw new MapperException(e);
    } catch (InvocationTargetException e) {
        throw new MapperException(e.getTargetException() != null ? e.getTargetException() : e);
    }
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

License:Open Source License

private void buildMultiGet(String statementId, String collection) {
    Integer fetchSize = null;//from   w  w w .  ja v a 2  s .  c  om
    Integer timeout = null;
    Class<?> resultType = entityClass;
    //~~~~~~~~~~~~~~~~~~~~~~~
    boolean flushCache = true;
    boolean useCache = false;
    boolean resultOrdered = false;
    KeyGenerator keyGenerator = new NoKeyGenerator();

    SqlSource sqlSource = new DynamicSqlSource(configuration, getMultiGetSql(collection));

    String resultMap = null;
    Iterator<String> resultMapNames = configuration.getResultMapNames().iterator();
    while (resultMapNames.hasNext()) {
        String name = resultMapNames.next();
        ResultMap temp = configuration.getResultMap(name);
        if (temp.getType().equals(entityClass)) {
            resultMap = temp.getId();
            break;
        }
    }
    assistant.addMappedStatement(statementId, sqlSource, StatementType.PREPARED, SqlCommandType.SELECT,
            fetchSize, timeout, null, idField.getType(), resultMap, resultType, null, flushCache, useCache,
            resultOrdered, keyGenerator, null, null, databaseId, lang);
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

License:Open Source License

private void buildBatchDelete(String statementId, String collection) {
    Integer timeout = null;/*  w ww. j a v a  2s. c  om*/
    Class<?> parameterType = idField.getType();

    //~~~~~~~~~~~~~~~~~~~~~~~
    boolean flushCache = true;
    boolean useCache = false;
    boolean resultOrdered = false;
    KeyGenerator keyGenerator = new NoKeyGenerator();

    SqlSource sqlSource = new DynamicSqlSource(configuration, getBatchDeleteSql(collection));

    assistant.addMappedStatement(statementId, sqlSource, StatementType.PREPARED, SqlCommandType.DELETE, null,
            timeout, null, parameterType, null, null, null, flushCache, useCache, resultOrdered, keyGenerator,
            null, null, databaseId, lang);
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

License:Open Source License

private void buildBatchInsert(String statementId, String collection) {
    Integer fetchSize = null;//from   ww  w . ja  v a 2 s.  c  o  m
    Integer timeout = null;
    Class<?> parameterType = entityClass;

    ///~~~~~~~~~~
    boolean flushCache = true;
    boolean useCache = false;
    boolean resultOrdered = false;
    KeyGenerator keyGenerator = new NoKeyGenerator();
    String keyProperty = null;
    String keyColumn = null;

    Id id = AnnotationUtils.findDeclaredAnnotation(Id.class, entityClass);
    GeneratedValue generatedValue = AnnotationUtils.findDeclaredAnnotation(GeneratedValue.class, entityClass);
    if (id != null) {
        String keyStatementId = entityClass.getName() + ".insert" + SelectKeyGenerator.SELECT_KEY_SUFFIX;
        if (!sharded) {
            if (containSn)
                snGenerators.put(statementId, new SnGenerator());

            if (configuration.hasKeyGenerator(keyStatementId)) {
                keyGenerator = configuration.getKeyGenerator(keyStatementId);
            } else if (generatedValue != null) {
                if (generatedValue.strategy() == GenerationType.UUID) {
                    keyGenerator = new UuidKeyGenerator(generatedValue.length());

                }
            } else {
                keyGenerator = id.generatedKeys() ? new Jdbc4KeyGenerator() : new NoKeyGenerator();
            }
        } else {
            if (containSn)
                shardSnGenerators.put(statementId, new ShardSnGenerator());
            if (generatedValue != null) {
                if (generatedValue.strategy() == GenerationType.UUID) {
                    shardedKeyGenerators.put(statementId, new ShardUuidKeyGenerator(generatedValue.length()));
                } else if (generatedValue.strategy() == GenerationType.TABLE
                        || generatedValue.strategy() == GenerationType.AUTO) {
                    shardedKeyGenerators.put(statementId, new ShardJdbc4KeyGenerator());
                }
            }
        }
        keyProperty = idField.getName();
        keyColumn = StringUtils.isBlank(id.column()) ? CaseFormatUtils.camelToUnderScore(idField.getName())
                : id.column();
    }

    List<SqlNode> contents = new ArrayList<SqlNode>();
    contents.add(this.getBatchInsertSql(collection));
    SqlSource sqlSource = new DynamicSqlSource(configuration, new MixedSqlNode(contents));

    assistant.addMappedStatement(statementId, sqlSource, StatementType.PREPARED, SqlCommandType.INSERT,
            fetchSize, timeout, null, parameterType, null, null, null, flushCache, useCache, resultOrdered,
            keyGenerator, keyProperty, keyColumn, databaseId, lang);
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

License:Open Source License

private void buildInsert(String statementId) {
    ///*from   www.j a va2s.c  om*/
    Integer timeout = null;
    Class<?> parameterType = entityClass;

    ///~~~~~~~~~~
    boolean flushCache = true;
    boolean useCache = false;
    boolean resultOrdered = false;
    KeyGenerator keyGenerator = new NoKeyGenerator();
    String keyProperty = null;
    String keyColumn = null;

    Id id = AnnotationUtils.findDeclaredAnnotation(Id.class, entityClass);
    GeneratedValue generatedValue = AnnotationUtils.findDeclaredAnnotation(GeneratedValue.class, entityClass);
    if (id != null) {
        String keyStatementId = entityClass.getName() + ".insert" + SelectKeyGenerator.SELECT_KEY_SUFFIX;
        if (!sharded) {

            if (containSn)
                snGenerators.put(statementId, new SnGenerator());
            if (configuration.hasKeyGenerator(keyStatementId)) {
                keyGenerator = configuration.getKeyGenerator(keyStatementId);
            } else if (generatedValue != null) {
                if (generatedValue.strategy() == GenerationType.UUID) {
                    keyGenerator = new UuidKeyGenerator(generatedValue.length());

                }
            } else {
                keyGenerator = id.generatedKeys() ? new Jdbc4KeyGenerator() : new NoKeyGenerator();
            }
        } else {
            if (containSn)
                shardSnGenerators.put(statementId, new ShardSnGenerator());
            if (generatedValue != null) {
                if (generatedValue.strategy() == GenerationType.UUID) {
                    shardedKeyGenerators.put(statementId, new ShardUuidKeyGenerator(generatedValue.length()));
                } else if (generatedValue.strategy() == GenerationType.TABLE
                        || generatedValue.strategy() == GenerationType.AUTO) {
                    shardedKeyGenerators.put(statementId, new ShardJdbc4KeyGenerator());
                }
            }
            //                shardedKeyGenerators.put(statementId, new shardeduu)
        }
        keyProperty = idField.getName();
        keyColumn = StringUtils.isBlank(id.column()) ? CaseFormatUtils.camelToUnderScore(idField.getName())
                : id.column();
    }

    List<SqlNode> contents = new ArrayList<SqlNode>();
    contents.add(this.getInsertSql());
    SqlSource sqlSource = new DynamicSqlSource(configuration, new MixedSqlNode(contents));
    String parameterMap = null;
    Iterator<String> parameterMapNames = configuration.getParameterMapNames().iterator();
    while (parameterMapNames.hasNext()) {
        String name = parameterMapNames.next();
        ParameterMap temp = configuration.getParameterMap(name);
        if (temp.getType().equals(entityClass)) {
            parameterMap = temp.getId();
            break;
        }
    }
    assistant.addMappedStatement(statementId, sqlSource, StatementType.PREPARED, SqlCommandType.INSERT, null,
            timeout, parameterMap, parameterType, null, null, null, flushCache, useCache, resultOrdered,
            keyGenerator, keyProperty, keyColumn, databaseId, lang);
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

License:Open Source License

private void buildDelete(String statementId) {
    Integer timeout = null;//w  w w  .j  a v a  2  s. co  m
    Class<?> parameterType = entityClass;

    //~~~~~~~~~~~~~~~~~~~~~~~
    boolean flushCache = true;
    boolean useCache = false;
    boolean resultOrdered = false;
    KeyGenerator keyGenerator = new NoKeyGenerator();
    List<SqlNode> contents = new ArrayList<SqlNode>();
    SqlNode sqlNode = new TextSqlNode(
            "DELETE FROM " + tableName + " WHERE " + getIdColumnName() + " = #{" + getIdFieldName() + "} ");
    contents.add(sqlNode);
    //        if (versionField != null)
    //            contents.add(new IfSqlNode(new TextSqlNode(getVersionSQL()),
    //                    getTestByField(null, versionField)));
    SqlSource sqlSource = new DynamicSqlSource(configuration, new MixedSqlNode(contents));

    assistant.addMappedStatement(statementId, sqlSource, StatementType.PREPARED, SqlCommandType.DELETE, null,
            timeout, null, parameterType, null, null, null, flushCache, useCache, resultOrdered, keyGenerator,
            null, null, databaseId, lang);
}