Search in sources :

Example 1 with Column

use of com.github.dreamroute.mybatis.pro.core.annotations.Column 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)

Aggregations

Column (com.github.dreamroute.mybatis.pro.core.annotations.Column)1 Id (com.github.dreamroute.mybatis.pro.core.annotations.Id)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Reflector (org.apache.ibatis.reflection.Reflector)1