MyBatis拦截器对update,insert拦截.

1.自定义拦截器类实现 org.apache.ibatis.plugin.Interceptor接口.

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;

@Intercepts(@Signature(type = Executor.class, method = "update", args = {MappedStatement.class,
        Object.class}))
public class MybatisIntercepter implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement)invocation.getArgs()[0];
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        Object parameter = invocation.getArgs()[1];
        Class clazz = parameter.getClass();
        String clazzName = clazz.getName();
//        if (clazzName.substring(clazzName.length() - 2,clazzName.length()).equals("Vo")){
        if (!clazz.getSuperclass().isInstance(Object.class)){
            Class superclass = clazz.getSuperclass();
            updateFeild(superclass.getDeclaredFields(),parameter,sqlCommandType);
        }else {
            updateFeild(parameter.getClass().getDeclaredFields(),parameter,sqlCommandType);
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {

    }

    private void updateFeild(Field[] declaredFields,Object parameter,SqlCommandType sqlCommandType) throws IllegalAccessException {
        for (Field field: declaredFields){
            if (SqlCommandType.INSERT.equals(sqlCommandType)){
                if (field.getName().equals("createTime")){
                    field.setAccessible(true);
                    field.set(parameter,new Date());
                }
            }else if (SqlCommandType.UPDATE.equals(sqlCommandType)){
                if (field.getName().equals("updateTime")){
                    field.setAccessible(true);
                    field.set(parameter,new Date());
                }
            }
        }
    }
}
Executor.class 中定义了可以拦截的部分方法.
package org.apache.ibatis.executor;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.transaction.Transaction;

/**
 * @author Clinton Begin
 */
public interface Executor {

  ResultHandler NO_RESULT_HANDLER = null;

  int update(MappedStatement ms, Object parameter) throws SQLException;

   List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

   List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;

   Cursor queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException;

  List flushStatements() throws SQLException;

  void commit(boolean required) throws SQLException;

  void rollback(boolean required) throws SQLException;

  CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);

  boolean isCached(MappedStatement ms, CacheKey key);

  void clearLocalCache();

  void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class targetType);

  Transaction getTransaction();

  void close(boolean forceRollback);

  boolean isClosed();

  void setExecutorWrapper(Executor executor);

}

2.在SpringMvc项目中, 在springmvc-mybatis.xml文件中注入拦截器


	
		
		
		
        
	

	

 

你可能感兴趣的:(后台)