mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.7k stars 12.82k forks source link

@MappedColumn #943

Open toxanbi4 opened 7 years ago

toxanbi4 commented 7 years ago

MyBatis version

3.4.2

Few changes and addition

package org.apache.ibatis.reflection;
public class Reflector {
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
    private Class<?> type;
.................................................................................................................................................................................................
    private Map<String, String> caseInsensitivePropertyMap = new HashMap();
    private Map<String, String> caseInsensitiveMappedPropertyMap = new HashMap();
    public Reflector(Class<?> clazz) {
        this.type = clazz;
        addDefaultConstructor(clazz);
        addGetMethods(clazz);
        addSetMethods(clazz);
        addFields(clazz);
..............................................................................................................................................................................................................
        for (String propName : this.readablePropertyNames) {
            try {
                MappedColumn mappedColumn = clazz.getDeclaredField(propName)
                        .getAnnotation(MappedColumn.class);
                if (mappedColumn != null) {
                    this.caseInsensitiveMappedPropertyMap.put(mappedColumn
                            .name().toUpperCase(Locale.ENGLISH), propName);
                }
            } catch (Exception e) {
            }
        }
        for (String propName : this.writeablePropertyNames) {
            try {
                MappedColumn mappedColumn = clazz.getDeclaredField(propName)
                        .getAnnotation(MappedColumn.class);
                if (mappedColumn != null) {
                    this.caseInsensitiveMappedPropertyMap.put(mappedColumn
                            .name().toUpperCase(Locale.ENGLISH), propName);
                }
            } catch (Exception e) {
            }
        }
    }
.....................................................................................................................................................................................................
    public String findPropertyName(String name) {
        String propertyName = (String) this.caseInsensitivePropertyMap.get(name
                .toUpperCase(Locale.ENGLISH));
        if (propertyName == null)
            propertyName = (String) this.caseInsensitiveMappedPropertyMap
                    .get(name.toUpperCase(Locale.ENGLISH));
        return propertyName;
    }
}
package org.apache.ibatis.annotations;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface MappedColumn {
    String name();
}
public class Enterprise {
    @MappedColumn(name = "ID_ENTERPRISE")
    private Long idEnterprise;
..............................................................................
}
kazuki43zoo commented 7 years ago

Please add more detail explanation for this issue.

toxanbi4 commented 7 years ago

This is an analog of the annotation @Column in JPA. It's need change one existing class and add one new annotation interface. This is only a draft version, but it's worker

harawata commented 7 years ago

Looks similar to #717 .