Mybatis 学习笔记 - 自定义类型解析器
发布日期:2021-06-30 14:54:20 浏览次数:3 分类:技术文章

本文共 3402 字,大约阅读时间需要 11 分钟。

Mybatis 学习笔记 - 自定义类型解析器

当对象字段为自定义类型时,mybatis需要我们自己定义解析器才能很好的工作。比如com.alibaba.fastjson.JSONObject

大体流程:

  1. 定义解析器
  2. 注册解析器
  3. 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Maven 学习笔记 - 跳过测试 skipTests
下一篇:Maven 学习笔记 多环境配置

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月07日 05时07分30秒