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 璇鍙ワ紝浠庤屽畬鎴愭暟鎹搴撴搷浣溿