A. mybatis杩炴帴鏁版嵁搴揿疄鐜板炲垹鏀规煡镄勬ラわ纴澶ц嚧鏄镐庢牱锛熸垜灏辨槸缂哄皯姒傚康镐х殑寮曞硷纴镓浠ュ崱浣忎简銆傚ぇ绁炲紩瀵兼垜
Mybatis 镎崭綔鏁版嵁搴揿拰Hiberante 閮藉樊涓嶅氥傛ゅ勪粙缁岖敤springMVC鍜孧ybatis
鍏埚湪applicationContext.xml閲岄溃閰岖疆Mybatis镄勪俊鎭
寤哄ソ鍦ㄤ笂闱㈠疄浣撶殑璺寰勪笅闱㈠缓濂藉疄浣掳纴XML镄勮矾寰勪笅闱㈠缓濂絏ML鏂囦欢
铹跺悗鍦╳eb.xml涓嬮溃寮曞叆applicationContext.xml鍗冲彲
B. MyBatis-Plus 浣跨敤杩欎箞鏂逛究锛屽簳灞傛槸濡备綍澶勭悊镄勫憿锛
MyBatis-plus鏄瀹屽叏锘轰簬MyBatis寮鍙戠殑涓涓澧炲己宸ュ叿锛屾槸鍦∕yBatis镄勫熀纭涓婂仛澧炲己镄勬嗘灦锛屼负绠鍖栧紑鍙戙佹彁楂樻晥鐜囱岀敓銆
瀹冨湪MyBatis铡熸湰镄勬嗘灦涓婂炲姞浜嗗緢澶氩疄鐢ㄦу姛鑳斤纴姣斿备箰瑙傞挛鎻掍欢銆佸瓧娈佃嚜锷ㄥ~鍏呭姛鑳姐佸垎椤垫彃浠躲佹浔浠舵瀯阃犲櫒銆乻ql 娉ㄥ叆鍣ㄧ瓑绛夈备娇鐢 MyBatis-plus 鍙浠ュ畬鍏ㄤ笉鍐欎换浣 XML 鏂囦欢锛岀洿鎺ヤ娇鐢ㄧ户镓夸简BaseMapper 鎺ュ彛镄勭被瀵硅薄瀹屾垚瀵规暟鎹搴撶殑鏄犲皠镎崭綔
锘轰簬鏄犲皠镄勫师鐞嗭纴MyBatis-plus 蹇呯劧瑕佸疄鐜 Mapper涓镄勬柟娉曚笌 SQL 璇鍙ョ殑瀵瑰簲杞鍖栵纴浠ヤ笅鍗充负 MyBatis-plus 閲嶈佹祦绋嫔浘渚
1.鍦 MyBatis-plus 涓锛 MybatisPlusAutoConfiguration 镊锷ㄩ厤缃绫荤殑 sqlSessionFactory() 鏂规硶涓 Spring鎻愪緵鍒涘缓 sqlSession 镄勫伐铡傜被瀵硅薄锛屽 sqlSessionFactory 杩涜屽畾涔夌殑瀹氢箟绫诲彉涓轰简 MybatisSqlSessionFactoryBean 銆
鍦 sqlSessionFactory() 鏂规硶涓锛岄櫎浜嗘敞鍏 MyBatis链韬镄勭粍浠讹纴杩树细娉ㄥ叆MyBatis-plus 镄 涓婚敭鐢熸垚鍣ㄣ丼QL 娉ㄥ叆鍣ㄧ瓑缁勪欢锛屾渶钖庨氲繃 MybatisSqlSessionFactoryBean#getObject() 鏂规硶銮峰彇鍒 sqlSessionFactory 瀵硅薄
2. MybatisSqlSessionFactoryBean#getObject() 镓ц屾噿锷犺浇绛栫暐锛屾渶钖庨氲繃 buildSqlSessionFactory() 鏂规硶鍒涘缓 SqlSessionFactory 宸ュ巶绫诲硅薄銆傝繖涓鏂规硶镄勬祦绋嫔緢闀匡纴涓嶈繃澶ц嚧鍙浠ュ垎涓轰袱涓姝ラわ细
3. MybatisXMLConfigBuilder#parse() 浼氩幓瑙f瀽閰岖疆鏂囦欢锛屾渶钖庝细璋幂敤鍒板叾鍐呴儴鏂规硶 mapperElement() 銆傝繖涓鏂规硶瀹屾垚瑙f瀽 Mapper宸ヤ綔锛屽苟灏嗗叾娣诲姞鍒伴厤缃绫 MybatisConfiguration 涓
4. MybatisConfiguration#addMapper() 鏂规硶鍏跺疄鏄铡昏皟鐢 MybatisMapperRegistry#addMapper() 鏂规硶锛屽叾镙稿绩鏄 #parse()
5. #parse() 鏂规硶鐪熸e紑濮嫔畬鎴 Mapper 鎺ュ彛涓镄勬柟娉曚笌 SQL 璇鍙ョ殑鏄犲皠锛屽叾涓 parseStatement() 鏂规硶鏄瑙f瀽 @Select/@Update 绛夋敞瑙e啓鍏ョ殑 SQL璇鍙ワ纴钥屼唬镰 GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type ) 阃氲繃 MaBatis-plus镄 SQL 娉ㄥ叆鍣ㄥ畬鎴 Mapper 鏂规硶涓 SQL 璇鍙ョ殑杞鍖
6. AbstractSqlInjector#inspectInject() 浼氩畬鎴 BaseMapper 鎺ュ彛涓鎻愪緵镄勯氱敤鏂规硶瀵瑰簲镄 SQL 璇鍙ュ嗳澶囷纴杩欓儴鍒嗕富瑕侀氲繃 AbstractMethod#inject() 鏂规硶瀹屾垚
7. AbstractMethod#inject() 鏂规硶骞舵病链変粈涔堢壒鍒镄勬搷浣滐纴鍙鏄璋幂敤鍏跺瓙绫诲疄鐜 injectMappedStatement() 鏂规硶銆备互 SelectOne#injectMappedStatement() 涓轰緥锛屽叾 SQL 璇鍙ョ殑镙稿绩鍦ㄤ簬 SqlMethod 绫伙纴杩欎釜鏋氢妇绫讳腑缂揿瓨浜嗗彲浠ュ姩镐佹嫾鎺ョ殑 SQL 璇鍙ヨ剼链锛屽彧闇瑕佸~涓婂弬鏁 format 灏卞彲浠ュ缑鍒 SQL 璇鍙ョ殑镓ц岃剼链銆
浠ヤ笂杩囩▼缁撴潫锛屽彧闇瑕佸皢镓链変俊鎭阃氲繃 addInsertMappedStatement() 鏂规硶灏佽呮垚 MappedStatement 瀵硅薄骞跺皢鍏跺姞鍏ュ埌瀹瑰櫒涓锛岃繖镙 Mapper鎺ュ彛鏂规硶璋幂敤镞讹纴灏卞彲浠ラ氲繃 锷ㄦ佷唬鐞 镄勬柟寮忔垒鍒板叾瀵瑰簲镓ц岀殑 SQL 鑴氭湰锛岃呖姝 SQL 璇鍙ュ嗳澶囧强閰岖疆瑙f瀽灏卞畬鎴愪简銆
链钖庢嫾鎺ョ殑 SQL 璇鍙 鑴氭湰褰㈠纺濡备笅绀轰緥锛屽疄闄呮墽琛屾暟鎹搴撴搷浣沧椂浼氲В鏋愯繖涓鑴氭湰瀹屾垚鍙橀噺镟挎崲锛屼粠钥屽缑鍒板彲镓ц岀殑 SQL 璇鍙
8. SqlSessionFactory 瀵硅薄镄勫垱寤洪渶瑕佸洖鍒 MybatisSqlSessionFactoryBean#buildSqlSessionFactory() 鏂规硶涓锛屽緢瀹规槗杩借釜鍒 #build() 鏂规硶锛屾渶钖庡叾瀹炴槸阃氲繃 SqlSessionFactoryBuilder#build() 鏂规硶鍒涘缓浜嗕竴涓 DefaultSqlSessionFactory 瀵硅薄杩斿洖
1. @MapperScan 娉ㄨВ阃氲繃 @Import(MapperScannerRegistrar.class) 寮曞叆镓鎻忔敞鍐岀殑绫 MapperScannerRegistrar 锛岃ョ被瀹炵幇浜 ImportBeanDefinitionRegistrar 鎺ュ彛骞堕吨鍐 registerBeanDefinitions() 鏂规硶锛屽湪璇ユ柟娉曚腑娉ㄥ唽浜 MapperScannerConfigurer 绫
2. MapperScannerConfigurer 鏄 Mapper鎺ュ彛镄勬壂鎻忛厤缃绫伙纴瀹炵幇浜 鎺ュ彛锛屽叾 () 鏂规硶浼氩湪瀹瑰櫒钖锷ㄨ繃绋嬩腑琚锲炶皟锛岄氲繃 ClassPathMapperScanner#scan() 鏂规硶瀹屾垚 Mapper 镄勬壂鎻忔敞鍐
3. ClassPathMapperScanner#processBeanDefinitions() 灏嗘壂鎻忓埌镄 Mapper鎺ュ彛鐢熸垚镄勫瑰簲 BeanDefinition 镄 beanClass 灞炴ф浛鎹涓 MapperFactoryBean 锛岃繖镙锋疮娆¤幏鍙 Mapper 瀹炰緥瀹为檯鏄阃氲繃 MapperFactoryBean 镄勫疄渚嫔幓銮峰彇
姝ゅ勪綋鐜颁简 FactoryBean 镄勫畾浣嶏纴鍗崇敤浜庤幏鍙栧悓涓绫 bean 镄勫伐铡 bean銆
4. @Autowired 镊锷ㄦ敞鍏 Mapper 瑙﹀彂瀹瑰櫒銮峰彇 bean 镄勬柟娉曪纴璋幂敤鍒 MapperFactoryBean#getObject() 鏂规硶锛屾渶缁堣皟鐢ㄥ埌 sqlSessionTemplate#getMapper() 鏂规硶
5.MyBatis-plus 浣跨敤镄勯厤缃绫绘槸 MybatisConfiguration 锛屾渶缁堣皟鐢ㄥ埌 MybatisMapperRegistry#getMapper() 鏂规硶锛岃繖閲屽氨杩涘叆浜嗗姩镐佷唬鐞呜幏鍙 MapperProxy 瀹炰緥镄勬祦绋
6. MybatisMapperProxyFactory#newInstance() 鏂规硶缁栾嚜锷ㄦ敞鍏ヨ繑锲炰竴涓 MybatisMapperProxy 浠g悊瀵硅薄
7.璋幂敤 Mapper 鎺ュ彛镄勬柟娉曡Е鍙戜唬鐞嗗硅薄镄 MybatisMapperProxy#invoke() 锛屾ゆ椂镙规嵁 Mapper 瀵硅薄琚璋幂敤镄勬柟娉旷敓鎴 MybatisMapperMethod 瀵硅薄锛岄氲繃 MybatisMapperMethod#execute() 铡荤湡姝e湴镓ц SQL 璇鍙ワ纴浠庤屽畬鎴愭暟鎹搴撴搷浣溿