Can not get Mapper name in LanguageDriver

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can not get Mapper name in LanguageDriver

李伟棠
I want to create BaseMapper by LanguageDriver, but I can't get Mapper name.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can not get Mapper name in LanguageDriver

Kazuki Shimizu
Could you explain more detail and use cases ?

On Wednesday, May 3, 2017 at 6:10:37 PM UTC+9, 李伟棠 wrote:
I want to create BaseMapper by LanguageDriver, but I can't get Mapper name.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can not get Mapper name in LanguageDriver

李伟棠
@Mapper
public interface ExampleMapper extends BaseMapper<Example, Long, ExampleCondition> {}

public interface BaseMapper<T, K, C> {
   @Lang(BaseMapperDriver.class)
   @Select("select * from ${table} where ${id}=#{id}")
   public T getById(@Param("id") K id);
}

public class BaseMapperDriver extends XMLLanguageDriver {
   @Override
   public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
       // I want to get the current processing Mapper class, and replace ${table}, ${id} value, but I can't.
       // Now, I must override org.apache.ibatis.binding.MapperRegistry
       // and org.apache.ibatis.session.Configuration , it's very trouble.
       return super.createSqlSource(configuration, script, parameterType);
   }
}


在 2017年5月3日星期三 UTC+8下午5:30:12,Kazuki Shimizu写道:
Could you explain more detail and use cases ?

On Wednesday, May 3, 2017 at 6:10:37 PM UTC+9, 李伟棠 wrote:
I want to create BaseMapper by LanguageDriver, but I can't get Mapper name.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can not get Mapper name in LanguageDriver

Kazuki Shimizu
Hi Iwao,

How about thinks this ?

On Friday, May 5, 2017 at 12:40:39 PM UTC+9, 李伟棠 wrote:
@Mapper
public interface ExampleMapper extends BaseMapper<Example, Long, ExampleCondition> {}

public interface BaseMapper<T, K, C> {
   @Lang(BaseMapperDriver.class)
   @Select("select * from ${table} where ${id}=#{id}")
   public T getById(@Param("id") K id);
}

public class BaseMapperDriver extends XMLLanguageDriver {
   @Override
   public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
       // I want to get the current processing Mapper class, and replace ${table}, ${id} value, but I can't.
       // Now, I must override org.apache.ibatis.binding.MapperRegistry
       // and org.apache.ibatis.session.Configuration , it's very trouble.
       return super.createSqlSource(configuration, script, parameterType);
   }
}


在 2017年5月3日星期三 UTC+8下午5:30:12,Kazuki Shimizu写道:
Could you explain more detail and use cases ?

On Wednesday, May 3, 2017 at 6:10:37 PM UTC+9, 李伟棠 wrote:
I want to create BaseMapper by LanguageDriver, but I can't get Mapper name.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can not get Mapper name in LanguageDriver

Iwao AVE!
Using mapper class in a language driver may not be the right approach.

The closest I can think of right now is to use a default method.

public interface BaseMapper<T, K, C> {
   @Select("select * from ${tableName} where ${idColumn}=#{id}")
   public T _getById(
     @Param("id") K id,
     @Param("tableName") String tableName,
     @Param("idColumn") String idColumn);

  default T getById(K id) throws Exception {
    return _getById(id, getTableName(), getIdColumn());
  }

  String getIdColumn();

  String getTableName();
}
Sub-interfaces must implement the last two methods to provide table name and id column name.

Technically, it's possible to get the runtime mapper class in the default method using reflection, but there is no guarantee that it works with future versions of MyBatis.

Regards,
Iwao

2017-05-16 1:36 GMT+09:00 Kazuki Shimizu <[hidden email]>:
Hi Iwao,

How about thinks this ?


On Friday, May 5, 2017 at 12:40:39 PM UTC+9, 李伟棠 wrote:
@Mapper
public interface ExampleMapper extends BaseMapper<Example, Long, ExampleCondition> {}

public interface BaseMapper<T, K, C> {
   @Lang(BaseMapperDriver.class)
   @Select("select * from ${table} where ${id}=#{id}")
   public T getById(@Param("id") K id);
}

public class BaseMapperDriver extends XMLLanguageDriver {
   @Override
   public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
       // I want to get the current processing Mapper class, and replace ${table}, ${id} value, but I can't.
       // Now, I must override org.apache.ibatis.binding.MapperRegistry
       // and org.apache.ibatis.session.Configuration , it's very trouble.
       return super.createSqlSource(configuration, script, parameterType);
   }
}


在 2017年5月3日星期三 UTC+8下午5:30:12,Kazuki Shimizu写道:
Could you explain more detail and use cases ?

On Wednesday, May 3, 2017 at 6:10:37 PM UTC+9, 李伟棠 wrote:
I want to create BaseMapper by LanguageDriver, but I can't get Mapper name.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...