Search in sources :

Example 1 with Sequence

use of com.qiuyj.mybatis.key.Sequence 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)

Aggregations

PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)1 Column (com.qiuyj.mybatis.annotation.Column)1 PrimaryKey (com.qiuyj.mybatis.annotation.PrimaryKey)1 Sequence (com.qiuyj.mybatis.key.Sequence)1