Skip to content

Dynamic sql using @SelectProvider #1013

@hj1123zz

Description

@hj1123zz

I'm trying to implement "common crud mapper" and I checked #320 the idea is similar.

Basicly, I want to do something like

public interface BaseMapper<T> {
    @SelectProvider(type = BaseMapperProvider.class, method="selectOne")
    List<T> select(@Param("id") Long id, @Param("columns") String[] columns);
}

public class BaseMapperProvider {
    public String selectOne(Method mapperMethod, Long id, String[] columns) {
        Class<?> entityClass = mapperMethod.getReturnType();
        String tableName = getTableName(entityClass);
        BEGIN();
        SELECT(String.joins(columns, ","));
        FROM(tableName);
        WHERE("id = " + id);
    }
}

Also we can generate sql in JPA way

public interface UserMapper {
    @SelectProvider(type = BaseMapperProvider.class, method="selectByExample")
    User findByUsername(String username);
}

public class BaseMapperProvider {
    public String selectByExample(Method mapperMethod, Object param) {
        Class<?> entityClass = mapperMethod.getReturnType();
        String tableName = getTableName(entityClass);
        String conditionColumn = StringUtils.substringAfter(mapperMethod.getName(), "findBy");
        BEGIN();
        SELECT("*");
        FROM(tableName);
        WHERE(conditionColumn + " = " + param);
    }
}

I checked source code and seems we can just add a Method type parameter to ProviderSqlSource construct like

public ProviderSqlSource(Configuration config, Object provider, Method mapperMethod)

And in MapperAnnotationBuilder

Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType);
return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation, method);

Any ideas?

Metadata

Metadata

Assignees

Labels

enhancementImprove a feature or add a new feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions