欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis mapper动态代理的原理解析

程序员文章站 2022-06-27 15:01:29
前言 在开始动态代理的原理讲解以前,我们先看一下集成mybatis以后dao层不使用动态代理以及使用动态代理的两种实现方式,通过对比我们自己实现dao层接口以及mybat...

前言

在开始动态代理的原理讲解以前,我们先看一下集成mybatis以后dao层不使用动态代理以及使用动态代理的两种实现方式,通过对比我们自己实现dao层接口以及mybatis动态代理可以更加直观的展现出mybatis动态代理替我们所做的工作,有利于我们理解动态代理的过程,讲解完以后我们再进行动态代理的原理解析,此讲解基于mybatis的环境已经搭建完成,并且已经实现了基本的用户类编写以及用户类的dao接口的声明,下面是dao层的接口代码

public interface userdao {
 /*
 查询所有用户信息
  */
 list<user> findall();
 /**
  * 保存用户
  * @param user
  */
 void save(user user);

 /**
  * 更新用户
  * @return
  */
 void update(user user);
 /**
  * 删除用户
  */
 void delete(integer userid);
 /**
  * 查找一个用户
  * @param userid
  * @return
  */
 user findone(integer userid);
 /**
  * 根据名字模糊查询
  * @param name
  * @return
  */
 list<user> findbyname(string name);
 /**
  * 根据组合对象进行模糊查询
  * @param vo
  * @return
  */
 list<user> findbyqueryvo(queryvo vo);
}

一、mybatis dao层两种实现方式的对比

1.dao层不使用动态代理

dao层不使用动态代理的话,就需要我们自己实现dao层的接口,为了简便起见,我只是实现了dao接口中的findall方法,以此方法为例子来展现我们自己实现dao的方式的情况,让我们来看代码:

public class userdaoimpl implements userdao{
 private sqlsessionfactory factory;
 public userdaoimpl(sqlsessionfactory factory){
  this.factory = factory;
 }
 public list<user> findall() {
  //1.获取sqlsession对象
  sqlsession sqlsession = factory.opensession();
  //2.调用selectlist方法
  list<user> list = sqlsession.selectlist("com.example.dao.userdao.findall");
  //3.关闭流
  sqlsession.close();
  return list;
 }
 public void save(user user) {
 }
 public void update(user user) {
 }
 public void delete(integer userid) {
 }
 public user findone(integer userid) {
  return null;
 }
 public list<user> findbyname(string name) {
  return null;
 }
 public list<user> findbyqueryvo(queryvo vo) {
  return null;
 }

这里的关键代码 list<user> list = sqlsession.selectlist("com.example.dao.userdao.findall"),需要我们自己手动调用sqlsession里面的方法,基于动态代理的方式最后的目标也是成功的调用到这里。

注意:如果是添加,更新或者删除操作的话需要在方法中增加事务的提交。

2.dao层使用mybatis的动态代理

使用动态代理的话dao层的接口声明完成以后只需要在使用的时候通过sqlsession对象的getmapper方法获取对应dao接口的代理对象,关键代码如下:

//3.获取sqlsession对象
sqlsession session = factory.opensession();
//4.获取dao的代理对象
userdao mapper = session.getmapper(userdao.class);
//5.执行查询所有的方法
list<user> list = mapper.findall();

获取到dao层的代理对象以后通过代理对象调用查询方法就可以实现查询所有用户列表的功能。

二、mybatis动态代理实现方式的原理解析

动态代理中最重要的类:sqlsession、mapperproxy、mappermethod,下面开始从入口方法到调用结束的过程分析。

1.调用方法的开始:

//4.获取dao的代理对象

userdao mapper = session.getmapper(userdao.class); 因为sqlsesseion为接口,所以我们通过debug方式发现这里使用的实现类为defaultsqlsession。

2.找到deaultsqlsession中的getmapper方法,发现这里没有做其他的动作,只是将工作继续抛到了configuration类中,configuration为类不是接口,可以直接进入该类的getmapper方法中

@override
 public <t> t getmapper(class<t> type) {
 return configuration.<t>getmapper(type, this);
 }

3. 找到configuration类的getmapper方法,这里也是将工作继续交到mapperregistry的getmapper的方法中,所以我们继续向下进行。

 public <t> t getmapper(class<t> type, sqlsession sqlsession) {
 return mapperregistry.getmapper(type, sqlsession);
 }

4. 找到mapperregistry的getmapper的方法,看到这里发现和以前不一样了,通过mapperproxyfactory的命名方式我们知道这里将通过这个工厂生成我们所关注的mapperproxy的代理类,然后我们通过mapperproxyfactory.newinstance(sqlsession);进入mapperproxyfactory的newinstance方法中

public <t> t getmapper(class<t> type, sqlsession sqlsession) {
 final mapperproxyfactory<t> mapperproxyfactory = (mapperproxyfactory<t>) knownmappers.get(type);
 if (mapperproxyfactory == null) {
  throw new bindingexception("type " + type + " is not known to the mapperregistry.");
 }
 try {
  return mapperproxyfactory.newinstance(sqlsession);
 } catch (exception e) {
  throw new bindingexception("error getting mapper instance. cause: " + e, e);
 }
 }

5. 找到mapperproxyfactory的newintance方法,通过参数类型sqlsession可以得知,上面的调用先进入第二个newinstance方法中并创建我们所需要重点关注的mapperproxy对象,第二个方法中再调用第一个newinstance方法并将mapperproxy对象传入进去,根据该对象创建代理类并返回。这里已经得到需要的代理类了,但是我们的代理类所做的工作还得继续向下看mapperproxy类。

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);
 }

6. 找到mapperproxy类,发现其确实实现了jdk动态代理必须实现的接口invocationhandler,所以我们重点关注invoke()方法,这里看到在invoke方法里先获取mappermethod类,然后调用mappermethod.execute(),所以我们继续查看mappermethod类的execute方法。

public class mapperproxy<t> implements invocationhandler, serializable {
 private static final long serialversionuid = -6424540398559729838l;
 private final sqlsession sqlsession;
 private final class<t> mapperinterface;
 private final map<method, mappermethod> methodcache;
 public mapperproxy(sqlsession sqlsession, class<t> mapperinterface, map<method, mappermethod> methodcache) {
 this.sqlsession = sqlsession;
 this.mapperinterface = mapperinterface;
 this.methodcache = methodcache;
 }

 @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);
 }

 private mappermethod cachedmappermethod(method method) {
 mappermethod mappermethod = methodcache.get(method);
 if (mappermethod == null) {
  mappermethod = new mappermethod(mapperinterface, method, sqlsession.getconfiguration());
  methodcache.put(method, mappermethod);
 }
 return mappermethod;
 }

 @usesjava7
 private object invokedefaultmethod(object proxy, method method, object[] args)
  throws throwable {
 final constructor<methodhandles.lookup> constructor = methodhandles.lookup.class
  .getdeclaredconstructor(class.class, int.class);
 if (!constructor.isaccessible()) {
  constructor.setaccessible(true);
 }
 final class<?> declaringclass = method.getdeclaringclass();
 return constructor
  .newinstance(declaringclass,
   methodhandles.lookup.private | methodhandles.lookup.protected
    | methodhandles.lookup.package | methodhandles.lookup.public)
  .unreflectspecial(method, declaringclass).bindto(proxy).invokewitharguments(args);
 }
 /**
 * backport of java.lang.reflect.method#isdefault()
 */
 private boolean isdefaultmethod(method method) {
 return ((method.getmodifiers()
  & (modifier.abstract | modifier.public | modifier.static)) == modifier.public)
  && method.getdeclaringclass().isinterface();
 }
}

7. 找到类mappermethod类的execute方法,发现execute中通过调用本类中的其他方法获取并封装返回结果,我们来看一下mappermethod整个类。

public object execute(sqlsession sqlsession, object[] args) {
 object result;
 switch (command.gettype()) {
  case insert: {
  object param = method.convertargstosqlcommandparam(args);
  result = rowcountresult(sqlsession.insert(command.getname(), param));
  break;
  }
  case update: {
  object param = method.convertargstosqlcommandparam(args);
  result = rowcountresult(sqlsession.update(command.getname(), param));
  break;
  }
  case delete: {
  object param = method.convertargstosqlcommandparam(args);
  result = rowcountresult(sqlsession.delete(command.getname(), param));
  break;
  }
  case select:
  if (method.returnsvoid() && method.hasresulthandler()) {
   executewithresulthandler(sqlsession, args);
   result = null;
  } else if (method.returnsmany()) {
   result = executeformany(sqlsession, args);
  } else if (method.returnsmap()) {
   result = executeformap(sqlsession, args);
  } else if (method.returnscursor()) {
   result = executeforcursor(sqlsession, args);
  } else {
   object param = method.convertargstosqlcommandparam(args);
   result = sqlsession.selectone(command.getname(), param);
  }
  break;
  case flush:
  result = sqlsession.flushstatements();
  break;
  default:
  throw new bindingexception("unknown execution method for: " + command.getname());
 }
 if (result == null && method.getreturntype().isprimitive() && !method.returnsvoid()) {
  throw new bindingexception("mapper method '" + command.getname() 
   + " attempted to return null from a method with a primitive return type (" + method.getreturntype() + ").");
 }
 return result;
 }

8. mappermethod类是整个代理机制的核心类,对sqlsession中的操作进行了封装使用。

该类里有两个内部类sqlcommand和methodsignature。 sqlcommand用来封装crud操作,也就是我们在xml中配置的操作的节点。每个节点都会生成一个mappedstatement类。

methodsignature用来封装方法的参数以及返回类型,在execute的方法中我们发现在这里又回到了sqlsession中的接口调用,和我们自己实现uerdao接口的方式中直接用sqlsession对象调用defaultsqlsession的实现类的方法是一样的,经过一大圈的代理又回到了原地,这就是整个动态代理的实现过程了。

public class mappermethod {
 private final sqlcommand command;
 private final methodsignature method;
 public mappermethod(class<?> mapperinterface, method method, configuration config) {
 this.command = new sqlcommand(config, mapperinterface, method);
 this.method = new methodsignature(config, mapperinterface, method);
 }
 public object execute(sqlsession sqlsession, object[] args) {
 object result;
 switch (command.gettype()) {
  case insert: {
  object param = method.convertargstosqlcommandparam(args);
  result = rowcountresult(sqlsession.insert(command.getname(), param));
  break;
  }
  case update: {
  object param = method.convertargstosqlcommandparam(args);
  result = rowcountresult(sqlsession.update(command.getname(), param));
  break;
  }
  case delete: {
  object param = method.convertargstosqlcommandparam(args);
  result = rowcountresult(sqlsession.delete(command.getname(), param));
  break;
  }
  case select:
  if (method.returnsvoid() && method.hasresulthandler()) {
   executewithresulthandler(sqlsession, args);
   result = null;
  } else if (method.returnsmany()) {
   result = executeformany(sqlsession, args);
  } else if (method.returnsmap()) {
   result = executeformap(sqlsession, args);
  } else if (method.returnscursor()) {
   result = executeforcursor(sqlsession, args);
  } else {
   object param = method.convertargstosqlcommandparam(args);
   result = sqlsession.selectone(command.getname(), param);
  }
  break;
  case flush:
  result = sqlsession.flushstatements();
  break;
  default:
  throw new bindingexception("unknown execution method for: " + command.getname());
 }
 if (result == null && method.getreturntype().isprimitive() && !method.returnsvoid()) {
  throw new bindingexception("mapper method '" + command.getname() 
   + " attempted to return null from a method with a primitive return type (" + method.getreturntype() + ").");
 }
 return result;
 }

 private object rowcountresult(int rowcount) {
 final object result;
 if (method.returnsvoid()) {
  result = null;
 } else if (integer.class.equals(method.getreturntype()) || integer.type.equals(method.getreturntype())) {
  result = rowcount;
 } else if (long.class.equals(method.getreturntype()) || long.type.equals(method.getreturntype())) {
  result = (long)rowcount;
 } else if (boolean.class.equals(method.getreturntype()) || boolean.type.equals(method.getreturntype())) {
  result = rowcount > 0;
 } else {
  throw new bindingexception("mapper method '" + command.getname() + "' has an unsupported return type: " + method.getreturntype());
 }
 return result;
 }

 private void executewithresulthandler(sqlsession sqlsession, object[] args) {
 mappedstatement ms = sqlsession.getconfiguration().getmappedstatement(command.getname());
 if (void.class.equals(ms.getresultmaps().get(0).gettype())) {
  throw new bindingexception("method " + command.getname() 
   + " needs either a @resultmap annotation, a @resulttype annotation," 
   + " or a resulttype attribute in xml so a resulthandler can be used as a parameter.");
 }
 object param = method.convertargstosqlcommandparam(args);
 if (method.hasrowbounds()) {
  rowbounds rowbounds = method.extractrowbounds(args);
  sqlsession.select(command.getname(), param, rowbounds, method.extractresulthandler(args));
 } else {
  sqlsession.select(command.getname(), param, method.extractresulthandler(args));
 }
 }

 private <e> object executeformany(sqlsession sqlsession, object[] args) {
 list<e> result;
 object param = method.convertargstosqlcommandparam(args);
 if (method.hasrowbounds()) {
  rowbounds rowbounds = method.extractrowbounds(args);
  result = sqlsession.<e>selectlist(command.getname(), param, rowbounds);
 } else {
  result = sqlsession.<e>selectlist(command.getname(), param);
 }
 // issue #510 collections & arrays support
 if (!method.getreturntype().isassignablefrom(result.getclass())) {
  if (method.getreturntype().isarray()) {
  return converttoarray(result);
  } else {
  return converttodeclaredcollection(sqlsession.getconfiguration(), result);
  }
 }
 return result;
 }

 private <t> cursor<t> executeforcursor(sqlsession sqlsession, object[] args) {
 cursor<t> result;
 object param = method.convertargstosqlcommandparam(args);
 if (method.hasrowbounds()) {
  rowbounds rowbounds = method.extractrowbounds(args);
  result = sqlsession.<t>selectcursor(command.getname(), param, rowbounds);
 } else {
  result = sqlsession.<t>selectcursor(command.getname(), param);
 }
 return result;
 }

 private <e> object converttodeclaredcollection(configuration config, list<e> list) {
 object collection = config.getobjectfactory().create(method.getreturntype());
 metaobject metaobject = config.newmetaobject(collection);
 metaobject.addall(list);
 return collection;
 }
 @suppresswarnings("unchecked")
 private <e> object converttoarray(list<e> list) {
 class<?> arraycomponenttype = method.getreturntype().getcomponenttype();
 object array = array.newinstance(arraycomponenttype, list.size());
 if (arraycomponenttype.isprimitive()) {
  for (int i = 0; i < list.size(); i++) {
  array.set(array, i, list.get(i));
  }
  return array;
 } else {
  return list.toarray((e[])array);
 }
 }
 private <k, v> map<k, v> executeformap(sqlsession sqlsession, object[] args) {
 map<k, v> result;
 object param = method.convertargstosqlcommandparam(args);
 if (method.hasrowbounds()) {
  rowbounds rowbounds = method.extractrowbounds(args);
  result = sqlsession.<k, v>selectmap(command.getname(), param, method.getmapkey(), rowbounds);
 } else {
  result = sqlsession.<k, v>selectmap(command.getname(), param, method.getmapkey());
 }
 return result;
 }
 public static class parammap<v> extends hashmap<string, v> {
 private static final long serialversionuid = -2212268410512043556l;
 @override
 public v get(object key) {
  if (!super.containskey(key)) {
  throw new bindingexception("parameter '" + key + "' not found. available parameters are " + keyset());
  }
  return super.get(key);
 }
 }
 public static class sqlcommand {
 private final string name;
 private final sqlcommandtype type;

 public sqlcommand(configuration configuration, class<?> mapperinterface, method method) {
  final string methodname = method.getname();
  final class<?> declaringclass = method.getdeclaringclass();
  mappedstatement ms = resolvemappedstatement(mapperinterface, methodname, declaringclass,
   configuration);
  if (ms == null) {
  if (method.getannotation(flush.class) != null) {
   name = null;
   type = sqlcommandtype.flush;
  } else {
   throw new bindingexception("invalid bound statement (not found): "
    + mapperinterface.getname() + "." + methodname);
  }
  } else {
  name = ms.getid();
  type = ms.getsqlcommandtype();
  if (type == sqlcommandtype.unknown) {
   throw new bindingexception("unknown execution method for: " + name);
  }
  }
 }
 public string getname() {
  return name;
 }
 public sqlcommandtype gettype() {
  return type;
 }
 private mappedstatement resolvemappedstatement(class<?> mapperinterface, string methodname,
  class<?> declaringclass, configuration configuration) {
  string statementid = mapperinterface.getname() + "." + methodname;
  if (configuration.hasstatement(statementid)) {
  return configuration.getmappedstatement(statementid);
  } else if (mapperinterface.equals(declaringclass)) {
  return null;
  }
  for (class<?> superinterface : mapperinterface.getinterfaces()) {
  if (declaringclass.isassignablefrom(superinterface)) {
   mappedstatement ms = resolvemappedstatement(superinterface, methodname,
    declaringclass, configuration);
   if (ms != null) {
   return ms;
   }
  }
  }
  return null;
 }
 }

 public static class methodsignature {
 private final boolean returnsmany;
 private final boolean returnsmap;
 private final boolean returnsvoid;
 private final boolean returnscursor;
 private final class<?> returntype;
 private final string mapkey;
 private final integer resulthandlerindex;
 private final integer rowboundsindex;
 private final paramnameresolver paramnameresolver;

 public methodsignature(configuration configuration, class<?> mapperinterface, method method) {
  type resolvedreturntype = typeparameterresolver.resolvereturntype(method, mapperinterface);
  if (resolvedreturntype instanceof class<?>) {
  this.returntype = (class<?>) resolvedreturntype;
  } else if (resolvedreturntype instanceof parameterizedtype) {
  this.returntype = (class<?>) ((parameterizedtype) resolvedreturntype).getrawtype();
  } else {
  this.returntype = method.getreturntype();
  }
  this.returnsvoid = void.class.equals(this.returntype);
  this.returnsmany = (configuration.getobjectfactory().iscollection(this.returntype) || this.returntype.isarray());
  this.returnscursor = cursor.class.equals(this.returntype);
  this.mapkey = getmapkey(method);
  this.returnsmap = (this.mapkey != null);
  this.rowboundsindex = getuniqueparamindex(method, rowbounds.class);
  this.resulthandlerindex = getuniqueparamindex(method, resulthandler.class);
  this.paramnameresolver = new paramnameresolver(configuration, method);
 }

 public object convertargstosqlcommandparam(object[] args) {
  return paramnameresolver.getnamedparams(args);
 }

 public boolean hasrowbounds() {
  return rowboundsindex != null;
 }

 public rowbounds extractrowbounds(object[] args) {
  return hasrowbounds() ? (rowbounds) args[rowboundsindex] : null;
 }

 public boolean hasresulthandler() {
  return resulthandlerindex != null;
 }

 public resulthandler extractresulthandler(object[] args) {
  return hasresulthandler() ? (resulthandler) args[resulthandlerindex] : null;
 }

 public string getmapkey() {
  return mapkey;
 }

 public class<?> getreturntype() {
  return returntype;
 }

 public boolean returnsmany() {
  return returnsmany;
 }

 public boolean returnsmap() {
  return returnsmap;
 }

 public boolean returnsvoid() {
  return returnsvoid;
 }

 public boolean returnscursor() {
  return returnscursor;
 }
 private integer getuniqueparamindex(method method, class<?> paramtype) {
  integer index = null;
  final class<?>[] argtypes = method.getparametertypes();
  for (int i = 0; i < argtypes.length; i++) {
  if (paramtype.isassignablefrom(argtypes[i])) {
   if (index == null) {
   index = i;
   } else {
   throw new bindingexception(method.getname() + " cannot have multiple " + paramtype.getsimplename() + " parameters");
   }
  }
  }
  return index;
 }
 private string getmapkey(method method) {
  string mapkey = null;
  if (map.class.isassignablefrom(method.getreturntype())) {
  final mapkey mapkeyannotation = method.getannotation(mapkey.class);
  if (mapkeyannotation != null) {
   mapkey = mapkeyannotation.value();
  }
  }
  return mapkey;
 }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。