使用Mybatis自定義插件(統一去除時間字段後面的.0)

今天,日月教大家如何使用mybatis自定義插件實現統一去除從數據庫查詢出的時間字段後面遺留的.0。
話不多說,直接上代碼。

定義插件

import com.chenqi.util.CheckUtils;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @Author chenqi
 * @Description mybatis 自定義插件(統一去除時間後面的.0)
 * @Date 17:48 2019/5/25
 **/
@Intercepts({@Signature(
        type= ResultSetHandler.class,
        method = "handleResultSets",
        args = {Statement.class})})
public class DateTimeInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();
        if (result instanceof ArrayList<?>) {
            List<?> list = (ArrayList<?>)result;
            for(Object obj: list){
                if(obj == null || obj instanceof Integer
                        || obj instanceof String
                        || obj instanceof Long
                        || obj instanceof Double
                        || obj instanceof Date
                        || obj instanceof BigDecimal){
                    continue;
                }

                for (Field field : obj.getClass().getDeclaredFields()) {
                    if(field.getName().indexOf("Time") != -1
                            && "class java.lang.String".equals(field.getType().toString())){
                        Object value = getFieldValueByName(field.getName(),obj);
                        String time = CheckUtils.objCheckNull(value) ? "" : value.toString();
                        if(!CheckUtils.strCheckNull(time)){
                            field.setAccessible(true);
                            field.set(obj, time.split("\\.")[0]);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 根據屬性名獲取屬性值
     * */
    private Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

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

    @Override
    public void setProperties(Properties properties) {
    }

}

使用示例

只需要將該類使用@Bean註解注入到spring容器即可

/**
 * 時間字段處理插件
 *
 * */
@Bean
public DateTimeInterceptor dateTimeInterceptor() {
    return new DateTimeInterceptor();
}

實現思路

1、首先通過實現mybatis的Interceptor接口,獲取到所有查詢sql的返回結果。
2、通過instanceof加循環篩選得到需要處理的時間屬性。(注意時間屬性需包含Time字符串)
3、通過java反射機制獲得屬性名稱、屬性類型、屬性值,並使用Field 類對屬性值進行修改並重新賦值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章