คุณสามารถพิมพ์คำสั่ง SQL ตามเวลาดำเนินการ
การคัดลอกรหัสมีดังนี้:
แพ็คเกจ mybatis;
นำเข้า java.text.dateFormat;
นำเข้า java.util.date;
นำเข้า java.util.list;
นำเข้า java.util.locale;
นำเข้า java.util.properties;
นำเข้า org.apache.ibatis.executor.executor;
นำเข้า org.apache.ibatis.mapping.boundsql;
นำเข้า org.apache.ibatis.mapping.mappedstatement;
นำเข้า org.apache.ibatis.mapping.parametermapping;
นำเข้า org.apache.ibatis.plugin.interceptor;
นำเข้า org.apache.ibatis.plugin.intercepts;
นำเข้า org.apache.ibatis.plugin.invocation;
นำเข้า org.apache.ibatis.plugin.plugin;
นำเข้า org.apache.ibatis.plugin.signature;
นำเข้า org.apache.ibatis.reflection.metaobject;
นำเข้า org.apache.ibatis.session.configuration;
นำเข้า org.apache.ibatis.session.resulthandler;
นำเข้า org.apache.ibatis.session.rowbounds;
นำเข้า org.apache.ibatis.type.typehandlerregistry;
@Intercepts ({
@Signature (type = executor.class, method = "update", args = {mappedStatement.class, object.class})
@Signature (type = executor.class, method = "query", args = {mappedStatement.class, object.class,
rowbounds.class, resulthandler.class})})
คลาสสาธารณะ MyBatisInterceptor ใช้ Interceptor {
คุณสมบัติคุณสมบัติส่วนตัว
การสกัดกั้นวัตถุสาธารณะ (การเรียกร้องการเรียกร้อง) โยนได้ {
MappedStatement MappedStatement = (MappedStatement) Invocation.getArgs () [0];
พารามิเตอร์วัตถุ = null;
if (invocation.getArgs (). ความยาว> 1) {
parameter = rachation.getArgs () [1];
-
String sqlid = mappedStatement.getId ();
BoundSQL BUNTSQL = MappedStatement.getBoundSQL (พารามิเตอร์);
การกำหนดค่าการกำหนดค่า = mappedStatement.getConfiguration ();
Object returnValue = null;
Long Start = System.currentTimeMillis ();
returnValue = Invocation.proceed ();
Long End = System.currentTimeMillis ();
นาน = (สิ้นสุด - เริ่มต้น);
ถ้า (เวลา> 1) {
String SQL = GetSQl (การกำหนดค่า, BoundSQL, SQLID, TIME);
System.err.println (SQL);
-
returnValue;
-
สตริงคงที่สาธารณะ GetSql (การกำหนดค่าการกำหนดค่า, boundSQL BoundSQL, String SQLID, เป็นเวลานาน) {
String SQL = showsQl (การกำหนดค่า, boundSQL);
StringBuilder str = new StringBuilder (100);
str.Append (SQLID);
str.append (":");
Str.Append (SQL);
str.append (":");
str.append (เวลา);
str.append ("MS");
return str.tostring ();
-
สตริงคงที่ส่วนตัว getParameterValue (Object obj) {
ค่าสตริง = null;
if (OBJ Instanceof String) {
value = "'" + obj.toString () + "" ";
} อื่นถ้า (OBJ Instanceof Date) {
DateFormat Formatter = DateFormat.getDateTimeInstance (DateFormat.default, DateFormat.default, locale.china);
value = "'" + formatter.format (วันที่ใหม่ ()) + "'";
} อื่น {
ถ้า (obj! = null) {
ค่า = obj.toString ();
} อื่น {
ค่า = "";
-
-
ค่าส่งคืน;
-
สตริงคงที่สาธารณะ showsQl (การกำหนดค่าการกำหนดค่า, boundSQL boundSQL) {
พารามิเตอร์ Object = BoundSql.GetParameterObject ();
รายการ <ParameterMapping> ParameterMappings = boundSQL.GetParameterMappings ();
String sql = boundsql.getsql (). replaceall ("[// s]+", "");
if (parameterMappings.size ()> 0 && parameterObject! = null) {
typeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry ();
if (typeHandlerRegistry.hastypehandler (parameterObject.getClass ())) {
sql = sql.replacefirst ("//?", getParameterValue (parameterObject));
} อื่น {
metaobject metaobject = configuration.newMetaObject (parameterObject);
สำหรับ (parametermapping parametermapping: parametermappings) {
String propertyName = parameterMapping.getProperty ();
if (metaobject.hasgetter (PropertyName)) {
Object obj = metaobject.getValue (PropertyName);
sql = sql.replacefirst ("//?", getParametervalue (obj));
} อื่นถ้า (boundsql.hasadditionalparameter (PropertyName)) {
Object OBJ = BoundSQL.GetAdDitionAlParameter (PropertyName);
sql = sql.replacefirst ("//?", getParametervalue (obj));
-
-
-
-
ส่งคืน SQL;
-
ปลั๊กอินวัตถุสาธารณะ (เป้าหมายวัตถุ) {
ส่งคืน plugin.wrap (เป้าหมายสิ่งนี้);
-
โมฆะสาธารณะ setProperties (คุณสมบัติคุณสมบัติ 0) {
this.properties = properties0;
-
-