MyBatis中binding 模塊的作用是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目網(wǎng)站建設、成都網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元黎川做網(wǎng)站,已為上家服務,為黎川各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
org.apache.ibatis.binding
封裝ibatis編程模型 ibatis編程模型中,SqlSession作為sql執(zhí)行的入口,實用方法為sqlSession.selectOne(namespace+id, 參數(shù)列表)的形式(例:sqlSession.selectOne("com.enjoylearning.mybatis.mapper.TUserMapper.selectByPrimaryKey", 2)),不易于維護和閱讀
找到SqlSession中對應的方法(insert|update|select)
找到命名空間和方法名(兩維坐標)
傳遞參數(shù)
MapperRegistry:mapper接口和對應的代理工廠的注冊中心;是Configuration的成員變量
MapperProxyFactory:用于生成mapper接口動態(tài)代理的實例對象;保證mapper實例對象是局部變量(為什么不緩存?見文章最后)
MapperMethod:封裝了Mapper接口中對應的方法信息,以繼對應sql語句的信息.MapperMethod對象不記錄任何狀態(tài),所以它可以再多個代理對象之間共享;sqlCommand:封裝sql語句;MtehodSignature:封裝mapper接口的入?yún)⒑头祷仡愋?/p>
個人理解
MapperRegistry的Map
, MapperProxyFactory>> knownMappers = new HashMap<>(); key:mapper的Class對象 value:生產(chǎn)這個mapper的動態(tài)代理示例的工廠 MapperProxyFactory:Map
methodCache key:方法的反射類 value 方法的信息(sql語句,入?yún)?返回值)
public class MapperRegistry { private final Configuration config;//config對象,mybatis全局唯一的 //記錄了mapper接口與對應MapperProxyFactory之間的關(guān)系 private final Map, MapperProxyFactory>> knownMappers = new HashMap<>(); public MapperRegistry(Configuration config) { this.config = config; } @SuppressWarnings("unchecked") public T getMapper(Class type, SqlSession sqlSession) { final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory ) 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); }
/** * * 用于生成mapper接口動態(tài)代理的實例對象; * @author Lasse Voss */ public class MapperProxyFactory{ //mapper接口的class對象 private final Class mapperInterface; //key是mapper接口中的某個方法的method對象,value是對應的MapperMethod,MapperMethod對象不記錄任何狀態(tài)信息,所以它可以在多個代理對象之間共享 private final Map methodCache = new ConcurrentHashMap<>();
public class MapperMethod { //從configuration中獲取方法的命名空間.方法名以及SQL語句的類型 private final SqlCommand command; //封裝mapper接口方法的相關(guān)信息(入?yún)?,返回類型)? private final MethodSignature method;
public class MapperProxyFactory{ //mapper接口的class對象 private final Class mapperInterface; //key是mapper接口中的某個方法的method對象,value是對應的MapperMethod,MapperMethod對象不記錄任何狀態(tài)信息,所以它可以在多個代理對象之間共享 private final Map methodCache = new ConcurrentHashMap<>(); public MapperProxyFactory(Class mapperInterface) { this.mapperInterface = mapperInterface; } public Class getMapperInterface() { return mapperInterface; } public Map getMethodCache() { return methodCache; } @SuppressWarnings("unchecked") protected T newInstance(MapperProxy mapperProxy) { //創(chuàng)建實現(xiàn)了mapper接口的動態(tài)代理對象 return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); } public T newInstance(SqlSession sqlSession) { //每次調(diào)用都會創(chuàng)建新的MapperProxy對象 final MapperProxy mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); } }
兩個newInstance方法生成mapper的動態(tài)代理對象MapperProxy
public class MapperProxyimplements InvocationHandler, Serializable { ....... @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (Object.class.equals(method.getDeclaringClass())) {//如果是Object本身的方法不增強 return method.invoke(this, args); } else if (isDefaultMethod(method)) { return invokeDefaultMethod(proxy, method, args); } } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } //從緩存中獲取mapperMethod對象,如果緩存中沒有,則創(chuàng)建一個,并添加到緩存中 final MapperMethod mapperMethod = cachedMapperMethod(method); //調(diào)用execute方法執(zhí)行sql return mapperMethod.execute(sqlSession, args); } private MapperMethod cachedMapperMethod(Method method) { return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration())); }
從invoke方法可以看出 是mapperMethod調(diào)用了execute方法
public class MapperMethod { ...... public Object execute(SqlSession sqlSession, Object[] args) { Object result; //根據(jù)sql語句類型以及接口返回的參數(shù)選擇調(diào)用不同的 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()) {//返回值為void executeWithResultHandler(sqlSession, args); result = null; } else if (method.returnsMany()) {//返回值為集合或者數(shù)組 result = executeForMany(sqlSession, args); } else if (method.returnsMap()) {//返回值為map result = executeForMap(sqlSession, args); } else if (method.returnsCursor()) {//返回值為游標 result = executeForCursor(sqlSession, args); } else {//處理返回為單一對象的情況 //通過參數(shù)解析器解析解析參數(shù) Object param = method.convertArgsToSqlCommandParam(args); result = sqlSession.selectOne(command.getName(), param); if (method.returnsOptional() && (result == null || !method.getReturnType().equals(result.getClass()))) { result = OptionalUtil.ofNullable(result); } } 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; }
重點看 result = sqlSession.selectOne(command.getName(), param); 這里使用了ibatis的用法
mapperProxy做查詢依賴的還是sqlSession.按照現(xiàn)在的代碼結(jié)構(gòu)來說,如果緩存了mapperProxy,當sqlSession失效,mapperProxy也就失效了. 如果修改代結(jié)構(gòu),那么sqlSession就要作為查詢參數(shù)
// 原本 TUser user = mapper.selectByPrimaryKey(2); // 修改后的寫法 TUser user = mapper.selectByPrimaryKey(sqslSession,2);
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。