มีผู้เริ่มต้น Java จำนวนมากที่ไม่รู้จัก Mybatis Interceptor Inteceptor มากนัก ที่นี่ฉันจะจัดระเบียบบทความต่อไปเพื่อให้คุณอธิบาย Mybatis Interceptor Inteceptor ใน Java
บทความนี้ส่วนใหญ่วิเคราะห์กลไกปลั๊กอินของ mybatis ซึ่งจริง ๆ แล้วเป็นแบบจำลองความรับผิดชอบห่วงโซ่การใช้งานของพร็อกซีแบบไดนามิก Java
ตามเอกสารอย่างเป็นทางการ MyBatis อนุญาตให้สกัดกั้นวิธีการต่อไปนี้เท่านั้นซึ่งกำหนดพารามิเตอร์ลายเซ็นของคำอธิบายประกอบการดักจับ
รหัสมีดังนี้
Executor (อัปเดต, Query, Flushstatements, commit, การย้อนกลับ, getTransaction, close, isClose) พารามิเตอร์ (getParameterObject, setParameters) ResultsEthandler
ซอร์สโค้ดของการประมวลผล interceptor มีดังนี้โดยที่ interceptorchain.pluginall (.. ) เป็นการทอผ้าสกัดที่กำหนดเอง:
รหัสมีดังนี้
/ * เมธอดใน org.apache.ibatis.session.configuration คลาส */parameterhandler newparameterhandler (mappedStatement mappedStatement, พารามิเตอร์วัตถุวัตถุ, boundsql boundsql) {parameterHandlerameterhandlament) /* intercept parameterHandler*/ parameterHandler = (parameterHandler) interceptorChain.pluginall (ParameterHandler); Return ParameterHandler;} ผลลัพธ์สาธารณะ ethandler newresultsethandler (ผู้ดำเนินการบริหาร, การแม็พสเตเมชั่นการแม็พสเตท, Rowbounds Rowbounds, parameterhandler parameterhandler, resulthandler resulthandler, boundsql boundsql) {ผลลัพธ์ ParameterHandler, Resulthandler, BoundSQL, Rowbounds); /* Intercept ResultsEthandler*/ ResultsEthandler = (ResultsEthandler) InterceptorChain.pluginall (ResultsEthandler); Returne ResultsEthandler;} Public Pattracthandler NewStatementHandler (Executor Executor, MappedStatement MappedStatement, Object ParameterObject, Rowbounds Rowbounds, Resulthandler Resulthandler, Boundsql Boundsql) resulthandler, boundsql); /* intercept atertmatehandler*/ attementhandler = (attementhandler) interceptorchain.pluginall (attementhandler); return statementhandler;} ผู้ดำเนินการสาธารณะ newExecutor (ธุรกรรมธุรกรรม, executortype executortype) {executortype = executortype == null? DefaultExecutortype: Executortype; executortype = executortype == null? Executortype.simple: Executortype; ผู้บริหารผู้บริหาร; if (executortype.batch == executortype) {executor = new batchexecutor (สิ่งนี้, ธุรกรรม); } อื่นถ้า (executortype.reuse == executortype) {executor = ใหม่ reuseExecutor (สิ่งนี้, ธุรกรรม); } else {executor = new SimpleExecutor (สิ่งนี้, ธุรกรรม); } if (cacheenabled) {executor = new cachingExecutor (Executor); } /* Intercept Executor* / executor = (Executor) InterceptorChain.pluginall (Executor); return executor;} ในการใช้งาน Interceptor ที่กำหนดเองคุณจะต้องใช้อินเตอร์เฟส Interceptor เท่านั้น รหัสทั่วไปมีดังนี้:
รหัสมีดังนี้
/* เมธอดและพารามิเตอร์ของคำอธิบายประกอบที่ระบุว่าอินเทอร์เฟซใดในการสกัดกั้น*/ @intercepts ({@signature (type = stementhandler.class, method = "เตรียม", args = {connectionsclass})}) คลาสสาธารณะของคุณ / * หมายเหตุ: ที่นี่วิธีการเรียกใช้งาน () จริง ๆ แล้วใช้เพื่อดำเนินการโทรแบบสำรวจของห่วงโซ่ interceptorchain (นั่นคือเพื่อดำเนินการวิธีการสกัดกั้นที่ลงทะเบียนทั้งหมดของ interceptor) และการเรียกใช้วิธีดั้งเดิมของวัตถุพร็อกซีสุดท้าย } / * สร้างพร็อกซีไปยังเป้าหมายเป้าหมายและคำอธิบายประกอบของ @Intercepts ใช้ในปลั๊กอินปลั๊กอินวัตถุสาธารณะ (เป้าหมายวัตถุ) { / * เมื่อคลาสเป้าหมายเป็นประเภทคำสั่ง } /* ใช้ในการตั้งค่าพารามิเตอร์การกำหนดค่า custom interceptor* / @Override โมฆะสาธารณะ setProperties (คุณสมบัติคุณสมบัติ) {}} ในหมู่พวกเขารหัสสำหรับการสกัดกั้นการโทรอยู่ใน plugin.wrap:
รหัสมีดังนี้
/* org.apache.ibatis.plugin.plugin คลาส*/ปลั๊กอินคลาสสาธารณะใช้การเรียกใช้งาน {/* ละเว้นรหัส ...*/การห่อวัตถุคงที่สาธารณะ (เป้าหมายวัตถุ, interceptor interceptor) {/* นี่คือการลงนามคำอธิบายประกอบ คลาส <?> type = target.getClass (); /* รับอินเทอร์เฟซที่ตรงกับคลาสเป้าหมาย*/ คลาส <?> [] อินเตอร์เฟส = getAllInterfaces (ประเภท, signatureMap); if (interfaces.length> 0) { /* ใช้ JDK Dynamic Proxy* / return proxy.newproxyinstance (type.getClassLoader (), อินเตอร์เฟส, ปลั๊กอินใหม่ (เป้าหมาย, interceptor, signatureMap)); } คืนเป้าหมาย; } /* การดำเนินการของวิธีการทั้งหมดของคลาสเป้าหมายการสกัดกั้นจะถูกดำเนินการที่นี่* / @Override วัตถุสาธารณะเรียกใช้ (พร็อกซีวัตถุวิธีการวิธีการวัตถุ [] args) พ่น throwable {ลอง {set <method> methods = signatureMap.get (method.getDeclaringClass (); if (methods! = null && methods.contains (เมธอด)) { /* ดำเนินการเมธอด interceptor* / return interceptor.intercept (การเรียกใช้ใหม่ (เป้าหมาย, วิธี, args)); } return method.invoke (เป้าหมาย, args); } catch (exception e) {โยน expsexutil.unwrapthrowable (e); }} / * ละเว้นรหัส ... * /}คุณจะเห็นได้ว่ารหัสหลักของการออกแบบ interceptor mybatis นั้นค่อนข้างง่าย แต่มีความยืดหยุ่นเพียงพอ เมื่อใช้ในทางปฏิบัติระวังอย่าให้พร็อกซีไร้ความหมาย (plugin.wrap)