|
@@ -0,0 +1,96 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
|
|
|
+ * <p>
|
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
+ * use this file except in compliance with the License. You may obtain a copy of
|
|
|
+ * the License at
|
|
|
+ * <p>
|
|
|
+ * https://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ * <p>
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
+ * License for the specific language governing permissions and limitations under
|
|
|
+ * the License.
|
|
|
+ */
|
|
|
+package com.baomidou.mybatisplus.extension.handlers;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+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 java.io.IOException;
|
|
|
+import java.sql.CallableStatement;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Jackson 实现 JSON 字段类型处理器
|
|
|
+ *
|
|
|
+ * @author hubin
|
|
|
+ * @since 2019-08-25
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@MappedJdbcTypes(JdbcType.VARCHAR)
|
|
|
+public class JacksonTypeHandler extends BaseTypeHandler<Object> {
|
|
|
+ private static ObjectMapper objectMapper;
|
|
|
+ private Class<Object> type;
|
|
|
+
|
|
|
+ static {
|
|
|
+ objectMapper = new ObjectMapper();
|
|
|
+ }
|
|
|
+
|
|
|
+ public JacksonTypeHandler(Class<Object> type) {
|
|
|
+ if (log.isTraceEnabled()) {
|
|
|
+ log.trace("JacksonTypeHandler(" + type + ")");
|
|
|
+ }
|
|
|
+ if (null == type) {
|
|
|
+ throw new MybatisPlusException("Type argument cannot be null");
|
|
|
+ }
|
|
|
+ this.type = type;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object 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 Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ return parse(rs.getString(columnName));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ return parse(rs.getString(columnIndex));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ return parse(cs.getString(columnIndex));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int columnIndex, Object parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ ps.setString(columnIndex, toJsonString(parameter));
|
|
|
+ }
|
|
|
+}
|