MyBatis-Plus:自定义TypeHandler实现JSON数组转List<Integer>及注意事项
使用MyBatis-Plus处理JSON数组转为List<Integer>,可以通过自定义TypeHandler实现。 1. 创建自定义TypeHandler: ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonIntegerListTypeHandler extends BaseTypeHandler<List<Integer>> { private static final ObjectMapper objectMapper = new ObjectMapper(); @Override public void setNonNullParameter(PreparedStatement ps, int i, List<Integer> parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, toJson(parameter)); } @Override public List<Integer> getNullableResult(ResultSet rs, String columnName) throws SQLException { return parseJson(rs.getString(columnName)); } @Override public List<Integer> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return parseJson(rs.getString(columnIndex)); } @Override public List<Integer> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return parseJson(cs.getString(columnIndex)); } private String toJson(List<Integer> list) { try { return objectMapper.writeValueAsString(list); } catch (Exception e) { throw new RuntimeException("Failed to convert list to JSON", e); } } private List<Integer> parseJson(String json) { if (json == null || json.trim().isEmpty()) { return null; } try { return objectMapper.readValue(json, new TypeReference<List<Integer>>() {}); } catch (Exception e) { throw new RuntimeException("Failed to parse JSON to list", e); } } } ``` 2. 在实体类字段上使用TypeHandler: ```java import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import java.util.List; @TableName("your_table") public class YourEntity { @TableField(value = "json_array_column", typeHandler = JsonIntegerListTypeHandler.class) private List<Integer> integerList; // getter和setter方法 public List<Integer> getIntegerList() { return integerList; } public void setIntegerList(List<Integer> integerList) { this.integerList = integerList; } } ``` 3. 确保数据库字段类型为能够存储JSON字符串的类型(如VARCHAR或TEXT)。 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: - JSON字符串格式必须正确,否则解析会失败导致异常。 - 数据库字段长度需足够存储序列化后的JSON字符串,避免数据截断。 - 使用Jackson库时确保版本兼容性,避免因版本问题导致序列化或反序列化错误。 ######[AI写代码神器 | 656点数解答 | 2025-08-26 11:26:36]
- @biuem2.com假人系统私人申明:红尘脚本定制版本关键参数揭秘(字节豆包 | 37点数解答 | 2024-07-30 18:26:07)316
- Java 代码示例:老板让组长统计在线课程数量及代码优化(字节豆包 | 182点数解答 | 2024-10-10 16:23:27)328
- Java代码修正:老板让统计在线课程数量的正确实现(阿里通义 | 400点数解答 | 2024-10-10 16:28:11)265
- HTML 任务列表代码:语法纠错、优化建议及额外逻辑要点(字节豆包 | 185点数解答 | 2025-01-01 17:54:37)245
- “评荐”技能代码深度解析与游戏技能系统开发指南(DeepSeek | 1700点数解答 | 2026-03-13 10:36:38)8
- C++ 代码实现:查找链表最大节点并移至末尾(字节豆包 | 549点数解答 | 2024-10-14 22:55:13)313
- Python实现深度优先爬虫:精准爬取百度域名下链接,模拟输出详细信息(GPT | 2142点数解答 | 2025-05-13 15:54:49)289
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)345
- 动态规划求解数组下标元素和最大值代码分析与优化(字节豆包 | 346点数解答 | 2026-03-16 17:56:30)17
- 巧用 MyBatis Plus 自定义拦截器实现数据权限控制(讯飞星火 | 567点数解答 | 2024-05-29 22:59:21)258
- MyBatis-Plus 纯代码实现联表查询:附详细示例代码(字节豆包 | 284点数解答 | 2024-10-17 11:46:14)211
- MyBatis-Plus 轻松实现:获取表中特定字段最大值全流程指南(阿里通义 | 527点数解答 | 2024-10-17 14:52:50)374