简体   繁体   中英

How to use mybatis mapper interface with generic type (BaseMapper<T>)?

I am in using mybatis 3.2.2, and all mapper interface extends a base interface, code like this: base interface:

public interface BaseMapper<T>{
   public int insert(T record);
   public int insertSelective(T record);
}

public interface JobMapper extends BaseMapper<Job>{
}

then i test the inert method,

jobMapper.insert(job);

the error is :

java.lang.NoSuchMethodError: com.xxx.framework.dao.ifaces.JobMapper.insert(Lcom/xxx/framework/model/Job;)I

But if like this:

public interface BaseMapper{
   public int insert(Job record);
   public int insertSelective(Job  record);
}

public interface JobMapper  extends BaseMapper{
}

the result is correct.

I really want to use generic base interface to implements some common method, like add,update,delete etc. Can someone tell me ?

I have a working example of generic base interfaces in one of my projects so this is possible with the proper mybatis configuration. However it is hard to tell what's wrong in your situation as you didn't attach your mybatis configs.

I think you could try to add type aliases package configuration (if you don't already have it) to solve this issue:

<typeAliases>
    <package name="com.xxx.framework.model"/>
</typeAliases>

See the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM