When using MyBatis-Plus for business development, we often need to automatically map the JSON fields in the database (such as arrays in strings) into JSONArray or List<String> types in Java.
By default, MyBatis-Plus does not support direct mapping of JSON types, so you need to use:
- @TableField(typeHandler = ...)
- Custom or existing TypeHandler
- Only when it works correctly can it work with @TableName(autoResultMap = true)!
Examples of real scenes
Suppose we now have a travel food table travel_cuisine, and the field tag_list inside is a JSON Array, which is used to store the tag ID list, and the sample data is as follows:
["tag-101", "tag-202", "tag-333"]
We want to use the following form to automatically map in Java entities:
@TableField(typeHandler = ) private JSONArray tagList;
Next, I will teach you to implement it step by step.
1. What is @TableField + typeHandler?
@TableField Introduction
@TableField is a field-level annotation provided by MyBatis-Plus, which is used to illustrate the mapping relationship between fields and databases.
The core parameters are as follows:
parameter | illustrate |
---|---|
value |
Corresponding database field name |
exist |
Whether the field exists in the database table structure |
typeHandler |
Field conversion processor for complex type mapping |
typeHandler function
TypeHandler is an important mechanism in MyBatis, which is responsible for the conversion between Java types and JDBC types.
You can use it to handle:
- JSON ↔ Java objects (such as JSONArray, Map, List)
- Comma-separated string ↔ List
- Enumeration ↔ Database storage values
Simply put, typeHandler is the bridge of data format!
2. @TableName(autoResultMap = true) must be enabled
By default, MyBatis-Plus field mapping does not use typeHandler unless you add:
@TableName(value = "travel_cuisine", autoResultMap = true)
What is autoResultMap?
This is telling MP: "Please generate a custom ResultMap, otherwise I don't support typeHandler and complex types conversions!"
If you forget to add this item, typeHandler will not take effect!
3. Practical code: Map the JSON field as JSONArray
1. Database table building statement (simplified version)
CREATE TABLE travel_cuisine ( id BIGINT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), tag_list TEXT -- JSON Array String );
2. Entity class configuration
@Data @TableName(value = "travel_cuisine", autoResultMap = true) public class TravelCuisineDO { private Long id; private String name; @TableField(typeHandler = ) private JSONArray tagList; }
3. Custom TypeHandler (based on FastJSON)
public class JsonArrayTypeHandler extends BaseTypeHandler<JSONArray> { @Override public void setNonNullParameter(PreparedStatement ps, int i, JSONArray parameter, JdbcType jdbcType) throws SQLException { (i, ()); } @Override public JSONArray getNullableResult(ResultSet rs, String columnName) throws SQLException { String result = (columnName); return (result); } @Override public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String result = (columnIndex); return (result); } @Override public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String result = (columnIndex); return (result); } }
Reminder: This JsonArrayTypeHandler uses FastJSON. If Jackson is needed, please replace the conversion logic.
4. FAQ Troubleshooting Guide
Problem phenomenon | Solution or suggestions |
---|---|
typeHandler is not effective | Check whether the entity class is enabled autoResultMap = true |
Report JSON parse error | Make sure the database fields are in real JSON format |
The field is null when stored | Confirm that the field is not transient and not ignored |
Want to use List<String> instead of JSONArray | Just write a ListStringTypeHandler |
Summary: Three things must be used together
Configuration Items | illustrate |
---|---|
@TableField(typeHandler = …) | Tag field converter |
@TableName(autoResultMap = true) | Tell MP to enable complex mapping |
Custom TypeHandler | Transfer JSON fields with Java types |
This is the article about MyBatis-Plus implementing elegant processing of JSON field mapping. For more related content related to MyBatis-Plus processing of JSON field mapping, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!