Mybatis 学习笔记 - 自定义类型解析器
1. 实现
2. 继承
发布日期:2021-06-30 14:54:20
浏览次数:3
分类:技术文章
本文共 3402 字,大约阅读时间需要 11 分钟。
Mybatis 学习笔记 - 自定义类型解析器
当对象字段为自定义类型时,mybatis需要我们自己定义解析器才能很好的工作。比如com.alibaba.fastjson.JSONObject
大体流程:
- 定义解析器
- 注册解析器
- OK
定义解析器
两个方案:
1. 实现 org.apache.ibatis.type.TypeHandler
接口
传入参数时: java类型 >> JDBC类型
获取结果时: java类型 << JDBC类型@MappedTypes(JSONObject.class)@MappedJdbcTypes(JdbcType.VARCHAR)public class JSONObjectHandler implements TypeHandler{ private JSONObject getResult(String jsonString) throws SQLException { if(jsonString != null){ JSONObject jsonObject; try{ jsonObject = JSONObject.parseObject(jsonString); }catch (JSONException ex){ throw new SQLException("jsonString >> JSON 失败,jsonString:" + jsonString); } return jsonObject; } return null; } /** * 设置参数时 java类型 转 JDBC类型 */ @Override public void setParameter(PreparedStatement preparedStatement, int parameterIndex, JSONObject jsonObject, JdbcType jdbcType) throws SQLException { preparedStatement.setString(parameterIndex, jsonObject.toJSONString()); } /** * 从结果集按列名取对象时 JdbcType.VARCHAR >> Java类型 */ @Override public JSONObject getResult(ResultSet resultSet, String columnLabel) throws SQLException { return getResult(resultSet.getString(columnLabel)); } /** * 从结果集按索引取对象时 JdbcType.VARCHAR >> Java类型 */ @Override public JSONObject getResult(ResultSet resultSet, int columnIndex) throws SQLException { return getResult(resultSet.getString(columnIndex)); } /** * 针对存储过程转换结果 */ @Override public JSONObject getResult(CallableStatement callableStatement, int parameterIndex) throws SQLException { return getResult(callableStatement.getString(parameterIndex)); }}
2. 继承 org.apache.ibatis.type.BaseTypeHandler
@MappedTypes(JSONArray.class)@MappedJdbcTypes(JdbcType.VARCHAR)public class JSONArrayHandler extends BaseTypeHandler{ public JSONArray getResult(String jsonString) throws SQLException { if(jsonString != null){ JSONArray jsonArray; try{ jsonArray = JSONArray.parseArray(jsonString); }catch (JSONException ex){ throw new SQLException("jsonString >> JSONArray 失败,jsonString:" + jsonString); } return jsonArray; } return null; } @Override public void setNonNullParameter(PreparedStatement preparedStatement, int parameterIndex, JSONArray jsonArray, JdbcType jdbcType) throws SQLException { preparedStatement.setString(parameterIndex, jsonArray.toJSONString()); } @Override public JSONArray getNullableResult(ResultSet resultSet, String columnLabel) throws SQLException { return getResult(resultSet.getString(columnLabel)); } @Override public JSONArray getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException { return getResult(resultSet.getString(columnIndex)); } @Override public JSONArray getNullableResult(CallableStatement callableStatement, int parameterIndex) throws SQLException { return getResult(callableStatement.getString(parameterIndex)); }}
注册解析器
也有两个方式
1. 全局配置文件里
src/main/resources/mybatis/mybatis-config.xml
... ...
2. PoemMapper.xml 文件里
...
参考资料
转载地址:https://jerryjin.blog.csdn.net/article/details/116093718 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月07日 05时07分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MyBatisPlus简单入门(SpringBoot)
2019-04-30
攻防世界web进阶PHP2详解
2019-04-30
攻防世界web进阶区web2详解
2019-04-30
xss-labs详解(上)1-10
2019-04-30
xss-labs详解(下)11-20
2019-04-30
攻防世界web进阶区ics-05详解
2019-04-30
攻防世界web进阶区ics-04详解
2019-04-30
sql注入总结学习
2019-04-30
Python 之 histogram直方图
2019-04-30
Python实现决策树 Desision Tree & 可视化
2019-04-30
决策树 Decision tree
2019-04-30
nominal和ordinal & 数据处理中四种基本数据类型
2019-04-30
Trie树(字典树)
2019-04-30
COMP7404 Machine Learing——ROC
2019-04-30
MATLAB与CUDA
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
vscode git
2019-04-30