Search in sources :

Example 1 with Reflector

use of org.apache.ibatis.reflection.Reflector in project mybatis-pro by Dreamroute.

the class ClassUtil method getAllFields.

/**
 * 获取实体所有JavaBean属性:
 * <ol>
 *     <li>JavaBean属性</li>
 *     <li>未被@Transient标记</li>
 * </ol>
 */
public static Set<Field> getAllFields(Class<?> cls) {
    Field[] fs = getFields(cls);
    Reflector r = new Reflector(cls);
    return Arrays.stream(fs).filter(f -> isJavaBeanProp(r, f)).collect(Collectors.toSet());
}
Also used : CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) Arrays(java.util.Arrays) MyBatisProException(com.github.dreamroute.mybatis.pro.core.exception.MyBatisProException) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AnnotatedElementUtils.hasAnnotation(org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation) Id(com.github.dreamroute.mybatis.pro.core.annotations.Id) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JsonUtil.toJsonStr(com.github.dreamroute.mybatis.pro.base.codec.enums.JsonUtil.toJsonStr) Transient(com.github.dreamroute.mybatis.pro.core.annotations.Transient) Mapper(com.github.dreamroute.mybatis.pro.sdk.Mapper) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Method(java.lang.reflect.Method) Collectors.toSet(java.util.stream.Collectors.toSet) Insert(org.apache.ibatis.annotations.Insert) Select(org.apache.ibatis.annotations.Select) Reflector(org.apache.ibatis.reflection.Reflector) Delete(org.apache.ibatis.annotations.Delete) ObjectUtils(org.springframework.util.ObjectUtils) Set(java.util.Set) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ReflectUtil.getFields(cn.hutool.core.util.ReflectUtil.getFields) Entry(java.util.Map.Entry) Function.identity(java.util.function.Function.identity) Update(org.apache.ibatis.annotations.Update) Arrays.stream(java.util.Arrays.stream) Field(java.lang.reflect.Field) Reflector(org.apache.ibatis.reflection.Reflector)

Example 2 with Reflector

use of org.apache.ibatis.reflection.Reflector in project mybatis-pro by Dreamroute.

the class MapperUtil method createSqlFragment.

private void createSqlFragment() {
    Map<String, String> values2Columns = new HashMap<>();
    IdType pkType = new IdType();
    PrimaryKey pk = new PrimaryKey();
    Reflector r = new Reflector(entityCls);
    ReflectionUtils.doWithFields(entityCls, field -> {
        Column colAn = field.getAnnotation(Column.class);
        String column = Optional.ofNullable(colAn).map(Column::value).orElse(SqlUtil.toLine(field.getName(), FIELDS_ALIAS_CACHE.get(entityCls)));
        values2Columns.put(field.getName(), column);
        Id idAn = field.getAnnotation(Id.class);
        if (idAn != null) {
            pkType.type = idAn.type();
            pk.name = field.getName();
        }
    }, f -> isJavaBeanProp(r, f));
    if (pkType.type == Type.IDENTITY) {
        values2Columns.remove(pk.name);
    }
    List<String> columns = new ArrayList<>();
    List<String> values = new ArrayList<>();
    values2Columns.forEach((fieldName, column) -> {
        columns.add(column);
        values.add(fieldName);
    });
    this.insertColumns = columns.stream().collect(Collectors.joining(",", "(", ")"));
    this.insertValues = values.stream().map(column -> "#{" + column + "}").collect(Collectors.joining(",", "(", ")"));
    this.createInsertExcludeNullColumnsAndValues(columns, values);
    this.createUpdateByIdColumns(columns, values);
    this.createUpdateByIdExcludeNullColumns(columns, values);
}
Also used : HashMap(java.util.HashMap) Column(com.github.dreamroute.mybatis.pro.core.annotations.Column) Reflector(org.apache.ibatis.reflection.Reflector) ArrayList(java.util.ArrayList) Id(com.github.dreamroute.mybatis.pro.core.annotations.Id)

Example 3 with Reflector

use of org.apache.ibatis.reflection.Reflector in project yyl_example by Relucent.

the class ReflectorExample method main.

public static void main(String[] args) {
    Reflector reflector = new Reflector(PrivateBean.class);
    // 获得对应属性,大小写不敏感
    String property1 = reflector.findPropertyName("value");
    String property2 = reflector.findPropertyName("VALUE");
    String property3 = reflector.findPropertyName("Value");
    System.out.println(property1);
    System.out.println(property2);
    System.out.println(property3);
}
Also used : Reflector(org.apache.ibatis.reflection.Reflector)

Aggregations

Reflector (org.apache.ibatis.reflection.Reflector)3 Id (com.github.dreamroute.mybatis.pro.core.annotations.Id)2 ArrayList (java.util.ArrayList)2 ReflectUtil.getFields (cn.hutool.core.util.ReflectUtil.getFields)1 JsonUtil.toJsonStr (com.github.dreamroute.mybatis.pro.base.codec.enums.JsonUtil.toJsonStr)1 Column (com.github.dreamroute.mybatis.pro.core.annotations.Column)1 Transient (com.github.dreamroute.mybatis.pro.core.annotations.Transient)1 MyBatisProException (com.github.dreamroute.mybatis.pro.core.exception.MyBatisProException)1 Mapper (com.github.dreamroute.mybatis.pro.sdk.Mapper)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Arrays (java.util.Arrays)1 Arrays.stream (java.util.Arrays.stream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1