From 03c1c4f6a15f63758cc8864996577f961bad3af2 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Thu, 12 Aug 2021 19:02:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8mybatis=E7=9A=84typehandler?= =?UTF-8?q?=E6=98=A0=E5=B0=84json=EF=BC=8C=E5=87=8F=E5=B0=91=E5=86=97?= =?UTF-8?q?=E4=BD=99=E4=BB=A3=E7=A0=81=E5=92=8C=E6=8F=90=E5=8D=87=E5=8F=AF?= =?UTF-8?q?=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/youlai/mall/pms/pojo/dto/SpuDTO.java | 2 +- .../youlai/mall/pms/pojo/entity/PmsSpu.java | 2 +- .../pms/service/impl/PmsSpuServiceImpl.java | 19 --- .../mybatis/config/MybatisPlusConfig.java | 17 +++ .../handler/ArrayObjectJsonTypeHandler.java | 120 ++++++++++++++++++ .../handler/IntegerArrayJsonTypeHandler.java | 24 ++++ .../handler/LongArrayJsonTypeHandler.java | 24 ++++ .../handler/StringArrayJsonTypeHandler.java | 21 +++ 8 files changed, 208 insertions(+), 21 deletions(-) create mode 100644 youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/ArrayObjectJsonTypeHandler.java create mode 100644 youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/IntegerArrayJsonTypeHandler.java create mode 100644 youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/LongArrayJsonTypeHandler.java create mode 100644 youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/StringArrayJsonTypeHandler.java diff --git a/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/dto/SpuDTO.java b/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/dto/SpuDTO.java index 9539d1084..7a86f0ec0 100644 --- a/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/dto/SpuDTO.java +++ b/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/dto/SpuDTO.java @@ -14,7 +14,7 @@ public class SpuDTO { private Long price; private Integer sales; private String pic; - private List pics; + private String[] pics; private String unit; private String description; private String detail; diff --git a/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/entity/PmsSpu.java b/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/entity/PmsSpu.java index abc5eab51..f4e103946 100644 --- a/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/entity/PmsSpu.java +++ b/mall-pms/pms-api/src/main/java/com/youlai/mall/pms/pojo/entity/PmsSpu.java @@ -25,7 +25,7 @@ public class PmsSpu extends BaseEntity { private Long price; private Integer sales; private String pic; - private String pics; + private String[] pics; private String unit; private String description; private String detail; diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java index a9cb92ed5..3bd6ce97a 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java @@ -55,10 +55,6 @@ public class PmsSpuServiceImpl extends ServiceImpl impleme // spu保存 PmsSpu spu = new PmsSpu(); BeanUtil.copyProperties(SpuDTO, spu); - if (SpuDTO.getPics() != null) { - String picUrls = JSONUtil.toJsonStr(SpuDTO.getPics()); - spu.setPics(picUrls); - } this.save(spu); // 属性保存 @@ -89,12 +85,6 @@ public class PmsSpuServiceImpl extends ServiceImpl impleme PmsSpu spu = this.getById(id); BeanUtil.copyProperties(spu, spuDTO); - if (StrUtil.isNotBlank(spu.getPics())) { - // spu专辑图片转换处理 json字符串 -> List - List pics = JSONUtil.toList(JSONUtil.parseArray(spu.getPics()), String.class); - spuDTO.setPics(pics); - } - // 属性 List attrs = iPmsSpuAttributeValueService. list(new LambdaQueryWrapper().eq(PmsSpuAttributeValue::getSpuId, id)); @@ -120,10 +110,6 @@ public class PmsSpuServiceImpl extends ServiceImpl impleme // spu保存 PmsSpu spu = new PmsSpu(); BeanUtil.copyProperties(SpuDTO, spu); - if (SpuDTO.getPics() != null) { - String pics = JSONUtil.toJsonStr(SpuDTO.getPics()); - spu.setPics(pics); - } this.updateById(spu); // 属性保存 @@ -202,11 +188,6 @@ public class PmsSpuServiceImpl extends ServiceImpl impleme PmsSpu spu = this.getById(spuId); SpuDTO SpuDTO = new SpuDTO(); BeanUtil.copyProperties(spu, SpuDTO); - if (StrUtil.isNotBlank(spu.getPics())) { - // spu专辑图片转换处理 json字符串 -> List - List pics = JSONUtil.toList(JSONUtil.parseArray(spu.getPics()), String.class); - SpuDTO.setPics(pics); - } // 属性 List attrs = iPmsSpuAttributeValueService.list( new LambdaQueryWrapper( diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java index c0cc2b326..4812d720e 100644 --- a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java @@ -1,11 +1,17 @@ package com.youlai.common.mybatis.config; import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.core.config.GlobalConfig; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.youlai.common.mybatis.handler.FieldFillHandler; +import com.youlai.common.mybatis.handler.IntegerArrayJsonTypeHandler; +import com.youlai.common.mybatis.handler.LongArrayJsonTypeHandler; +import com.youlai.common.mybatis.handler.StringArrayJsonTypeHandler; import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.TypeHandlerRegistry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; @@ -26,6 +32,17 @@ public class MybatisPlusConfig { return interceptor; } + @Bean + public ConfigurationCustomizer configurationCustomizer() { + return configuration -> { + // 全局注册自定义TypeHandler + TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); + typeHandlerRegistry.register(String[].class, JdbcType.OTHER, StringArrayJsonTypeHandler.class); + typeHandlerRegistry.register(Long[].class, JdbcType.OTHER, LongArrayJsonTypeHandler.class); + typeHandlerRegistry.register(Integer[].class, JdbcType.OTHER, IntegerArrayJsonTypeHandler.class); + }; + } + /** * 自动填充数据库创建人、创建时间、更新人、更新时间 */ diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/ArrayObjectJsonTypeHandler.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/ArrayObjectJsonTypeHandler.java new file mode 100644 index 000000000..74d68f56b --- /dev/null +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/ArrayObjectJsonTypeHandler.java @@ -0,0 +1,120 @@ +package com.youlai.common.mybatis.handler; + +import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.springframework.util.StringUtils; + +import java.lang.reflect.Array; +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Objects; + +/** + * 数组类型转换 json + *

+ * 主要是用于对象数据 基础类型包装对象不建议用 + * https://www.jianshu.com/p/ab832f3fe81c + * + * @author Gadfly + * @since 2021-06-30 15:20 + */ +@Slf4j +@MappedJdbcTypes(value = {JdbcType.OTHER}, includeNullJdbcType = true) +public class ArrayObjectJsonTypeHandler extends BaseTypeHandler { + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final String STRING_JSON_ARRAY_EMPTY = "[]"; + + static { + // 未知字段忽略 + MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + // 不使用科学计数 + MAPPER.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true); + // null 值不输出(节省内存) + MAPPER.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL); + } + + private final Class type; + + public ArrayObjectJsonTypeHandler(Class type) { + Objects.requireNonNull(type); + this.type = type; + } + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, E[] parameter, JdbcType jdbcType) throws SQLException { + ps.setString(i, toJson(parameter)); + } + + @Override + public E[] getNullableResult(ResultSet rs, String columnName) throws SQLException { + return toObject(rs.getString(columnName), type); + } + + @Override + public E[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + return toObject(rs.getString(columnIndex), type); + } + + @Override + public E[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + return toObject(cs.getString(columnIndex), type); + } + + /** + * object 转 json + * + * @param obj 对象 + * @return String json字符串 + */ + private String toJson(E[] obj) { + if (ArrayUtils.isEmpty(obj)) { + return STRING_JSON_ARRAY_EMPTY; + } + + try { + return MAPPER.writeValueAsString(obj); + } catch (JsonProcessingException e) { + throw new RuntimeException("mybatis column to json error,obj:" + Arrays.toString(obj), e); + } + } + + /** + * 转换对象 + * + * @param json json数据 + * @param clazz 类 + * @return E + */ + private E[] toObject(String json, Class clazz) { + if (json == null) { + return null; + } + + if (!StringUtils.hasText(json)) { + return newArray(clazz); + } + + try { + return MAPPER.readValue(json, clazz); + } catch (JsonProcessingException e) { + log.error("mybatis column json to object error,json:{}", json, e); + return newArray(clazz); + } + } + + @SuppressWarnings("unchecked") + private E[] newArray(Class clazz) { + return (E[]) Array.newInstance(clazz.getComponentType(), 0); + } +} diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/IntegerArrayJsonTypeHandler.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/IntegerArrayJsonTypeHandler.java new file mode 100644 index 000000000..08474be07 --- /dev/null +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/IntegerArrayJsonTypeHandler.java @@ -0,0 +1,24 @@ +package com.youlai.common.mybatis.handler; + +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; +import org.springframework.stereotype.Component; + +/** + * Integer 数组类型转换 json + * https://www.jianshu.com/p/ab832f3fe81c + * + * @author Gadfly + * @since 2021-06-30 15:19 + */ +@Slf4j +@Component +@MappedTypes(value = {Integer[].class}) +@MappedJdbcTypes(value = {JdbcType.VARCHAR}, includeNullJdbcType = true) +public class IntegerArrayJsonTypeHandler extends ArrayObjectJsonTypeHandler { + public IntegerArrayJsonTypeHandler() { + super(Integer[].class); + } +} diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/LongArrayJsonTypeHandler.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/LongArrayJsonTypeHandler.java new file mode 100644 index 000000000..2965b1719 --- /dev/null +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/LongArrayJsonTypeHandler.java @@ -0,0 +1,24 @@ +package com.youlai.common.mybatis.handler; + +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; +import org.springframework.stereotype.Component; + +/** + * Long 数组类型转换 json + * https://www.jianshu.com/p/ab832f3fe81c + * + * @author Gadfly + * @since 2021-06-30 15:26 + */ +@Slf4j +@Component +@MappedTypes(value = {Long[].class}) +@MappedJdbcTypes(value = {JdbcType.OTHER}, includeNullJdbcType = true) +public class LongArrayJsonTypeHandler extends ArrayObjectJsonTypeHandler { + public LongArrayJsonTypeHandler() { + super(Long[].class); + } +} diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/StringArrayJsonTypeHandler.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/StringArrayJsonTypeHandler.java new file mode 100644 index 000000000..025409149 --- /dev/null +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/StringArrayJsonTypeHandler.java @@ -0,0 +1,21 @@ +package com.youlai.common.mybatis.handler; + +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; +import org.springframework.stereotype.Component; + +/** + * @author Gadfly + * @since 2021-06-30 15:27 + */ +@Slf4j +@Component +@MappedTypes(value = {String[].class}) +@MappedJdbcTypes(value = {JdbcType.OTHER}, includeNullJdbcType = true) +public class StringArrayJsonTypeHandler extends ArrayObjectJsonTypeHandler { + public StringArrayJsonTypeHandler() { + super(String[].class); + } +}