springboot~mybatis-plus更优雅的处理mysql8.0的json字段

打印 上一主题 下一主题

主题 1776|帖子 1776|积分 5330

MySQL 8.0 引入了许多新特性和改进,旨在增强性能、可用性和安全性。以下是一些主要的新特性:数据字典,窗口函数,公共表表达式 (CTE),JSON 改进,隐式列和生成列,字符集和排序规则,原生支持 GIS 功能,支持更强的 SSL/TLS 加密选项等。
下面文章主要介绍mysql字段为Json类型主动映射到java的实体类型的方法:
数据表结构
  1. CREATE TABLE user_actions (
  2.     action_id INT PRIMARY KEY,
  3.     user_id INT,
  4.     action_time DATETIME,
  5.     action_details JSON
  6. );
  7. -- 示例数据插入
  8. INSERT INTO user_actions (action_id, user_id, action_time, action_details) VALUES
  9. (1, 1, '2025-04-01 10:00:00', '{"action": "login", "duration": 120}'),
  10. (2, 1, '2025-04-02 11:00:00', '{"action": "view", "item_id": 101, "duration": 300}'),
  11. (3, 2, '2025-04-05 12:00:00', '{"action": "login", "duration": 150}'),
  12. (4, 3, '2025-04-08 13:00:00', '{"action": "purchase", "item_id": 102, "amount": 29.99}'),
  13. (5, 2, '2025-04-09 14:00:00', '{"action": "view", "item_id": 103, "duration": 200}'),
  14. (6, 3, '2025-04-09 15:00:00', '{"action": "view", "item_id": 101, "duration": 100}');
复制代码
实体结构
  1. @Data
  2. @TableName(value = "user_actions", autoResultMap = true)
  3. public class UserAction {
  4.         private Integer actionId;
  5.         private Integer userId;
  6.         private LocalDateTime actionTime;
  7.         @TableField(typeHandler = JsonTypeHandler.class)
  8.         private ActionDetail actionDetails;
  9. }
  10. @Data
  11. class ActionDetail {
  12.         private String action;
  13.         private Integer duration;
  14.         @JsonProperty("item_id")
  15.         private Integer itemId;
  16.         private double amount;
  17. }
复制代码
注意:实体类必要添加@TableName(autoResultMap = true)
字段上添加对应的注解@TableField(typeHandler = JsonTypeHandler.class)
类型转换器JsonTypeHandler代码
  1. @MappedTypes(Map.class)
  2. public class JsonTypeHandler<T> extends BaseTypeHandler<T> {
  3.         private static final ObjectMapper objectMapper = new ObjectMapper();
  4.         private Class<T> type;
  5.         public JsonTypeHandler(Class<T> type) {
  6.                 this.type = type;
  7.         }
  8.         @Override
  9.         public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
  10.                 try {
  11.                         ps.setString(i, objectMapper.writeValueAsString(parameter));
  12.                 }
  13.                 catch (IOException e) {
  14.                         throw new SQLException("Failed to convert JSON to String", e);
  15.                 }
  16.         }
  17.         @Override
  18.         public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
  19.                 String json = rs.getString(columnName);
  20.                 if (json != null) {
  21.                         try {
  22.                                 return objectMapper.readValue(json, type);
  23.                         }
  24.                         catch (IOException e) {
  25.                                 throw new SQLException("Failed to convert String to JSON", e);
  26.                         }
  27.                 }
  28.                 return null;
  29.         }
  30.         @Override
  31.         public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  32.                 String json = rs.getString(columnIndex);
  33.                 if (json != null) {
  34.                         try {
  35.                                 return objectMapper.readValue(json, type);
  36.                         }
  37.                         catch (IOException e) {
  38.                                 throw new SQLException("Failed to convert String to JSON", e);
  39.                         }
  40.                 }
  41.                 return null;
  42.         }
  43.         @Override
  44.         public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  45.                 String json = cs.getString(columnIndex);
  46.                 if (json != null) {
  47.                         try {
  48.                                 return objectMapper.readValue(json, type);
  49.                         }
  50.                         catch (IOException e) {
  51.                                 throw new SQLException("Failed to convert String to JSON", e);
  52.                         }
  53.                 }
  54.                 return null;
  55.         }
  56. }
复制代码
最后,可正常剖析出效果



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

祗疼妳一个

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表