In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how MybatisPlus customizes the TypeHandler mapping JSON type to List. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Custom TypeHandler mapping JSON type is List1. Entity class
Only the fields that need to be mapped are shown here, and annotations are added to the fields and entity classes that you want to map.
@ Data@TableName (value = "report", autoResultMap = true) public class Report {private static final long serialVersionUID = 1L; @ ApiModelProperty ("id") @ TableId (value = "id", type = IdType.AUTO) private Integer id; @ ApiModelProperty ("Registration Information") @ TableField (typeHandler = ReportUserListTypeHandler.class) private List reportInfo;} 2. ListTypeHandler
Provides a processor that converts JSONArray to Java List collections
Import cn.hutool.core.collection.CollUtil;import cn.hutool.core.util.StrUtil;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.TypeReference;import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.MappedJdbcTypes;import org.apache.ibatis.type.MappedTypes; import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List MappedJdbcTypes (JdbcType.VARBINARY) @ MappedTypes ({List.class}) public abstract class ListTypeHandler extends BaseTypeHandler {@ Override public void setNonNullParameter (PreparedStatement ps, int I, List parameter, JdbcType jdbcType) throws SQLException {String content = CollUtil.isEmpty (parameter)? Null: JSON.toJSONString (parameter); ps.setString (I, content);} @ Override public List getNullableResult (ResultSet rs, String columnName) throws SQLException {return this.getListByJsonArrayString (rs.getString (columnName));} @ Override public List getNullableResult (ResultSet rs, int columnIndex) throws SQLException {return this.getListByJsonArrayString (rs.getString (columnIndex)) } @ Override public List getNullableResult (CallableStatement cs, int columnIndex) throws SQLException {return this.getListByJsonArrayString (cs.getString (columnIndex));} private List getListByJsonArrayString (String content) {return StrUtil.isBlank (content)? New ArrayList (): JSON.parseObject (content, this.specificType ());} / * * specific type, provided by subclass * * @ return specific type * / protected abstract TypeReference specificType ();} 3. ReportUserListTypeHandler
List collection generic types are provided by concrete subclasses
Import com.alibaba.fastjson.TypeReference;import com.hanku.business.model.ReportUser; import java.util.List; public class ReportUserListTypeHandler extends ListTypeHandler {@ Override protected TypeReference specificType () {return new TypeReference () {};}} 4. Java generics
If the TypeReference type is provided directly in the ListTypeHandler class, it is equivalent to the TypeReference type. Subsequent fastjson cannot determine the specific Java type during conversion, and the converted type will eventually be List; similarly, if you use Jackson as a JSON conversion tool, when you are not sure of the specific type, it will always be converted to LinkedHashMap type, and you need to use TypeReference to convert again.
Custom usage notes for TypeHandler
A custom TypeHandler can be used to automatically convert an attribute when it is inserted into the database and queried. In this case, the attribute of type Map is converted to CLOB and then stored in the database. Due to the complexity of the Map,mp comes with the json converter will lose part of the information.
Type converters can also configure java types and jdbc types through annotations
@ MappedTypes: annotated configuration java type
@ MappedJdbcTypes: annotated configuration jdbc type
Define @ Slf4j@MappedTypes ({Object.class}) @ MappedJdbcTypes (JdbcType.VARCHAR) public class WeightListTypeHandler extends AbstractJsonTypeHandler {private static Gson gson = new Gson (); private final Class type; public WeightListTypeHandler (Class type) {if (log.isTraceEnabled ()) {log.trace ("WeightListTypeHandler (" + type + ")");} Assert.notNull (type, "Type argument cannot be null"); this.type = type } @ Override protected Object parse (String json) {Type type1 = new TypeToken () {} .getType (); return gson.fromJson (json, type1);} @ Override protected String toJson (Object obj) {return gson.toJson (obj);} public static void setGson (Gson gson) {Assert.notNull (gson, "Gson should not be null"); WeightListTypeHandler.gson = gson;}} use
Note the @ TableName annotation autoResultMap attribute
@ Data@NoArgsConstructor@TableName (value = "mix_target", autoResultMap = true) public class MixTarget extends Model {@ TableId (value = "id", type = IdType.AUTO) private Long id; / * indicator description * / @ TableField ("description") private String description; / * indicator name * / @ TableField ("name") private String name / * corresponding attribute name * / @ TableField ("property_name") private String propertyName; / * starting point type * / @ TableField ("source_type") private String sourceType / * attribute corresponds to weight list * key attribute name value weight under the specified condition * / @ TableField (value = "weight_list", typeHandler = WeightListTypeHandler.class,jdbcType = JdbcType.CLOB) private Map weightList / * running status * 0 New not running * 1 running * 2 running successfully * 3 running failed * / @ TableField ("status") private Integer status; / * * whether * 1 true * 0 false * / @ TableField ("enable") private Integer enable is available @ TableField ("create_time") private LocalDateTime createTime;} this is the end of the article on "how to customize TypeHandler mapping JSON type to List by MybatisPlus". I hope the above content can be of some help to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.