Search in sources :

Example 6 with PropertyColumnMapping

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

the class AbstractCommonSqlBuilder method selectList.

@Override
public Object selectList(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(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
        }
        return new StaticSqlSource(sqlInfo.getConfiguration(), 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) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) SQL(org.apache.ibatis.jdbc.SQL)

Example 7 with PropertyColumnMapping

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

the class EnumTypeConditionChecker method doCheck.

@Override
@SuppressWarnings("unchecked")
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
    Enumerated enumerated = AnnotationUtils.findAnnotation(field, Enumerated.class);
    if (Objects.isNull(enumerated)) {
        if (Objects.isNull(preRv.fieldMethod)) {
            try {
                preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
            } catch (Exception e) {
            // ignore
            }
        }
        if (Objects.nonNull(preRv.fieldMethod)) {
            enumerated = AnnotationUtils.findAnnotation(preRv.fieldMethod, Enumerated.class);
        }
    }
    if (Objects.nonNull(enumerated)) {
        Class<?> type = getFieldJavaType(field);
        if (type.isEnum()) {
            PropertyColumnMapping enumMapping = sqlInfo.getPropertyColumnMappingByPropertyName(field.getName());
            TypeHandler enumTypeHandlerType = enumerated.type() == Enumerated.ValueType.ORDINAL ? new EnumOrdinalTypeHandler(type) : new EnumTypeHandler(type);
            enumMapping.setTypeHandler(enumTypeHandlerType);
            enumMapping.setJdbcType(enumerated.jdbcType());
            sqlInfo.setHasEnumField();
        }
    }
    preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
    return preRv;
}
Also used : Enumerated(com.qiuyj.mybatis.annotation.Enumerated) PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumTypeHandler(org.apache.ibatis.type.EnumTypeHandler) TypeHandler(org.apache.ibatis.type.TypeHandler) EnumTypeHandler(org.apache.ibatis.type.EnumTypeHandler)

Example 8 with PropertyColumnMapping

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

the class PrimaryKeyAnnotationChecker method doCheck.

@Override
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
    preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
    if (!sqlInfo.hasPrimaryKey()) {
        boolean hasPrimaryKey = AnnotationUtils.hasAnnotation(field, PrimaryKey.class);
        if (!hasPrimaryKey) {
            try {
                if (Objects.isNull(preRv.fieldMethod)) {
                    preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
                }
                hasPrimaryKey = AnnotationUtils.hasAnnotation(preRv.fieldMethod, PrimaryKey.class);
            } catch (IllegalStateException e) {
            // ignore
            }
        }
        if (hasPrimaryKey) {
            String columnName = null;
            Column column = AnnotationUtils.findAnnotation(field, Column.class);
            if (Objects.nonNull(column)) {
                columnName = column.value();
            } else {
                if (Objects.isNull(preRv.fieldMethod)) {
                    try {
                        preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
                    } catch (IllegalStateException e) {
                    // ignore
                    }
                    if (Objects.nonNull(preRv.fieldMethod)) {
                        column = AnnotationUtils.findAnnotation(preRv.fieldMethod, Column.class);
                        if (Objects.nonNull(column)) {
                            columnName = column.value();
                        }
                    }
                }
            }
            if (StringUtils.isBlank(columnName)) {
                columnName = StringUtils.camelCaseToUnderscore(field.getName());
            }
            Class<?> fieldType = getFieldJavaType(field);
            if (Objects.isNull(sqlInfo.getConfiguration())) {
                sqlInfo.setPrimaryKey(new PropertyColumnMapping(field.getName(), columnName, fieldType));
            } else {
                sqlInfo.setPrimaryKey(new PropertyColumnMapping(field.getName(), columnName, sqlInfo.getConfiguration().getTypeHandlerRegistry().getTypeHandler(fieldType)));
            }
            // 解析@Sequence注解
            Sequence sequence = AnnotationUtils.findAnnotation(field, Sequence.class);
            if (Objects.isNull(sequence)) {
                if (Objects.isNull(preRv.fieldMethod)) {
                    try {
                        preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
                    } catch (IllegalStateException e) {
                    // ignore
                    }
                    if (Objects.nonNull(preRv.fieldMethod)) {
                        sequence = AnnotationUtils.findAnnotation(preRv.fieldMethod, Sequence.class);
                    }
                }
            }
            if (Objects.nonNull(sequence)) {
                String sequenceName = sequence.name();
                if (StringUtils.isBlank(sequenceName)) {
                    throw new IllegalStateException("Sequence name can not be empty");
                } else {
                    sqlInfo.setSequenceName(sequenceName);
                }
            }
            preRv.intValue = ConditionChecker.SKIP_ONE;
        }
    }
    return preRv;
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) Column(com.qiuyj.mybatis.annotation.Column) PrimaryKey(com.qiuyj.mybatis.annotation.PrimaryKey) Sequence(com.qiuyj.mybatis.key.Sequence)

Example 9 with PropertyColumnMapping

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

the class MysqlBatchInsertParameterObjectResolver method resolveParameterObject.

@Override
public List<ParameterMapping> resolveParameterObject(Configuration config, SqlInfo sqlInfo, Object paramObj, SqlNode sqlNode) {
    List list = (List) ParameterResolver.resolveParameter(paramObj).getParameterValues()[0];
    if (list.isEmpty()) {
        throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
    } else {
        this.sqlInfo = sqlInfo;
        this.config = config;
        StringJoiner grammaJoiner = new StringJoiner(",");
        StringJoiner valueJoiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> singleInstanceParameterMapping = new ArrayList<>(sqlInfo.getFieldCount());
        ParameterMapping canonic = new ParameterMapping.Builder(config, "list", batchInsertTypeHandler).build();
        for (PropertyColumnMapping propertyColumnMapping : sqlInfo.getPropertyColumnMappings()) {
            valueJoiner.add(SqlProvider.PREPARE_FLAG);
            singleInstanceParameterMapping.add(canonic);
        }
        List<ParameterMapping> allParameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
        String value = valueJoiner.toString();
        for (int i = 0; i < list.size(); i++) {
            grammaJoiner.add(value);
            allParameterMappings.addAll(singleInstanceParameterMapping);
        }
        // 重新设置StaticTextSqlNode的text属性的值
        resetStaticSqlNode((StaticTextSqlNode) sqlNode, grammaJoiner.toString());
        return allParameterMappings;
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringJoiner(java.util.StringJoiner)

Example 10 with PropertyColumnMapping

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

the class ColumnAnnotationChecker method doCheck.

@Override
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
    String columnName = null;
    Column column = AnnotationUtils.findAnnotation(field, Column.class);
    if (Objects.nonNull(column)) {
        columnName = column.value();
    } else {
        // 查找对应的getter方法
        try {
            if (Objects.isNull(preRv.fieldMethod)) {
                preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
            }
            column = AnnotationUtils.findAnnotation(preRv.fieldMethod, Column.class);
            if (Objects.nonNull(column)) {
                columnName = column.value();
            }
        } catch (IllegalStateException e) {
        // ingore
        }
    }
    if (StringUtils.isBlank(columnName)) {
        columnName = StringUtils.camelCaseToUnderscore(field.getName());
    }
    Class<?> fieldType = getFieldJavaType(field);
    if (Objects.isNull(sqlInfo.getConfiguration())) {
        sqlInfo.addPropertyColumn(new PropertyColumnMapping(field.getName(), columnName, fieldType));
    } else {
        sqlInfo.addPropertyColumn(new PropertyColumnMapping(field.getName(), columnName, sqlInfo.getConfiguration().getTypeHandlerRegistry().getTypeHandler(fieldType)));
    }
    preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
    return preRv;
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) Column(com.qiuyj.mybatis.annotation.Column)

Aggregations

PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)14 ArrayList (java.util.ArrayList)8 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)8 SQL (org.apache.ibatis.jdbc.SQL)6 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)4 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)4 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)3 Column (com.qiuyj.mybatis.annotation.Column)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 EnumOrdinalTypeHandler (org.apache.ibatis.type.EnumOrdinalTypeHandler)2 EnumTypeHandler (org.apache.ibatis.type.EnumTypeHandler)2 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)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 BatchInsertTypeHandler (com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1