mybatis 自动填充默认值

阅读更多





	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	

	
		
		
			
			
			
			
			
			
			
			
		
		
			
		
		
			
			
		
	
	




//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Intercepts({@Signature(
    type = Executor.class,
    method = "update",
    args = {MappedStatement.class, Object.class}
)})
public class AutoDateInterceptor implements Interceptor {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private static final String METHOD_PREPARE = "prepare";
    private static final String METHOD_SETPARAMETERS = "setParameters";
    private static final String METHOD_UPDATE = "update";
    private static String createDateColumnName;
    private static String updateDateColumnName;
    private static DbType dbType;
    public static String createDateFieldName;
    public static String updateDateFieldName;

    public AutoDateInterceptor() {
    }

    public Object intercept(Invocation invocation) throws Throwable {
        if("update".equals(invocation.getMethod().getName())) {
            MappedStatement statement = (MappedStatement)invocation.getArgs()[0];
            Object parameter = invocation.getArgs()[1];
            SqlCommandType sqlCommandType = statement.getSqlCommandType();
            Date d = new Date();
            if(parameter instanceof Map) {
                Iterator var6 = ((Map)parameter).values().iterator();

                while(var6.hasNext()) {
                    Object o = var6.next();
                    if(SqlCommandType.INSERT == sqlCommandType) {
                        this.create(o, d);
                    } else if(SqlCommandType.UPDATE == sqlCommandType) {
                        this.update(o, d);
                    }
                }
            } else if(SqlCommandType.INSERT == sqlCommandType) {
                this.create(parameter, d);
            } else if(SqlCommandType.UPDATE == sqlCommandType) {
                this.update(parameter, d);
            }
        }

        return invocation.proceed();
    }

    public void create(Object o, Date d) {
        try {
            Field f1 = ReflectionUtils.getDeclaredField(o, createDateFieldName);
            if(f1 != null) {
                ReflectionUtils.setFieldValue(o, f1, d);
            }

            Field f2 = ReflectionUtils.getDeclaredField(o, updateDateFieldName);
            if(f2 != null) {
                ReflectionUtils.setFieldValue(o, f2, d);
            }
        } catch (Exception var5) {
            this.log.error(var5.getMessage(), var5);
        }

    }

    public void update(Object o, Date d) {
        try {
            Field f = ReflectionUtils.getDeclaredField(o, updateDateFieldName);
            if(f != null) {
                ReflectionUtils.setFieldValue(o, f, d);
            }
        } catch (Exception var4) {
            this.log.error(var4.getMessage(), var4);
        }

    }

    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }

    public void setProperties(Properties properties) {
        createDateColumnName = properties.getProperty("createDateColumnName", "gmt_create");
        updateDateColumnName = properties.getProperty("updateDateColumnName", "gmt_modified");
        dbType = DbType.get(properties.getProperty("dbType", "oracle"));
        createDateFieldName = StringUtil.underlineToHump(createDateColumnName);
        updateDateFieldName = StringUtil.underlineToHump(updateDateColumnName);
    }
}

 

你可能感兴趣的:(mybatis 自动填充默认值)