mybatis / mybatis-3

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

More Scalable @SelectProvider #320

Closed IamFive closed 8 years ago

IamFive commented 9 years ago

Current SelectProvider support use paramObject as arg and return String as result.

String sql(paramObject)

I hope it can support :

[String|SqlNode] sql([paramObject, [mappedStatement]])

Basicly, I want to implement a "common crud" mapper. I hope i can dynamic build sql and control return type for the mapped statement.

The current @SelectProvider is useless to me, I didn't see any advantage to xml defined sql. But if we can get mappedStatement in the sql build process, we can dynamic change the whole statement, like ReturnType.

Code I want to implement:

public interface CrudMapper<T> {
       @SelectProvider(type=CrudMapperImpl.class, method="select")
       List<T> select(T example);
}

public interface SomeMapper extends CrudMapper<Some> {
}

public interface Some2Mapper extends CrudMapper<Some2> {
}

public class CrudMapperImpl {

        public SqlNode(Object param, MappedStatement ms) {
                getEntityClass(ms);
                ms.setReturnType(getEntityClass(ms));
                BEGIN();
                SELECT("xxxxx");
                FROM(tableName);
                //...............
                //..........
        }
}
IamFive commented 9 years ago

if we can have @SqlSource annotation is an option too.

@SqlSource(class=xx, method=xx) List select(T example);

elw00d commented 9 years ago

You can try to use @Lang annotation and provide custom LanguageDriver. See velocity-scripting project for example.

ghost commented 8 years ago

How a java annotation propertity set a generic class,just like:

 @InsertProvider(type = BaseSQLProvider<Entity>.class, method = "insertSQL")
IamFive commented 8 years ago

@jast90 forget it,
I create another issue before https://github.com/mybatis/mybatis-3/issues/321, It's due to mybatis3's bug, the location is link in the issue.

harawata commented 8 years ago

Have you checked @abel533 's common mapper ? I cannot read Chinese, but it seems like what you guys are looking for.

IamFive commented 8 years ago

Sure, it's a nice common mapper, but it's something like "monkey path", inject map statement which make architecture not so intuitive

ghost commented 8 years ago

@harawata If you want implement common mapper,how you do ?Could you share me you idea?

harawata commented 8 years ago

@jast90 I use mybatis-generator for tables that do not need complex queries.

Anyway, I will look into #321 maybe this weekend.

abel533 commented 8 years ago

@harawata Common Mapper is like mybatis-generator, but it generator MappedStatement at the runtime.

By the default, you doesn't need to use JPA.

harawata commented 8 years ago

Hi guys,

The fix for #321 is available in the latest snapshot. Can we close this issue now?

@abel533 Hope the fix helps you improve your common mapper. Nice work! :+1:

IamFive commented 8 years ago

ok will check it later