Mybatis學習查詢映射過程解析

來源:互聯網
上載者:User
1.功能架構

Mybatis的功能架構分為三層:

(1)API介面層:提供給外部使用的介面API,比如dao層介面。

(2)資料處理層:負責具體的SQL尋找、SQL解析、SQL執行和執行結果映射處理等。它主要的目的是根據調用的請求完成一次資料庫操作。

(3)基礎支撐層:負責最基礎的功能支撐,包括串連管理、交易管理、配置載入和緩衝處理、日誌,這些都是共用的東西,將他們抽取出來作為最基礎的組件。為上層的資料處理層提供最基礎的支撐。

2.核心原理:JAVA動態代理

       代理模式是常用的Java設計模式,它的特徵是代理類與委託類有同樣的介面,代理類主要負責為委託類預先處理訊息、過濾訊息、把訊息轉寄給委託類,以及事後處理訊息等。代理類與委託類之間通常會存在關聯關係,一個代理類的對象與一個委託類的對象關聯,代理類的對象本身並不真正實現服務,而是通過調用委託類的對象的相關方法,來提供特定的服務。分為動態代理和靜態代理。

       動態代理類的位元組碼在程式運行時由Java反射機制動態產生,無需程式員手工編寫它的原始碼。

實現方式: JDK動態代理實現

           效率相對低,被代理類需實現對應的介面 cglib動態代理實現

          效率相對高,產生目標類的子類

public class UserProxy<T> implements InvocationHandler {    private final SqlSession sqlSession;    private final Class<T> mapperInterface;     public UserProxy(SqlSession sqlSession, Class<T> mapperInterface) {        this.sqlSession = sqlSession;        this.mapperInterface = mapperInterface;    }     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        System.out.println(method.getName() + ":" + Arrays.toString(args));        return null;    }}  public interface UserMapper {    UserInfo getUserById(long id);}  public class ProxyTest {    public static void main(String[] args) {        UserProxy userProxy = new UserProxy(null, null);        UserMapper userMapper = (UserMapper)Proxy.newProxyInstance(ProxyTest.class.getClassLoader(),                new Class[]{UserMapper.class},                userProxy);        System.out.println(userMapper.getUserById(1l));    }}
簡單查詢過程:

public class OrderInfoTest {    static Logger log = LogManager.getLogger(OrderInfoTest.class);    public static void main(String[] args) throws IOException {        // 載入設定檔,並擷取session        String resource = "mybatis-config.xml";        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));        SqlSession session = sessionFactory.openSession();        // 擷取可操作的介面        OrderInfoMapper orderInfoMapper = session.getMapper(OrderInfoMapper.class); // 產生動態代理類        //  執行查詢        // OrderInfo orderInfo = session.selectOne("mybatis.study.customer.mapper.OrderInfoMapper.getOrderInfoById", 1L);        OrderInfo orderInfo = orderInfoMapper.getOrderInfoById(1l);        System.out.println(orderInfo.getMoney());    }}

通過調用DAO介面的方法,與直接使用statementId標識的方式,結果是一致的,中間肯定是做了映射關係。

這就說明了為什麼mapper檔案中的namespace必須要與dao層介面的全限定名一致。下面看下映射的過程。 3.mapper(dao)到session使用statementId查詢的映射過程 3.1 sqlSession建立過程主要類的說明

SqlSessionFactoryBuilder:用於建立SqlSessionFactory的執行個體,build方法入參設定檔的資料流

SqlSessionFactory是建立SqlSession執行個體的工廠介面,實作類別有兩個

預設的實現是DefaultSqlSessionFactory,調用openSession擷取session對象,進行操作

/** * * 從設定檔擷取環境、資料來源、事務類型來建立 sqlSession * * @param execType 執行器類型 * @param level 事務層級 * @param autoCommit 是否自動認可 * @return session */private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {    Transaction tx = null;    try {        final Environment environment = configuration.getEnvironment();  // 擷取配置的環境        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);  // 環境的事務工廠, 預設交易管理ManagedTransactionFactory        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);  // 建立事務對象        final Executor executor = configuration.newExecutor(tx, execType);    // 建立執行器        return new DefaultSqlSession(configuration, executor, autoCommit);  // 返回預設的sqlSession    } catch (Exception e) {        closeTransaction(tx);        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);    } finally {        ErrorContext.instance().reset();    }}

而SqlSessionManager,實現了SqlSessionFactory和SqlSession介面,直接具有session的功能,內部封裝 DefaultSqlSessionFactory,是DefaultSqlSessionFactory的加強版。

總之,通過上述的過程得到可操作的session,其中最重要的就是Configuration的構建,下面說明下Configuration的解析過程 3.2 設定檔的解析

  從上面的代碼可以看到,設定檔的解析是通過XMLConfigBuilder實現的。

public class XMLConfigBuilder extends BaseBuilder {     private boolean parsed;    private final XPathParser parser;    private String environment;    private final ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();    private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {        super(new Configuration());        ErrorContext.instance().resource("SQL Mapper Configuration");        this.configuration.setVariables(props);        this.parsed = false;        this.environment = environment;        this.parser = parser;    }     public Configuration parse() {        if (parsed) {            throw new BuilderException("Each XMLConfigBuilder can only be used once.");        }        parsed = true;        parseConfiguration(parser.evalNode("/configuration"));        return configuration;    }     private void parseConfiguration(XNode root) {        try {            propertiesElement(root.evalNode("properties"));  // 解析屬性            Properties settings = settingsAsProperties(root.evalNode("settings"));   // 解析設定項            loadCustomVfs(settings);            typeAliasesElement(root.evalNode("typeAliases"));   // 解析別名            pluginElement(root.evalNode("plugins"));      // 解析外掛程式            objectFactoryElement(root.evalNode("objectFactory"));    // 解析對象工廠            objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));   // objectWrapper工廠,可以對結果進行一些特殊的處理            reflectorFactoryElement(root.evalNode("reflectorFactory"));  // 反射工廠            settingsElement(settings);            environmentsElement(root.evalNode("environments"));   // 解析環境            databaseIdProviderElement(root.evalNode("databaseIdProvider"));   // 解析資料庫提供廠商            typeHandlerElement(root.evalNode("typeHandlers"));    // 解析配置的類型處理器            mapperElement(root.evalNode("mappers"));    // 解析sql對應檔        } catch (Exception e) {            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);        }    }}

通過mybatis設定檔和mapper檔案的解析,

1.將mapper介面資訊,註冊到了mapperRegistry,  // 以class<T> 和代理工廠註冊到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);  }}

這樣,針對session.getMapper(OrderInfoMapper.class)產生動態代理類,就對應上了

2.Statement資訊註冊到了mappedStatements

public final class MappedStatement {     private String resource;    private Configuration configuration;    private String id;    private Integer fetchSize;    private Integer timeout;    private StatementType statementType;    private ResultSetType resultSetType;    private SqlSource sqlSource;    private Cache cache;    private ParameterMap parameterMap;    private List<ResultMap> resultMaps;    private boolean flushCacheRequired;    private boolean useCache;    private boolean resultOrdered;    private SqlCommandType sqlCommandType;    private KeyGenerator keyGenerator;    private String[] keyProperties;    private String[] keyColumns;    private boolean hasNestedResultMaps;    private String databaseId;    private Log statementLog;    private LanguageDriver lang;    private String[] resultSets;      public BoundSql getBoundSql(Object parameterObject) {        BoundSql boundSql = sqlSource.getBoundSql(parameterObject);        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();        if (parameterMappings == null || parameterMappings.isEmpty()) {            boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);        }        for (ParameterMapping pm : boundSql.getParameterMappings()) {            String rmId = pm.getResultMapId();            if (rmId != null) {                ResultMap rm = configuration.getResultMap(rmId);                if (rm != null) {                    hasNestedResultMaps |= rm.hasNestedResultMaps();                }            }        }        return boundSql;    }}
3.3 調用映射

session擷取mapper,調用了Configuration中從mapper庫中查詢到MapperProxyFactory對象,調用方法,執行MapperProxy中invoke方法

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

執行mapperMethod的execute方法

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

4.總結

    主要是梳理了下從DAO介面到Mybatis查詢的過程,解釋了Mybatis採用的動態代理模式,將DAO介面的方法映射到session使用statementId查詢的過程,Mybatis源碼中涉及了很多工廠類和建造類,可以借鑒。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.