List<String> list = new ArrayList<>();
list.add("123);
list.add("456)
Gson gson = new Gson();
String jsonResult = gson.toJson(list, new TypeToken<List<?>>(){}.getType());
JSONArray obj = JSONArray.parseArray(jsonResult);
System.out.println("json =" + obj);
1,转Json工具类
package net.xiaoxiangshop.util;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Type;
import org.springframework.util.Assert;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
* Utils - JSON
*
*/
public final class JsonUtils {
/**
* ObjectMapper
*/
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
* 不可实例化
*/
private JsonUtils() {
}
/**
* 将对象转换为JSON字符串
*
* @param value
* 对象
* @return JSON字符串
*/
public static String toJson(Object value) {
Assert.notNull(value, "[Assertion failed] - value is required; it must not be null");
try {
return OBJECT_MAPPER.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将JSON字符串转换为对象
*
* @param json
* JSON字符串
* @param valueType
* 类型
* @return 对象
*/
public static <T> T toObject(String json, Class<T> valueType) {
Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
Assert.notNull(valueType, "[Assertion failed] - valueType is required; it must not be null");
try {
return OBJECT_MAPPER.readValue(json, valueType);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将JSON字符串转换为对象
*
* @param json
* JSON字符串
* @param typeReference
* 类型
* @return 对象
*/
public static <T> T toObject(String json, TypeReference<?> typeReference) {
Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
Assert.notNull(typeReference, "[Assertion failed] - typeReference is required; it must not be null");
try {
return OBJECT_MAPPER.readValue(json, typeReference);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将JSON字符串转换为对象
*
* @param json
* JSON字符串
* @param javaType
* 类型
* @return 对象
*/
public static <T> T toObject(String json, JavaType javaType) {
Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
Assert.notNull(javaType, "[Assertion failed] - javaType is required; it must not be null");
try {
return OBJECT_MAPPER.readValue(json, javaType);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将JSON字符串转换为树
*
* @param json
* JSON字符串
* @return 树
*/
public static JsonNode toTree(String json) {
Assert.hasText(json, "[Assertion failed] - json must have text; it must not be null, empty, or blank");
try {
return OBJECT_MAPPER.readTree(json);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 将对象转换为JSON流
*
* @param writer
* Writer
* @param value
* 对象
*/
public static void writeValue(Writer writer, Object value) {
Assert.notNull(writer, "[Assertion failed] - writer is required; it must not be null");
Assert.notNull(value, "[Assertion failed] - value is required; it must not be null");
try {
OBJECT_MAPPER.writeValue(writer, value);
} catch (JsonGenerationException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* 构造类型
*
* @param type
* 类型
* @return 类型
*/
public static JavaType constructType(Type type) {
Assert.notNull(type, "[Assertion failed] - type is required; it must not be null");
return TypeFactory.defaultInstance().constructType(type);
}
/**
* 构造类型
*
* @param typeReference
* 类型
* @return 类型
*/
public static JavaType constructType(TypeReference<?> typeReference) {
Assert.notNull(typeReference, "[Assertion failed] - typeReference is required; it must not be null");
return TypeFactory.defaultInstance().constructType(typeReference);
}
}
2,Mybatis类型处理器(typeHandlers),以List为例:
package net.xiaoxiangshop.handlers;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import net.xiaoxiangshop.entity.ProductImage;
import net.xiaoxiangshop.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import com.fasterxml.jackson.core.type.TypeReference;
@MappedJdbcTypes({JdbcType.LONGVARCHAR})
public class ProductImageHandler extends BaseTypeHandler<List<ProductImage>>{
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List<ProductImage> parameter, JdbcType jdbcType) throws SQLException {
if (parameter != null) {
ps.setString(i, JsonUtils.toJson(parameter));
}
}
@Override
public List<ProductImage> getNullableResult(ResultSet rs, String columnName) throws SQLException {
String dbData = rs.getString(columnName);
if (StringUtils.isEmpty(dbData)) {
return null;
}
return JsonUtils.toObject(dbData, new TypeReference<List<ProductImage>>() { });
}
@Override
public List<ProductImage> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String dbData = rs.getString(columnIndex);
if (StringUtils.isEmpty(dbData)) {
return null;
}
return JsonUtils.toObject(dbData, new TypeReference<List<ProductImage>>() { });
}
@Override
public List<ProductImage> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String dbData = cs.getString(columnIndex);
if (StringUtils.isEmpty(dbData)) {
return null;
}
return JsonUtils.toObject(dbData, new TypeReference<List<ProductImage>>() { });
}
}
3,XML配置:
<!-- 更新 -->
<update id="updateImage" parameterType="net.xiaoxiangshop.entity.vo.ProductVo">
UPDATE product
SET last_modified_date = #{entity.lastModifiedDate},
product_images = #{entity.productImages, typeHandler=net.xiaoxiangshop.handlers.ProductImageHandler}
WHERE id = #{entity.id}
AND version = #{entity.version};
</update>
4,实体类定义,productImages字段为List类型:
package net.xiaoxiangshop.entity.vo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import net.xiaoxiangshop.entity.ProductImage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Data
@TableName("product")
public class ProductVo implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id")
private Long id;
private List<ProductImage> productImages = new ArrayList<>();
private Date lastModifiedDate;
private Long version;
}
5,文档地址:
https://mybatis.org/mybatis-3/zh/configuration.html