Search in sources :

Example 1 with PropertyColumnMapping

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

the class AbstractSqlGeneratorEngine method analysis.

@Override
public void analysis(Class<? extends Mapper<?, ?>> actualMapperClass, Configuration configuration) {
    if (Objects.isNull(sqlInfos)) {
        sqlInfos = new HashMap<>();
    }
    if (!sqlInfos.containsKey(actualMapperClass)) {
        synchronized (sqlInfoWriteLock) {
            if (!sqlInfos.containsKey(actualMapperClass)) {
                /*
           * 这里生成SqlInfo会非常的耗时
           * 所以这里一定要将结果缓存起来
           */
                SqlInfo mapperSqlInfo = new SqlInfo(actualMapperClass, chain, configuration);
                sqlInfos.put(actualMapperClass, mapperSqlInfo);
            }
        }
    }
    /*
     * 如果当前解析的mapperClass所对应的实体类有枚举类型
     * 那么就解析所有属性生成对应的ResultMap
     */
    SqlInfo currentSqlInfo = getSqlInfo(actualMapperClass);
    // 这里判断下,sqlinfo里面的Configuration是否为null,如果为null,那么需要设置
    if (Objects.isNull(currentSqlInfo.getConfiguration())) {
        currentSqlInfo.setConfiguration(configuration);
        // 此时需要设置sqlInfo里面的
        if (Objects.isNull(currentSqlInfo.getPrimaryKey().getTypeHandler())) {
            currentSqlInfo.getPrimaryKey().setTypeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(currentSqlInfo.getPrimaryKey().getTargetClass()));
        }
        for (PropertyColumnMapping pcm : currentSqlInfo.getPropertyColumnMappings()) {
            if (Objects.isNull(pcm.getTypeHandler())) {
                pcm.setTypeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(pcm.getTargetClass()));
            }
        }
    }
    if (currentSqlInfo.hasEnumField() && !resultMaps.containsKey(actualMapperClass)) {
        synchronized (resultMapWriteLock) {
            if (!resultMaps.containsKey(actualMapperClass)) {
                initResultMap(currentSqlInfo, actualMapperClass);
            }
        }
    }
}
Also used : SqlInfo(com.qiuyj.mybatis.SqlInfo) PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping)

Example 2 with PropertyColumnMapping

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

the class AbstractSqlGeneratorEngine method initResultMap.

private void initResultMap(SqlInfo sqlInfo, Class<? extends Mapper<?, ?>> mapperClass) {
    List<ResultMapping> resultMappings = new ArrayList<>(sqlInfo.getFieldCount());
    for (PropertyColumnMapping pcm : sqlInfo.getWithoutPrimaryKey()) {
        resultMappings.add(new ResultMapping.Builder(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getJavaClassPropertyName(), pcm.getTypeHandler()).build());
    }
    resultMappings.add(0, new ResultMapping.Builder(sqlInfo.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).flags(Collections.singletonList(ResultFlag.ID)).build());
    resultMaps.put(mapperClass, new ResultMap.Builder(sqlInfo.getConfiguration(), "mapper-" + mapperClass.getName(), sqlInfo.getBeanType(), resultMappings).build());
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping)

Example 3 with PropertyColumnMapping

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

the class SqlProvider method update.

public ReturnValueWrapper update(MappedStatement ms, SqlInfo sqlInfo, Object args) {
    checkPrimaryKey(sqlInfo);
    checkBeanType(sqlInfo.getBeanType(), args);
    BeanExampleResolver exampleResolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
    if (!exampleResolver.hasPrimaryKeyAndNotDefault()) {
        throw new NoPrimaryKeyException("primary key is default value");
    } else {
        List<PropertyColumnMapping> nonNullColumns = exampleResolver.getWithoutPrimaryKey();
        if (nonNullColumns.isEmpty()) {
            throw new IllegalStateException("Please update at least one column");
        } else {
            List<ParameterMapping> parameterMappings = new ArrayList<>(nonNullColumns.size() + 1);
            SQL sql = new SQL();
            sql.UPDATE(sqlInfo.getTableName());
            for (PropertyColumnMapping pcm : nonNullColumns) {
                sql.SET(pcm.getDatabaseColumnName() + " = ?");
                parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            }
            sql.WHERE(sqlInfo.getPrimaryKey().getDatabaseColumnName() + " = ?");
            parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).build());
            return new ReturnValueWrapper(new StaticTextSqlNode(sql.toString()), parameterMappings);
        }
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) BeanExampleResolver(com.qiuyj.mybatis.BeanExampleResolver) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 4 with PropertyColumnMapping

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

the class SqlProvider method insert.

public ReturnValueWrapper insert(MappedStatement ms, SqlInfo sqlInfo) {
    // 不建议以下这种方式创建SQL,这样会增加编译之后的字节码文件数量
    // 因为这样会创建一个匿名内部类
    /*SQL sql = new SQL() {
      {
        INSERT_INTO(sqlInfo.getTableName());
        INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias());
        String[] prepareColumnValues = new String[sqlInfo.getFiledCount()];
        Arrays.fill(prepareColumnValues, PREPARE_FLAG);
        INTO_VALUES(prepareColumnValues);
      }
    };*/
    String[] prepareColumnValues = new String[sqlInfo.getFieldCount()];
    Arrays.fill(prepareColumnValues, PREPARE_FLAG);
    String sql = new SQL().INSERT_INTO(sqlInfo.getTableName()).INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias()).INTO_VALUES(prepareColumnValues).toString();
    List<ParameterMapping> parameterMappings = new ArrayList<>(sqlInfo.getFieldCount());
    TypeHandlerRegistry reg = ms.getConfiguration().getTypeHandlerRegistry();
    ParameterMapping.Builder parameterBuilder;
    for (PropertyColumnMapping mapping : sqlInfo.getPropertyColumnMappings()) {
        parameterBuilder = new ParameterMapping.Builder(ms.getConfiguration(), mapping.getJavaClassPropertyName(), mapping.getTypeHandler());
        parameterMappings.add(parameterBuilder.build());
    }
    return new ReturnValueWrapper(new StaticTextSqlNode(sql), parameterMappings);
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 5 with PropertyColumnMapping

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

the class SqlProvider method selectList.

public ReturnValueWrapper selectList(MappedStatement ms, SqlInfo sqlInfo, Object args) {
    checkBeanType(sqlInfo.getBeanType(), args);
    BeanExampleResolver resolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
    List<PropertyColumnMapping> exampleSelectList = resolver.selectExample();
    if (exampleSelectList.isEmpty()) {
        throw new IllegalStateException("Please specify at least one condition");
    } else {
        SQL sql = new SQL().SELECT(sqlInfo.getAllColumnsWithAlias()).FROM(sqlInfo.getTableName());
        List<ParameterMapping> parameterMappings = new ArrayList<>(exampleSelectList.size());
        for (PropertyColumnMapping pcm : exampleSelectList) {
            parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
        }
        return new ReturnValueWrapper(new StaticTextSqlNode(sql.toString()), parameterMappings);
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) BeanExampleResolver(com.qiuyj.mybatis.BeanExampleResolver) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Aggregations

PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)9 ArrayList (java.util.ArrayList)4 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)4 SQL (org.apache.ibatis.jdbc.SQL)3 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)3 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)2 Column (com.qiuyj.mybatis.annotation.Column)2 SqlInfo (com.qiuyj.mybatis.SqlInfo)1 Enumerated (com.qiuyj.mybatis.annotation.Enumerated)1 PrimaryKey (com.qiuyj.mybatis.annotation.PrimaryKey)1 Sequence (com.qiuyj.mybatis.key.Sequence)1 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 EnumOrdinalTypeHandler (org.apache.ibatis.type.EnumOrdinalTypeHandler)1 EnumTypeHandler (org.apache.ibatis.type.EnumTypeHandler)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1