In general, the database table structure will have update_time, modify time, because this field is basically not very relevant to the business, so often forget to set the value of these two fields during the development process, this plug-in is to solve this problem. Create id, create_time and so on in the same way. You can also write your own paging plug-in in this way. Cut the code.

1. Write a custom annotation called update_time

package com.zb.iscrm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @auther: Yang Hongxing * @date: 2018/11/28 09:38 * @description: */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) public @interface UpdateTime { String value() default"";
}

Copy the code

2. Write a Mybatis plugin

This is a Mybatis plugin. The @intercepts plugin specifies the operations to be intercepted

package com.zb.iscrm.mybatisInterceptor;

import com.zb.iscrm.annotation.UpdateTime;
import com.zb.iscrm.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;

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

/ * * *@Auther: Hongxing Yang *@Date: 2018/11/28 09:41
 * @DescriptionThe mybatis plugin is used to add the current time to */ when performing Update
@Slf4j
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class UpdateTimeInterceptor  implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        // Get the SQL command
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        // Get parameters
        Object parameter = invocation.getArgs()[1];
        if(parameter ! =null) {
            // Get a member variable
            Field[] declaredFields = parameter.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                if(field.getAnnotation(UpdateTime.class) ! =null) { // The update statement inserts updateTime
                    if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
                        field.setAccessible(true);
                        if (field.get(parameter) == null) {
                            field.set(parameter, DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS));
                        }
                    }
                }
            }
        }
        // You can add create_time or id generation in the same way
        
        return invocation.proceed();
    }

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

    @Override
    public void setProperties(Properties properties) {}}Copy the code

Finally, register the plugin in the MyBatis configuration file and you’re done

<? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE configuration PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <! - registered plug-in - > < plugins > < plugin interceptor = "com. Zb. Iscrm. MybatisInterceptor. UpdateTimeInterceptor" / > < / plugins > </configuration>Copy the code