import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 自定义类型处理器JsonTypeHanlder
*/
public class JsonTypeHanlder<T> implements TypeHandler<T> {
private Class<T> type;
public JsonTypeHanlder(Class<T> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
// 拿到xml中配置的javaType
this.type = type;
}
@Override
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parseJsonString(parameter));
}
@Override
public T getResult(ResultSet rs, String columnName) throws SQLException {
return parseJavaObject(rs.getString(columnName));
}
@Override
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
return parseJavaObject(rs.getString(columnIndex));
}
@Override
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
return parseJavaObject(cs.getString(columnIndex));
}
private String parseJsonString(T parameter) {
if (parameter == null) {
return null;
}
return JSON.toJSONString(parameter);
}
@SuppressWarnings("unchecked")
private T parseJavaObject(String parameter) {
if (parameter == null) {
return null;
}
if (parameter.startsWith("[")) {
return (T) JSON.parseArray(parameter, this.type);
}
return JSON.parseObject(parameter, this.type);
}
}
or
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 自定义类型处理器JsonTypeHanlder
*/
public class JsonTypeHanlder<T> extends BaseTypeHandler<T> {
private final static Log log = LogFactory.getLog(JsonTypeHanlder.class);
private static ObjectMapper objectMapper;
private Class<T> type;
static {
objectMapper = new ObjectMapper();
}
public JsonTypeHanlder(Class<T> type) {
if (log.isTraceEnabled()) {
log.trace("JsonTypeHandler(" + type + ")");
}
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
}
private T parse(String json) {
try {
if (json == null || json.length() == 0) {
return null;
}
return objectMapper.readValue(json, type);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String toJsonString(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
return (T) parse(rs.getString(columnName));
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return (T) parse(rs.getString(columnIndex));
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException
{
return (T) parse(cs.getString(columnIndex));
}
@Override
public void setNonNullParameter(PreparedStatement ps, int columnIndex, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(columnIndex, toJsonString(parameter));
}
}
public class SysDept extends BaseEntity {
private ArrayList<String> departmentLeader;
}
<resultMap type="SysDept" id="SysDeptResult">
<result property="departmentLeader" column="department_leader" jdbcType="OTHER"
typeHandler="包名.JsonTypeHanlder" />
</resultMap>
<insert id="insertDept" parameterType="SysDept">
insert into sys_dept
(<if test="departmentLeader != null and departmentLeader != ''">
department_leader,
</if>)
values(
<if test="departmentLeader != null and departmentLeader != ''">
#{departmentLeader,jdbcType=OTHER
,typeHandler=包名.JsonTypeHanlder},
</if>)
</insert>