Search in sources :

Example 1 with ReturnValueWrapper

use of com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper in project qiuyj-code by qiuyuanjun.

the class AbstractSqlGeneratorEngine method getReturnValue.

/**
 * 根据不同的情况得到对应的SqlNode
 */
private ReturnValueWrapper getReturnValue(SqlInfo sqlInfo, MappedStatement ms, Method mapperMethod, Object args) {
    ReturnValueWrapper returnValue = null;
    if (resolver.isExampleMethod(mapperMethod)) {
        // 这里需要解析参数
        Method reflectionMethod = ReflectionUtils.getDeclaredMethod(baseSqlProvider.getClass(), mapperMethod.getName(), ms.getClass(), SqlInfo.class, Object.class);
        returnValue = (ReturnValueWrapper) ReflectionUtils.invokeMethod(baseSqlProvider, reflectionMethod, ms, sqlInfo, args);
    } else {
        // 如果还是ProviderSqlSource,那么需要重新生成Sql,这么判断比上面那种方式更好,效率也更快
        if (ms.getSqlSource().getClass() == ProviderSqlSource.class || parameterObjectIsArrayOrCollection(args)) {
            // 这里需要重新生成sqlNode
            Method reflectionMethod = ReflectionUtils.getDeclaredMethod(baseSqlProvider.getClass(), mapperMethod.getName(), ms.getClass(), SqlInfo.class);
            returnValue = (ReturnValueWrapper) ReflectionUtils.invokeMethod(baseSqlProvider, reflectionMethod, ms, sqlInfo);
        }
    }
    return returnValue;
}
Also used : ReturnValueWrapper(com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper) Method(java.lang.reflect.Method) ProviderSqlSource(org.apache.ibatis.builder.annotation.ProviderSqlSource)

Example 2 with ReturnValueWrapper

use of com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper in project qiuyj-code by qiuyuanjun.

the class AbstractSqlGeneratorEngine method generateSql.

@Override
public void generateSql(MappedStatement ms, Class<? extends Mapper<?, ?>> mapperClass, Method mapperMethod, Object args) {
    // 得到当前实体类的映射数据库的信息
    SqlInfo sqlInfo = getSqlInfo(mapperClass);
    // 得到返回值
    ReturnValueWrapper returnValue = getReturnValue(sqlInfo, ms, mapperMethod, args);
    if (Objects.nonNull(returnValue)) {
        // 处理自定义生成ParameterMapping,如果没有自定义,那么该方法犹如一个空方法
        returnValue.customizedResolveParameterObject(sqlInfo, args, ms.getConfiguration());
        MetaObject msMetaObject = ms.getConfiguration().newMetaObject(ms);
        // 重新设置sqlSource
        msMetaObject.setValue("sqlSource", returnValue.generateSqlSource(ms.getConfiguration()));
        // 如果类型是Select的,那么还需要设置resultMap
        if (sqlInfo.hasEnumField() && ms.getSqlCommandType() == SqlCommandType.SELECT) {
            msMetaObject.setValue("resultMaps", getResultMap(mapperClass));
        } else // 当然,前提是当前的主键标注了@Sequence注解
        if (Objects.nonNull(sqlInfo.getSequenceName()) && ms.getSqlCommandType() == SqlCommandType.INSERT) {
            generateSequenceKey(ms, msMetaObject, sqlInfo);
        }
    }
}
Also used : SqlInfo(com.qiuyj.mybatis.SqlInfo) ReturnValueWrapper(com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 3 with ReturnValueWrapper

use of com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper in project qiuyj-code by qiuyuanjun.

the class MySqlDialect method batchInsert.

@Override
public ReturnValueWrapper batchInsert(Configuration configuration, SqlInfo sqlInfo) {
    // 由于批量增加的sql可能会很长,所以这里给一个较大的初始化值
    StringBuilder sqlBuilder = new StringBuilder(256);
    sqlBuilder.append("INSERT INTO ").append(sqlInfo.getTableName());
    StringJoiner joiner = new StringJoiner(",", "(", ")");
    for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
        joiner.add(column);
    }
    sqlBuilder.append(joiner.toString());
    sqlBuilder.append(" VALUES ");
    return new ReturnValueWrapper(new StaticTextSqlNode(sqlBuilder.toString()), MysqlBatchInsertParameterObjectResolver.getInstance());
}
Also used : ReturnValueWrapper(com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) StringJoiner(java.util.StringJoiner)

Aggregations

ReturnValueWrapper (com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper)3 SqlInfo (com.qiuyj.mybatis.SqlInfo)1 Method (java.lang.reflect.Method)1 StringJoiner (java.util.StringJoiner)1 ProviderSqlSource (org.apache.ibatis.builder.annotation.ProviderSqlSource)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)1