Mybatis plus about the operation of JSON fields in MYSQL

Mybatis plus about the operation of JSON fields in MYSQL

First of all, the JSON field is applicable only in Sql5.7+. When you use it, you must pay attention to your MySql version!!!

1. Mysql related fields

2. JSON content inserted in related fields

3. Modify the corresponding entity class

The two fields related to us in content 3 are JSON types in mysql. For our convenience, we can customize the encapsulation object to receive and pass our JSON fields. The following two fields share a type of encapsulated object, as follows: premaritalandpremaritaninfo. Here we can write a static class to encapsulate under the current entity class, or write a separate class to encapsulate.

In the entity class we are concerned about, there are two attributes in the annotation that we are concerned about:
@TableName(value = "t_ppaal",autoResultMap = true)
autoResultMap = true: automatically match the mapping of resultmap in our mapper.xml.

@TableField(value = "premarital", typeHandler = JacksonTypeHandler.class)
typeHandler = JacksonTypeHandler.class: because this is the field receiving of Json type, we add a TypeHandler of Json type, which is the field handler in MybatisPlus. We can use it to deal with entity class attributes and table fields.

@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "t_ppaal",autoResultMap = true)
public class Ppaal extends BaseEntity implements Serializable {
	/**
	/**
	 * Classification of diseases detected before marriage HQ003
	 */
	@TableField(value = "premarital", typeHandler = JacksonTypeHandler.class)
	@ApiModelProperty(value = "Classification of diseases detected before marriage HQ003")
	private PremaritalAndPremaritalInfo premarital;

	/**
	 * Pre pregnancy risk factors HQ004
	 */
	@TableField(value = "progestation", typeHandler = JacksonTypeHandler.class)
	@ApiModelProperty(value = "Pre pregnancy risk factors HQ004")
	private PremaritalAndPremaritalInfo progestation;
	/**
	 * Encapsulation of json fields
	 */
	@Data
	public static class PremaritalAndPremaritalInfo implements Serializable {
		private List<String> woman;
		private List<String> man;
	}
}

4. Modify the corresponding mapper.xml

What needs to be modified here are jdbcType, typeHandler and javaType. If not, just add it
First, modify the corresponding typeHandler, which is the same as that in the entity class.
Set the javatype. The javatype is the entity we encapsulated. Because it is encapsulated inside the entity, it can be defined with the $symbol
jdbcType, because there is no corresponding Json type in jdbcType. So we customize the object that receives Json type. Because it is java language, we define jdbcType as JAVA_OBJECT.

<result column="premarital" jdbcType="JAVA_OBJECT" property="premarital"
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"
javaType="com.esmooc.life.marriage.entity.Ppaal$PremaritalAndPremaritalInfo"/>

<result column="progestation" jdbcType="JAVA_OBJECT" property="progestation"
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"
javaType="com.esmooc.life.marriage.entity.Ppaal$PremaritalAndPremaritalInfo"/>

The content of this article is about the problems encountered in the work, and it is also the process of constantly fighting and upgrading. I hope you will communicate more and put forward valuable suggestions.

First of all, I would like to introduce myself. I graduated from Jiaotong University in 13 years. I once worked in a small company, went to large factories such as Huawei OPPO, and joined Alibaba in 18 years, until now. I know that most junior and intermediate Java engineers who want to improve their skills often need to explore and grow by themselves or sign up for classes, but there is a lot of pressure on training institutions to pay nearly 10000 yuan in tuition fees. The self-study efficiency of their own fragmentation is very low and long, and it is easy to encounter the ceiling technology to stop. Therefore, I collected a "full set of learning materials for java development" and gave it to you. The original intention is also very simple. I hope to help friends who want to learn by themselves and don't know where to start, and reduce everyone's burden at the same time. Add the business card below to get a full set of learning materials

Tags: Android Interview Back-end Front-end

Posted by CavemanUK on Sun, 07 Aug 2022 22:46:51 +0530