在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 請問mybatis-generator怎么自動生成mapper接口方法的實現(xiàn)?

請問mybatis-generator怎么自動生成mapper接口方法的實現(xiàn)?

在網(wǎng)上找過了,只能生成實體類,mapper接口,xml,請問怎么生成接口方法的實現(xiàn),還是說沒有這個功能?

回答
編輯回答
不舍棄

mybatis不需要實現(xiàn)Mapper接口,mybatis框架有個binding模塊,會生成代理來實現(xiàn)Mapper接口。

2018年2月25日 06:39
編輯回答
北城荒

mybatis的mapper是通過動態(tài)代理來實現(xiàn)的。
關(guān)于動態(tài)代理的API文檔:https://docs.oracle.com/javas...
動態(tài)代理類顧名思義即是JVM在運(yùn)行時動態(tài)產(chǎn)生的一個新類,具體可以查閱相關(guān)文檔。
mybatis中由MapperProxyFactory產(chǎn)生mapper類和實例:

public class MapperProxyFactory<T> {

  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();

  public MapperProxyFactory(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }

  public Class<T> getMapperInterface() {
    return mapperInterface;
  }

  public Map<Method, MapperMethod> getMethodCache() {
    return methodCache;
  }

  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

}

MapperProxy實現(xiàn)InvocationHandler了(此處mapperProxy并非提問所說的mapper接口的代理類,真正的代理類和實例是在MapperProxyFactory中產(chǎn)生的):

class MapperProxy implements InvocationHandler, Serializable {
    ...
@Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

  ...
}

具體的執(zhí)行是在這里:
final MapperMethod mapperMethod = cachedMapperMethod(method);
mapperMethod.execute(sqlSession, args);

有session和映射的sql語句,即可完成執(zhí)行一個語句的動作。

詳細(xì)可以看這篇blog: Mybais之mapperproxy
關(guān)于動態(tài)代理延伸閱讀:
Java 動態(tài)代理機(jī)制分析及擴(kuò)展,第 1 部分
Java 動態(tài)代理機(jī)制分析及擴(kuò)展,第 2 部分

2018年8月22日 20:55
編輯回答
挽青絲

在generatorConfig.xml中設(shè)置了通用Mapper(或者自定義的Mapper繼承了此通用Mapper)
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">

        <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>

</plugin>
mybatis-generator自動生成的Mapper會自動繼承在這里設(shè)置的Mapper,tk.mybatis.mapper.common.Mapper包含了所有基本的單表的CRUD的實現(xiàn),可以直接調(diào)用無需自己寫接口實現(xiàn),當(dāng)需要繼續(xù)多表操作的時候,可以自己在接口中加方法并實現(xiàn)和普通的mybatis一樣的用法。

2017年9月18日 13:18
編輯回答
敢試

生產(chǎn)的mapper接口有繼承tk.mybatis.mapper.common.Mapper,常用操作都已經(jīng)有了,不需要在xml中寫實現(xiàn)。

可以參考:http://blog.csdn.net/isea533/...
還有:https://github.com/abel533/My...

2017年6月30日 11:58