springboot~mybatis-plus更優雅的處(chu)理mysql8.0的json字段
MySQL 8.0 引(yin)入了許多(duo)新(xin)特(te)性(xing)(xing)和改進(jin),旨在(zai)增(zeng)強(qiang)性(xing)(xing)能(neng)、可(ke)用性(xing)(xing)和安(an)全性(xing)(xing)。以(yi)下(xia)是一些主要的新(xin)特(te)性(xing)(xing):數(shu)據字(zi)典(dian),窗口函(han)數(shu),公共(gong)表表達式 (CTE),JSON 改進(jin),隱式列和生(sheng)成列,字(zi)符集和排序規則,原生(sheng)支持 GIS 功(gong)能(neng),支持更強(qiang)的 SSL/TLS 加密選項等(deng)。
下面文章(zhang)主要介紹(shao)mysql字(zi)段(duan)為Json類型自動映射(she)到java的實體類型的方法:
數據表結構
CREATE TABLE user_actions (
action_id INT PRIMARY KEY,
user_id INT,
action_time DATETIME,
action_details JSON
);
-- 示例數據插入
INSERT INTO user_actions (action_id, user_id, action_time, action_details) VALUES
(1, 1, '2025-04-01 10:00:00', '{"action": "login", "duration": 120}'),
(2, 1, '2025-04-02 11:00:00', '{"action": "view", "item_id": 101, "duration": 300}'),
(3, 2, '2025-04-05 12:00:00', '{"action": "login", "duration": 150}'),
(4, 3, '2025-04-08 13:00:00', '{"action": "purchase", "item_id": 102, "amount": 29.99}'),
(5, 2, '2025-04-09 14:00:00', '{"action": "view", "item_id": 103, "duration": 200}'),
(6, 3, '2025-04-09 15:00:00', '{"action": "view", "item_id": 101, "duration": 100}');
實體結構
@Data
@TableName(value = "user_actions", autoResultMap = true)
public class UserAction {
private Integer actionId;
private Integer userId;
private LocalDateTime actionTime;
@TableField(typeHandler = JsonTypeHandler.class)
private ActionDetail actionDetails;
}
@Data
class ActionDetail {
private String action;
private Integer duration;
@JsonProperty("item_id")
private Integer itemId;
private double amount;
}
注意:實體類需要添加@TableName(autoResultMap = true)
字段上添加(jia)對(dui)應的注(zhu)解@TableField(typeHandler = JsonTypeHandler.class)
類型轉換器JsonTypeHandler代碼
@MappedTypes(Map.class)
public class JsonTypeHandler<T> extends BaseTypeHandler<T> {
private static final ObjectMapper objectMapper = new ObjectMapper();
private Class<T> type;
public JsonTypeHandler(Class<T> type) {
this.type = type;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
try {
ps.setString(i, objectMapper.writeValueAsString(parameter));
}
catch (IOException e) {
throw new SQLException("Failed to convert JSON to String", e);
}
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
String json = rs.getString(columnName);
if (json != null) {
try {
return objectMapper.readValue(json, type);
}
catch (IOException e) {
throw new SQLException("Failed to convert String to JSON", e);
}
}
return null;
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String json = rs.getString(columnIndex);
if (json != null) {
try {
return objectMapper.readValue(json, type);
}
catch (IOException e) {
throw new SQLException("Failed to convert String to JSON", e);
}
}
return null;
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String json = cs.getString(columnIndex);
if (json != null) {
try {
return objectMapper.readValue(json, type);
}
catch (IOException e) {
throw new SQLException("Failed to convert String to JSON", e);
}
}
return null;
}
}
最后,可正常解析出結果
