找不对自定义typeHandler:java.lang.IllegalStateException: No typehandler found for mapping ...

1、自定义typeHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.apollo.cloud.common.core.mybatis.typehandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
 
@MappedTypes({JSONObject.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class JsonTypeHandler extends BaseTypeHandler<JSONObject> {
 
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter,JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSONUtil.toJsonStr(parameter));
    }
 
    @Override
    public JSONObject getNullableResult(ResultSet rs, String columnName)throws SQLException {
        return JSONUtil.parseObj(rs.getString(columnName)).toBean(JSONObject.class);
    }
 
    @Override
    public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return JSONUtil.parseObj(rs.getString(columnIndex)).toBean(JSONObject.class);
    }
 
    @Override
    public JSONObject getNullableResult(CallableStatement cs, int columnIndex)throws SQLException {
        return JSONUtil.parseObj(cs.getString(columnIndex)).toBean(JSONObject.class);
    }
}

2、配置包扫描

1
2
mybatis-plus:
  type-handlers-package: com.apollo.cloud.common.core.mybatis.typehandler

3、实体使用

1
2
3
4
5
6
7
8
9
10
import cn.hutool.json.JSONObject;
 
@Data
@TableName(value = "order")
@EqualsAndHashCode(callSuper = true)
@ApiModel
public class OrderInfo {
    @TableField(typeHandler = JsonTypeHandler.class, jdbcType= JdbcType.VARCHAR)
    private JSONObject payResponse;
}

4、问题原因:实体使用的类型和typeHandler中定义的不一致,类型修改一致问题解决
  实体使用:             

 

   typeHandler定义:

 

posted on   滚动的蛋  阅读(310)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-03-18 开发基本问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示