1. List<String> list = new ArrayList<>();
  2. list.add("123);
  3. list.add("456)
  4. Gson gson = new Gson();
  5. String jsonResult = gson.toJson(list, new TypeToken<List<?>>(){}.getType());
  6. JSONArray obj = JSONArray.parseArray(jsonResult);
  7. System.out.println("json =" + obj);

1,转Json工具类

  1. package net.xiaoxiangshop.util;
  2. import java.io.IOException;
  3. import java.io.Writer;
  4. import java.lang.reflect.Type;
  5. import org.springframework.util.Assert;
  6. import com.fasterxml.jackson.core.JsonGenerationException;
  7. import com.fasterxml.jackson.core.JsonParseException;
  8. import com.fasterxml.jackson.core.JsonProcessingException;
  9. import com.fasterxml.jackson.core.type.TypeReference;
  10. import com.fasterxml.jackson.databind.JavaType;
  11. import com.fasterxml.jackson.databind.JsonMappingException;
  12. import com.fasterxml.jackson.databind.JsonNode;
  13. import com.fasterxml.jackson.databind.ObjectMapper;
  14. import com.fasterxml.jackson.databind.type.TypeFactory;
  15. /**
  16. * Utils - JSON
  17. *
  18. */
  19. public final class JsonUtils {
  20. /**
  21. * ObjectMapper
  22. */
  23. private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  24. /**
  25. * 不可实例化
  26. */
  27. private JsonUtils() {
  28. }
  29. /**
  30. * 将对象转换为JSON字符串
  31. *
  32. * @param value
  33. * 对象
  34. * @return JSON字符串
  35. */
  36. public static String toJson(Object value) {
  37. Assert.notNull(value, "[Assertion failed] - value is required; it must not be null");
  38. try {
  39. return OBJECT_MAPPER.writeValueAsString(value);
  40. } catch (JsonProcessingException e) {
  41. throw new RuntimeException(e.getMessage(), e);
  42. }
  43. }
  44. /**
  45. * 将JSON字符串转换为对象
  46. *
  47. * @param json
  48. * JSON字符串
  49. * @param valueType
  50. * 类型
  51. * @return 对象
  52. */
  53. public static <T> T toObject(String json, Class<T> valueType) {
  54. Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
  55. Assert.notNull(valueType, "[Assertion failed] - valueType is required; it must not be null");
  56. try {
  57. return OBJECT_MAPPER.readValue(json, valueType);
  58. } catch (JsonParseException e) {
  59. throw new RuntimeException(e.getMessage(), e);
  60. } catch (JsonMappingException e) {
  61. throw new RuntimeException(e.getMessage(), e);
  62. } catch (IOException e) {
  63. throw new RuntimeException(e.getMessage(), e);
  64. }
  65. }
  66. /**
  67. * 将JSON字符串转换为对象
  68. *
  69. * @param json
  70. * JSON字符串
  71. * @param typeReference
  72. * 类型
  73. * @return 对象
  74. */
  75. public static <T> T toObject(String json, TypeReference<?> typeReference) {
  76. Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
  77. Assert.notNull(typeReference, "[Assertion failed] - typeReference is required; it must not be null");
  78. try {
  79. return OBJECT_MAPPER.readValue(json, typeReference);
  80. } catch (JsonParseException e) {
  81. throw new RuntimeException(e.getMessage(), e);
  82. } catch (JsonMappingException e) {
  83. throw new RuntimeException(e.getMessage(), e);
  84. } catch (IOException e) {
  85. throw new RuntimeException(e.getMessage(), e);
  86. }
  87. }
  88. /**
  89. * 将JSON字符串转换为对象
  90. *
  91. * @param json
  92. * JSON字符串
  93. * @param javaType
  94. * 类型
  95. * @return 对象
  96. */
  97. public static <T> T toObject(String json, JavaType javaType) {
  98. Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
  99. Assert.notNull(javaType, "[Assertion failed] - javaType is required; it must not be null");
  100. try {
  101. return OBJECT_MAPPER.readValue(json, javaType);
  102. } catch (JsonParseException e) {
  103. throw new RuntimeException(e.getMessage(), e);
  104. } catch (JsonMappingException e) {
  105. throw new RuntimeException(e.getMessage(), e);
  106. } catch (IOException e) {
  107. throw new RuntimeException(e.getMessage(), e);
  108. }
  109. }
  110. /**
  111. * 将JSON字符串转换为树
  112. *
  113. * @param json
  114. * JSON字符串
  115. * @return 树
  116. */
  117. public static JsonNode toTree(String json) {
  118. Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
  119. try {
  120. return OBJECT_MAPPER.readTree(json);
  121. } catch (JsonProcessingException e) {
  122. throw new RuntimeException(e.getMessage(), e);
  123. } catch (IOException e) {
  124. throw new RuntimeException(e.getMessage(), e);
  125. }
  126. }
  127. /**
  128. * 将对象转换为JSON流
  129. *
  130. * @param writer
  131. * Writer
  132. * @param value
  133. * 对象
  134. */
  135. public static void writeValue(Writer writer, Object value) {
  136. Assert.notNull(writer, "[Assertion failed] - writer is required; it must not be null");
  137. Assert.notNull(value, "[Assertion failed] - value is required; it must not be null");
  138. try {
  139. OBJECT_MAPPER.writeValue(writer, value);
  140. } catch (JsonGenerationException e) {
  141. throw new RuntimeException(e.getMessage(), e);
  142. } catch (JsonMappingException e) {
  143. throw new RuntimeException(e.getMessage(), e);
  144. } catch (IOException e) {
  145. throw new RuntimeException(e.getMessage(), e);
  146. }
  147. }
  148. /**
  149. * 构造类型
  150. *
  151. * @param type
  152. * 类型
  153. * @return 类型
  154. */
  155. public static JavaType constructType(Type type) {
  156. Assert.notNull(type, "[Assertion failed] - type is required; it must not be null");
  157. return TypeFactory.defaultInstance().constructType(type);
  158. }
  159. /**
  160. * 构造类型
  161. *
  162. * @param typeReference
  163. * 类型
  164. * @return 类型
  165. */
  166. public static JavaType constructType(TypeReference<?> typeReference) {
  167. Assert.notNull(typeReference, "[Assertion failed] - typeReference is required; it must not be null");
  168. return TypeFactory.defaultInstance().constructType(typeReference);
  169. }
  170. }

2,Mybatis类型处理器(typeHandlers),以List为例:

  1. package net.xiaoxiangshop.handlers;
  2. import java.sql.CallableStatement;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.List;
  7. import net.xiaoxiangshop.entity.ProductImage;
  8. import net.xiaoxiangshop.util.JsonUtils;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.apache.ibatis.type.BaseTypeHandler;
  11. import org.apache.ibatis.type.JdbcType;
  12. import org.apache.ibatis.type.MappedJdbcTypes;
  13. import com.fasterxml.jackson.core.type.TypeReference;
  14. @MappedJdbcTypes({JdbcType.LONGVARCHAR})
  15. public class ProductImageHandler extends BaseTypeHandler<List<ProductImage>>{
  16. @Override
  17. public void setNonNullParameter(PreparedStatement ps, int i, List<ProductImage> parameter, JdbcType jdbcType) throws SQLException {
  18. if (parameter != null) {
  19. ps.setString(i, JsonUtils.toJson(parameter));
  20. }
  21. }
  22. @Override
  23. public List<ProductImage> getNullableResult(ResultSet rs, String columnName) throws SQLException {
  24. String dbData = rs.getString(columnName);
  25. if (StringUtils.isEmpty(dbData)) {
  26. return null;
  27. }
  28. return JsonUtils.toObject(dbData, new TypeReference<List<ProductImage>>() { });
  29. }
  30. @Override
  31. public List<ProductImage> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  32. String dbData = rs.getString(columnIndex);
  33. if (StringUtils.isEmpty(dbData)) {
  34. return null;
  35. }
  36. return JsonUtils.toObject(dbData, new TypeReference<List<ProductImage>>() { });
  37. }
  38. @Override
  39. public List<ProductImage> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  40. String dbData = cs.getString(columnIndex);
  41. if (StringUtils.isEmpty(dbData)) {
  42. return null;
  43. }
  44. return JsonUtils.toObject(dbData, new TypeReference<List<ProductImage>>() { });
  45. }
  46. }

3,XML配置:

  1. <!-- 更新 -->
  2. <update id="updateImage" parameterType="net.xiaoxiangshop.entity.vo.ProductVo">
  3. UPDATE product
  4. SET last_modified_date = #{entity.lastModifiedDate},
  5. product_images = #{entity.productImages, typeHandler=net.xiaoxiangshop.handlers.ProductImageHandler}
  6. WHERE id = #{entity.id}
  7. AND version = #{entity.version};
  8. </update>

4,实体类定义,productImages字段为List类型:

  1. package net.xiaoxiangshop.entity.vo;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.Data;
  6. import net.xiaoxiangshop.entity.ProductImage;
  7. import java.io.Serializable;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11. @Data
  12. @TableName("product")
  13. public class ProductVo implements Serializable {
  14. private static final long serialVersionUID = 1L;
  15. @TableId(value = "id")
  16. private Long id;
  17. private List<ProductImage> productImages = new ArrayList<>();
  18. private Date lastModifiedDate;
  19. private Long version;
  20. }

5,文档地址:

https://mybatis.org/mybatis-3/zh/configuration.html