当前位置: 首页 > 后端技术 > Java

mybatis-plus页面插件失效探索

时间:2023-04-01 14:57:25 Java

缃戜笂鍩虹涓婇兘鏄帹鑽愰厤缃涓嬶細@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){mybatisPlusInterceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));杩斿洖mybatisPlusInterceptor锛涗絾鏄紝浠呬粎閫氳繃杩欐牱鍋氾紝鎴戜滑鑳借揪鍒版垜浠殑鏈熸湜鍚楋紵缁撳眬鏄垎椤垫彃浠舵病鏈夋晥鏋滐紝涓轰粈涔堝憿锛燄煒斿浘1鍜屽浘2閫氳繃瀵规瘮浠ヤ笂涓ゅ浘鍙互鍙戠幇锛屽浘1涓殑DefaultSqlSession.selectList()搴曞眰璋冪敤浜哖lugin.invoke()锛汥efaultSqlSession.selectList()搴曞眰璋冪敤鍥?涓殑CachingExecutor.query()锛屽叾涓浘1鏄垎椤垫彃浠剁敓鏁堟椂鐨勮皟鐢ㄩ摼锛屽浘2鏄垎椤垫彃浠跺け鏁堟椂鐨勮皟鐢ㄩ摼銆備篃灏辨槸璇达紝鍒嗛〉鎻掍欢澶辫触鐨勫師鍥犳槸mybatis-plusPlugin绫绘病鏈変负鍒嗛〉鎻掍欢鎷︽埅鍣ㄧ敓鎴怑xecutor浠g悊銆傛垜搴旇鎬庝箞鍔烇紵濡備笅鍥撅紝鍦ㄦ瀯寤篠qlSessionFactory鏃讹紝闇€瑕佸湪MybatisSqlSessionFactoryBean涓樉绀哄苟璁剧疆Plugin銆侤Bean(name="defaultSqlSessionFactory")publicSqlSessionFactorydefaultSqlSessionFactory(){MybatisSqlSessionFactoryBeanbean=newMybatisSqlSessionFactoryBean();bean.setDataSource(dataSource);//璁剧疆鎷︽埅鍣╞ean.setPlugins(mybatisPlusInterceptor);SqlSessionFactorysqlSessionFactory=bean.getObject();//璁剧疆鑷姩鎻愪氦sqlSessionFactory.openSession(true);returnsqlSessionFactory;}閭d箞锛屼粈涔堟椂鍊欑敓鎴愬垎椤垫彃浠剁殑浠g悊绫诲憿锛熷厛鍙戝竷绛旀锛?/璁剧疆鑷姩鎻愪氦sqlSessionFactory.openSession(true);璋冪敤閾惧涓嬶細鍥?鐪嬬粏鑺傦細DefaultSqlSessionFactory.openSessionFromDataSource()缁嗚妭锛歱rivateSqlSessionopenSessionFromDataSource(ExecutorTypeexecType,TransactionIsolationLevellevel,booleanautoCommit){try{finalEnvironmentenvironment=configuration.getEnvironment();finalTransactionFactorytransactionFactory=getTransactionFactoryFromEnvironment(鐜);tx=transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);//杩欎竴姝ュ緢鍏抽敭锛屽垱寤烘墽琛岃€呭疄渚媐inalExecutorexecutor=configuration.newExecutor(tx,execType);杩斿洖鏂扮殑DefaultSqlSession锛堥厤缃€佹墽琛屽櫒銆佽嚜鍔ㄦ彁浜わ級锛泒catch(Exceptione){closeTransaction(tx);//鍙兘宸茬粡鑾峰彇浜嗕竴涓繛鎺ワ紝鎵€浠ヨ鎴戜滑璋冪敤close()throwExceptionFactory.wrapException("Erroropeningsession.Cause:"+e,e);}鏈€鍚巤ErrorContext.instance().reset();}}Configuration.newExecutor()璇︽儏锛歱ublicExecutornewExecutor(Transactiontransaction,ExecutorTypeexecutorType){executorType=executorType==null?榛樿鎵ц鍣ㄧ被鍨嬶細鎵ц鍣ㄧ被鍨嬶紱鎵ц鍣ㄧ被鍨?鎵ц鍣ㄧ被鍨?=绌猴紵鎵ц鍣ㄧ被鍨?SIMPLE锛氭墽琛屽櫒绫诲瀷锛涙墽琛岃€呮墽琛岃€咃紱if(ExecutorType.BATCH==executorType){executor=newBatchExecutor(this,transaction);}elseif(ExecutorType.REUSE==executorType){executor=newReuseExecutor(this,trans琛屽姩锛?}else{executor=newSimpleExecutor(this,transaction);}if(cacheEnabled){executor=newCachingExecutor(executor);}//浠g悊鍒颁笂闈㈢殑executor锛堢洰鐨勬槸鎶婃彃浠跺拰executor灏佽鎴愪竴涓唬鐞嗙被锛塭xecutor=(Executor)interceptorChain.pluginAll(executor);鍥炲綊鎵ц浜猴紱}MybatisPlusInterceptor.pluginAll();publicObjectpluginAll(Objecttarget){for(Interceptorinterceptor:interceptors){target=interceptor.plugin(target);杩斿洖鐩爣锛泒閫氳繃涓婇潰鐨勫叧閿唬鐮佸睍绀猴紝鎴戜滑澶ф浜嗚В浜嗗垎椤垫彃浠朵唬鐞嗙被鍦ㄤ竴浜涢噸瑕佽妭鐐逛腑浜х敓鐨勯€昏緫銆傛帴涓嬫潵锛屾垜浠户缁簡瑙e垎椤垫彃浠跺叿浣撳伐浣滅殑鏁堟灉銆傚浘4publicbooleanwillDoQuery(){if(countMs!=null){countSql=countMs.getBoundSql(parameter);}else{countMs=buildAutoCountMappedStatement(ms);//鏋勫缓鏌ヨ璁℃暟SQLStringcountSqlStr=autoCountSql(page,boundSql.getSql());PluginUtils.MPBoundSqlmpBoundSql=PluginUtils.mpBoundSql(boundSql);//鏋勫缓BoundSqlcountSql=newBoundSql(countMs.getConfiguration(),countSqlStr,mpBoundSql.parameterMappings(),parameter);PluginoundUtils.setSmpcountParam.additionalParameters());}//鏌ヨ璁℃暟鍊糒istresult=executor.query(countMs,parameter,rowBounds,resultHandler,cacheKey,countSql);}鎺ヤ笅鏉aginationInnerInterceptor.beforeQuery()鐢熸垚鍒嗛〉sql锛涙渶鍚嶮ybatisPlusInterceptor.intercept()涓殑executor.query()鎵ц鍒嗛〉sql銆?/p>
最新推荐
猜你喜欢