mybatisJava对象、list和json转换

1. 参考mybatis-plus

mybatis Java对象、list和json转换 网上好多不靠谱,参考mybatis-plus中@TableField,mybatis中自定义实现
这样不需要对象中属性字符串接收,保存到表中,都是转义字符,使用时还要手动转换为对象或者List
使用时直接添加注解,比较方便
	//对象,也可以JSONObject
    @TableOperateField(typeHandler = JacksonTypeHandler.class)
    private SysWarnNotesysWarnNote;
	//List,泛型也可以是Object,,这样比较随便了
    @TableOperateField(typeHandler = JacksonListTypeHandler.class)
    private List sysWarnNote;

2. 仿照mybatis-plus自定义注解TableOperateField

package com.yl.cache.annotation;

import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.UnknownTypeHandler;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * TableOperateField注解,用于mybatis insert或者update表时自动给字段赋值
 * 

使用方法 *

 *  @TableOperateField(value=TableOperateField.OperateType.Insert)
 *   private Date createTime;
 *  
* * @author liuxubo * @date 2023/7/23 14:39 */
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) public @interface TableOperateField { /** * 操作字段类型 * * @return */ OperateType value() default OperateType.None; /** * 类型处理器 (该默认值不代表会按照该值生效) * @return */ Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class; /** * 操作sql类型 */ public enum OperateType { None, Insert, Delete, Update, Select, InsertOrUpdate, ; } }

3. 新增、修改日期自动填充

新增、修改时,创建时间和修改时间,手动填充不优雅,使用数据库默认值时间会和系统时间不一致情况 ,这里模仿mybatis-plus实现

package com.yl.cache.interceptor;

import com.yl.cache.annotation.TableOperateField;
import com.yl.cache.annotation.TableOperateField.OperateType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
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.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Properties;

/**
 * 表字段自动填充值拦截类
 *
 * @author liuxubo
 * @date 2023/7/23 14:56
 */
@Slf4j
@Component //使用 mybatis-spring-boot-starter 会自动注入 识别 mybatis的Interceptor 接口实现类
@Intercepts({@Signature(type = org.apache.ibatis.executor.Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class TableOperateInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];

        // 获取 SQL 命令
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
//        log.info("{}", sqlCommandType);

        // 获取参数
        Object parameter = invocation.getArgs()[1];

        // 获取私有成员变量
        Field[] declaredFields = parameter.getClass().getDeclaredFields();
        if (parameter.getClass().getSuperclass() != null) {
            Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields();
            declaredFields = ArrayUtils.addAll(declaredFields, superField);
        }
        for (Field field : declaredFields) {
            TableOperateField tableOperateField = field.getAnnotation(TableOperateField.class);
            if (tableOperateField == null) {
                continue;
            }
            // insert
            if (tableOperateField.value().equals(TableOperateField.OperateType.Insert) || tableOperateField.value().equals(OperateType.InsertOrUpdate)) {
                if (SqlCommandType.INSERT.equals(sqlCommandType)) {
                    setFieldDateValue(parameter, field);

                }
            }
            // update
            if (tableOperateField.value().equals(TableOperateField.OperateType.Update) || tableOperateField.value().equals(TableOperateField.OperateType.InsertOrUpdate)) {
                if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
                    field.setAccessible(true);
                    setFieldDateValue(parameter, field);
                }
            }
        }
        return invocation.proceed();
    }

    /**
     * 设置时间值
     *
     * @param parameter bean对象
     * @param field     field对象
     * @throws IllegalAccessException
     */
    private static void setFieldDateValue(Object parameter, Field field) throws IllegalAccessException {
        field.setAccessible(true);
        if (field.getType().equals(LocalDateTime.class)) {
            field.set(parameter, LocalDateTime.now());
        } else if (field.getType().equals(LocalDate.class)) {
            field.set(parameter, LocalDate.now());
        } else {
            field.set(parameter, new Date());
        }
    }


    /**
     * 生成MyBatis拦截器代理对象
     */
    @Override
    public Object plugin(Object target) {
        if (target instanceof org.apache.ibatis.executor.Executor) {
            return Plugin.wrap(target, this);
        }
        return target;
    }


    /**
     * 设置插件属性(直接通过Spring的方式获取属性,所以这个方法一般也用不到)
     * 项目启动的时候数据就会被加载
     */
    @Override
    public void setProperties(Properties properties) {
    }
}

4. 测试新增、修改日期自动填充

@Data
public class User {
    /**
     * 创建时间
     */
    @TableOperateField(value = TableOperateField.OperateType.Insert)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableOperateField(value = OperateType.InsertOrUpdate)
    private LocalDateTime updateTime;
}

5. Java实体类、List和表中json转换处理器

package com.yl.cache.handler;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * mybatis-plus 内置 AbstractJsonTypeHandler
 *
 * @author liuxubo
 * @date 2023/8/1 0:08
 */
public abstract class AbstractJsonTypeHandler<T> extends BaseTypeHandler<T> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, toJson(parameter));
    }

    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        final String json = rs.getString(columnName);
        return StringUtils.isBlank(json) ? null : parse(json);
    }

    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        final String json = rs.getString(columnIndex);
        return StringUtils.isBlank(json) ? null : parse(json);
    }

    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        final String json = cs.getString(columnIndex);
        return StringUtils.isBlank(json) ? null : parse(json);
    }

    /**
     * 将表中字段值为json字符串解析为Java对象
     *
     * @param json 表中字段值为json字符串
     * @return:T
     */
    protected abstract T parse(String json);

    /**
     * 将Java对象序列化为json字符串保存到表中
     *
     * @param obj Java对象
     * @return:java.lang.String
     */
    protected abstract String toJson(T obj);
}
package com.yl.cache.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yl.cache.annotation.TableOperateField;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Jackson 实现 JSON 字段类型处理器
 * 

List、Map、自定义对象,转换为json字符串存储到数据库,查询出来的时候自动转换为相应的对象 * * @author hubin * @since 2019-08-25 */ @Slf4j @MappedTypes({Object.class}) @MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CHAR, JdbcType.NCHAR, JdbcType.LONGVARCHAR}) public class JacksonTypeHandler extends AbstractJsonTypeHandler<Object> { private static ObjectMapper OBJECT_MAPPER; private final Class<?> type; /** * 构造函数 * * @param type 指定类型 * @date:2023/7/31 17:52 */ public JacksonTypeHandler(Class<?> type) { if (log.isTraceEnabled()) { log.trace("JacksonTypeHandler(" + type + ")"); } Objects.requireNonNull(type, "Type argument cannot be null"); this.type = type; } @Override protected Object parse(String json) { try { return getObjectMapper().readValue(json, type); } catch (IOException e) { throw new RuntimeException(e); } } @Override protected String toJson(Object obj) { try { return getObjectMapper().writeValueAsString(obj); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } public static ObjectMapper getObjectMapper() { if (null == OBJECT_MAPPER) { OBJECT_MAPPER = new ObjectMapper(); } return OBJECT_MAPPER; } public static void setObjectMapper(ObjectMapper objectMapper) { Objects.requireNonNull(objectMapper, "ObjectMapper should not be null"); JacksonTypeHandler.OBJECT_MAPPER = objectMapper; } /** * 查找实体类中含有注解 @TableOperateField(typeHandler = JacksonTypeHandler.class) 的字段 * * @param clazz class * @return 实体类中含有注解 @TableOperateField(typeHandler = JacksonTypeHandler.class) 的字段列表 */ public static List<Type> findJsonValueFieldName(Class<?> clazz) { if (clazz != null) { return Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.isAnnotationPresent(TableOperateField.class) && JacksonTypeHandler.class.isAssignableFrom(field.getAnnotation(TableOperateField.class).typeHandler())) .map(Field::getGenericType) .collect(Collectors.toList()); } return Collections.EMPTY_LIST; } } package com.yl.cache.handler; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.yl.cache.annotation.TableOperateField; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * Jackson 实现 JSON 字段类型处理器 *

List、Map、自定义对象,转换为json字符串存储到数据库,查询出来的时候自动转换为相应的对象 * * @author hubin * @since 2019-08-25 */ @Slf4j @MappedTypes({List.class}) @MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CHAR, JdbcType.NCHAR, JdbcType.LONGVARCHAR}) public class JacksonListTypeHandler extends AbstractJsonTypeHandler<List<Object>> { private static ObjectMapper OBJECT_MAPPER; private final Class<?> type; /** * 构造函数 * * @param type 指定类型 * @date:2023/7/31 17:52 */ public JacksonListTypeHandler(Class<?> type) { if (log.isTraceEnabled()) { log.trace("JacksonTypeHandler(" + type + ")"); } Objects.requireNonNull(type, "Type argument cannot be null"); this.type = type; } @Override protected List<Object> parse(String json) { try { return getObjectMapper().readValue(json, List.class); } catch (IOException e) { throw new RuntimeException(e); } } @Override protected String toJson(List<Object> obj) { try { return getObjectMapper().writeValueAsString(obj); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } public static ObjectMapper getObjectMapper() { if (null == OBJECT_MAPPER) { OBJECT_MAPPER = new ObjectMapper(); } return OBJECT_MAPPER; } public static void setObjectMapper(ObjectMapper objectMapper) { Objects.requireNonNull(objectMapper, "ObjectMapper should not be null"); JacksonListTypeHandler.OBJECT_MAPPER = objectMapper; } /** * 查找实体类中含有注解 @TableOperateField(typeHandler = JacksonTypeHandler.class) 的字段 * * @param clazz class * @return 实体类中含有注解 @TableOperateField(typeHandler = JacksonTypeHandler.class) 的字段列表 */ public static List<Type> findJsonValueFieldName(Class<?> clazz) { if (clazz != null) { return Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.isAnnotationPresent(TableOperateField.class) && JacksonListTypeHandler.class.isAssignableFrom(field.getAnnotation(TableOperateField.class).typeHandler())) .map(Field::getGenericType) .collect(Collectors.toList()); } return Collections.EMPTY_LIST; } } package com.yl.cache.handler; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.yl.cache.annotation.TableOperateField; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * Fastjson 实现 JSON 字段类型处理器 * * @author hubin * @since 2019-08-25 */ @Slf4j @MappedTypes({Object.class}) @MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CHAR, JdbcType.NCHAR, JdbcType.LONGVARCHAR}) public class FastjsonTypeHandler extends AbstractJsonTypeHandler<Object> { private final Class<?> type; public FastjsonTypeHandler(Class<?> type) { if (log.isTraceEnabled()) { log.trace("FastjsonTypeHandler(" + type + ")"); } Objects.requireNonNull(type, "Type argument cannot be null"); this.type = type; } @Override protected Object parse(String json) { return JSON.parseObject(json, type); } @Override protected String toJson(Object obj) { return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty); } /** * 查找实体类中含有注解 @TableOperateField(typeHandler = FastjsonTypeHandler.class) 的字段 * * @param clazz class * @return EnumValue字段 */ public static List<Type> findJsonValueFieldName(Class<?> clazz) { if (clazz != null) { return Arrays.stream(clazz.getDeclaredFields()) .filter(field -> field.isAnnotationPresent(TableOperateField.class) && FastjsonTypeHandler.class.isAssignableFrom(field.getAnnotation(TableOperateField.class).typeHandler())) .map(Field::getGenericType) .collect(Collectors.toList()); } return Collections.EMPTY_LIST; } }

6. 注册处理类

自定义配置扫描包type-json-package,指定扫描范围

mybatis:
  mapper-locations: classpath:mappers/*.xml
  type-aliases-package: com.yl.cache.entity
  configuration:
    map-underscore-to-camel-case: true
  type-json-package: com.yl.cache.entity

配置类

package com.yl.cache.config;

import com.yl.cache.handler.FastjsonTypeHandler;
import com.yl.cache.handler.JacksonListTypeHandler;
import com.yl.cache.handler.JacksonTypeHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * MyBatis配置自定义 typeHandler 推荐实现ConfigurationCustomizer接口配置方式
 *
 * @author liuxb
 * @date 2021/12/19 18:17
 */
@Slf4j
@org.springframework.context.annotation.Configuration
public class CommonMyBatisConfig implements ConfigurationCustomizer {
    private static final ResourcePatternResolver RESOURCE_PATTERN_RESOLVER = new PathMatchingResourcePatternResolver();
    private static final MetadataReaderFactory METADATA_READER_FACTORY = new CachingMetadataReaderFactory();

    @Value("${mybatis.type-json-package:}")
    private String typeJsonPackage;


    @Override
    public void customize(Configuration configuration) {
        // 设置null也返回
        configuration.setCallSettersOnNulls(true);
        // 设置一行都为null也返回
        configuration.setReturnInstanceForEmptyRow(true);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();

        //如果给定扫描json类包,注册类名为属性含有 @TableOperateField(typeHandler = JacksonTypeHandler.class)的实体类
        if (StringUtils.hasLength(typeJsonPackage)) {
            Set<Class<?>> classes = scanClasses(typeJsonPackage, null);
            if (classes.isEmpty()) {
                log.warn("Can't find class in '[" + typeJsonPackage + "]' package. Please check your configuration.");
                return;
            }

            Set<Type> jacksonTypes = classes.stream().map(cls -> JacksonTypeHandler.findJsonValueFieldName(cls))
                    .flatMap(types -> types.stream())
                    .collect(Collectors.toSet());
            registerTypeHandler(typeHandlerRegistry, jacksonTypes, JacksonTypeHandler.class);

            Set<Type> fastJsonTypes = classes.stream().map(cls -> FastjsonTypeHandler.findJsonValueFieldName(cls))
                    .flatMap(types -> types.stream())
                    .collect(Collectors.toSet());
            registerTypeHandler(typeHandlerRegistry, fastJsonTypes, FastjsonTypeHandler.class);

            Set<Type> jacksonListTypes = classes.stream().map(cls -> JacksonListTypeHandler.findJsonValueFieldName(cls))
                    .flatMap(types -> types.stream())
                    .collect(Collectors.toSet());
            registerTypeHandler(typeHandlerRegistry, jacksonListTypes, JacksonListTypeHandler.class);
        }

    }

    /**
     * 注册自定义类型处理器
     *
     * @param typeHandlerRegistry
     * @param types 类型
     */
    private void registerTypeHandler(TypeHandlerRegistry typeHandlerRegistry, Set<Type> types, Class typeHandlerClass) {
        if (!types.isEmpty()) {
            log.info("mybatis register json type handler {}", types);
            types.forEach(type -> {
                //集合类
                String typeName = type.getTypeName();
                if(typeName.contains("java.util.List<") && typeName.endsWith(">")){
                    Class<?> rawType = ((ParameterizedTypeImpl) type).getRawType();
                    typeHandlerRegistry.register(rawType, typeHandlerClass);
                }else{
                    //实体类
                    Class<?> clazz = (Class<?>) type;
                    if (org.apache.commons.lang3.ClassUtils.isPrimitiveOrWrapper(clazz) || String.class.equals(clazz)) {
                        throw new RuntimeException("请检查@TableOperateField(typeHandler = JacksonTypeHandler.class)标记的属性类型");
                    }
                    typeHandlerRegistry.register(clazz, typeHandlerClass);
                }
            });
        }
    }


    /**
     * 根据包路径和指定类的子类,获取当前包和子包下的全部类
     *
     * @param packagePatterns 指定包
     * @param assignableType  父类
     * @return
     */
    private Set<Class<?>> scanClasses(String packagePatterns, Class<?> assignableType) {
        Set<Class<?>> classes = new HashSet<>();
        try {
            String[] packagePatternArray = StringUtils.tokenizeToStringArray(packagePatterns, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
            for (String packagePattern : packagePatternArray) {
                Resource[] resources = RESOURCE_PATTERN_RESOLVER.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + ClassUtils.convertClassNameToResourcePath(packagePattern) + "/**/*.class");
                for (Resource resource : resources) {
                    try {
                        ClassMetadata classMetadata = METADATA_READER_FACTORY.getMetadataReader(resource).getClassMetadata();
                        Class<?> clazz = Resources.classForName(classMetadata.getClassName());
                        if (assignableType == null || assignableType.isAssignableFrom(clazz)) {
                            classes.add(clazz);
                        }
                    } catch (Throwable e) {
                        log.warn("Cannot load the '" + resource + "'. Cause by " + e.toString());
                    }
                }
            }
        } catch (IOException e) {
            log.warn("scan " + packagePatterns + ", io error");
        }
        return classes;
    }


}

7. 测试 保证增删改查接口都能正常

CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键id(自增)',
  `sys_warn_note` varchar(100) DEFAULT NULL COMMENT '系统告警信息',
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='测试用户表'

上送json字符串,JSONObject 接收,表字段为varchar,text,json类型都可以,使用mysql8
http://localhost:8080/jobWarnMessage/add
{
        "code": "006",
        "sysWarnNote": {
            "address": "北京大雨了"
        }
    }
@Data
public class User{
    @TableOperateField(typeHandler = JacksonTypeHandler.class)
    private JSONObject sysWarnNote;
}

mybatisJava对象、list和json转换_第1张图片
也可以为List

@Data
public class SysWarnNote {
    private String name;
    private String content;
}
@Data
public class User{
    @TableOperateField(typeHandler = JacksonListTypeHandler.class)
    private List<SysWarnNote> sysWarnNote;
}

你可能感兴趣的:(mybatis)