Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mybatis plus TableField typehandler 问题 #357

Closed
xgj1988 opened this issue Jun 13, 2018 · 24 comments
Closed

mybatis plus TableField typehandler 问题 #357

xgj1988 opened this issue Jun 13, 2018 · 24 comments

Comments

@xgj1988
Copy link

xgj1988 commented Jun 13, 2018

# 问题描述:
查询的时候,如果用mybatis mapper 配置返回的查询,typehandler会起作用,但是用ServiceImpl(mybatis plus 的类)的selecrtById(其实任何查询方法都可以),那么TableField 配置的TypeHandler就不起作用,看如下代码。
# 代码:
JAVA代码判断
@TableField(el = "images, typeHandler=com.zyzc.xyzy.config.db.JsonTypeHandler")
private List images;

Mybatis XML代码片段

# 原因分析
我跟踪代码的时候发现了这一点。就是在在用ServiceImpl的selectById的时候。
执行到mybatis的DefaultResultSetHandler的 typeHandlerRegistry.hasTypeHandler(propertyType, rsw.getJdbcType(columnName)) 这个方法的时候,返回的是false。这个方法里面又去比较的是 TypeHandlerRegistry的 TYPE_HANDLER_MAP 这个集合比较的。而且是用的java.util.list这个类去找的。确实没找到。。但是为什么 mybatis 那种可以。。因为时间关系,我就没往下看了。。

# 结论
我认为我这个问题可能要么就是我配置错了(TableFiled上面),要么就是MybatisPlus的一个BUG。

@xgj1988
Copy link
Author

xgj1988 commented Jun 13, 2018

# 补充一下
奇怪的是插入的时候typehandler 又是生效的,,就是 查询的时候 无效。

@qmdx
Copy link
Member

qmdx commented Jun 19, 2018

查询的 sql 你打印下看看,我们 源码测试用例里面有一个 typeHandler 的例子你可以参考下

@zhiqizhu
Copy link

zhiqizhu commented Jul 26, 2018

遇到同样的问题,打印的SQL没有问题,我认为是MybatisPlus的BUG

@xiao0yy
Copy link

xiao0yy commented Jul 27, 2018

在@TableField中指定typeHandler,所有的BaseMapper的查询相关方法都不会生效.但又无法在BaseMapper指定ResultMap.求帮助.

@spencexu
Copy link

遇到同样的问题,插入更新没问题,查询的时候使用了默认的handler

@miemieYaho
Copy link
Member

写 resultMap,TableName注解指定该 resultMap

@ynfatal
Copy link

ynfatal commented Sep 11, 2018

@spencexu 那最后你怎么解决这个问题,能否指点一下,谢谢

@spencexu
Copy link

@ynfatal TableField和resultMap都配置了typeHandler,一个处理查询一个处理更新,测试了一下,确实是可以的

@vnobo
Copy link

vnobo commented Jul 2, 2019

这个问题,还没有修复,最新版 3.1.2

@haochencheng
Copy link

see
https://mp.baomidou.com/config/#typehandlerspackage

在配置文件中添加配置
mybatis-plus:
typeHandlersPackage:

@givedrug
Copy link

givedrug commented Nov 27, 2019

@TableName(value = "project",autoResultMap = true)
加上autoResultMap可以了!

typeHandlersPackage倒是无所谓加不加

@664623107
Copy link

@miemieYaho 如果是自定义SQL的话,这个问题仍然存在,加上@TableName(autoResultMap = true)也没用,只能自己加ResultMap

@miemieYaho
Copy link
Member

本来就这样的

@664623107
Copy link

@miemieYaho OK,知道不能那么用就放心了,但不是很理解为什么不能……

@miemieYaho
Copy link
Member

因为芯是mybatis

@mathcoder23
Copy link

在MyBatis DefaultSetResult 这个处理类的typehandler管理中,没有发现@TableField中的typehandler被注入。因此我选择手动遍历MP的@TableField注解,并且自行注入

这里说明一下
1,ClassUtil是包扫描工具,基于hutool,自行依赖
2,先扫描了注解@TableName进行定位,然后在扫描@TableField,所以类需要有注解。

private void initMybatisTypeHandler(ApplicationReadyEvent event){
        SqlSessionFactory sqlSessionFactory = event.getApplicationContext().getBean(SqlSessionFactory.class);
        ClassUtil.scanPackageByAnnotation("com.xxx.xxx", TableName.class).forEach(clazz->{
            for(Field filed : clazz.getDeclaredFields()){
                TableField tableName = filed.getAnnotation(TableField.class);
                if(null != tableName && !tableName.typeHandler().equals(UnknownTypeHandler.class)){
                    sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(filed.getType(), tableName.typeHandler());
                }
            }
        });
    }

@yangzhentai
Copy link

see
https://mp.baomidou.com/config/#typehandlerspackage

在配置文件中添加配置
mybatis-plus:
typeHandlersPackage:

感谢,通过修改这个配置文件确实可以

@cainli
Copy link

cainli commented Oct 10, 2020

按理配置 mybatis-plus.typeHandlersPackage=xxx, 会注入MybatisPlusProperties 然后会在MybatisPlusAutoConfiguration中设置MybatisSqlSessionFactoryBean 中的typeHandlersPackage,但是调试的时候("com.baomidou:mybatis-plus-boot-starter:2.3.1"),这个配置是不起作用的, MybatisSqlSessionFactoryBean中使用typeHandlersPackage的时机早于MybatisPlusProperties的初始化,所以可以在MybatisSqlSessionFactoryBean配置中直接注入typeHandlersPackage

@rogue2yjg
Copy link

按理配置 mybatis-plus.typeHandlersPackage=xxx, 会注入MybatisPlusProperties 然后会在MybatisPlusAutoConfiguration中设置MybatisSqlSessionFactoryBean 中的typeHandlersPackage,但是调试的时候("com.baomidou:mybatis-plus-boot-starter:2.3.1"),这个配置是不起作用的, MybatisSqlSessionFactoryBean中使用typeHandlersPackage的时机早于MybatisPlusProperties的初始化,所以可以在MybatisSqlSessionFactoryBean配置中直接注入typeHandlersPackage

如何在MybatisSqlSessionFactoryBean配置中直接注入typeHandlersPackage?

@gitMii
Copy link

gitMii commented Mar 5, 2021

在MyBatis DefaultSetResult这个处理类的typehandler管理中,没有发现@TableField中的typehandler被注入。因此我选择手动遍历MP的@TableField注解,并且自行注入

这里说明一下
1,ClassUtil是包扫描工具,基于hutool,自行依赖
2,先扫描了注解@TableName进行定位,然后在扫描@TableField,所以类需要有注解。

private void initMybatisTypeHandler(ApplicationReadyEvent event){
        SqlSessionFactory sqlSessionFactory = event.getApplicationContext().getBean(SqlSessionFactory.class);
        ClassUtil.scanPackageByAnnotation("com.xxx.xxx", TableName.class).forEach(clazz->{
            for(Field filed : clazz.getDeclaredFields()){
                TableField tableName = filed.getAnnotation(TableField.class);
                if(null != tableName && !tableName.typeHandler().equals(UnknownTypeHandler.class)){
                    sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(filed.getType(), tableName.typeHandler());
                }
            }
        });
    }

请问这个方法什么时候执行呢,没有找到

@1063666434
Copy link

能不能问一下,这个baseTypeHandler,我在使用mybatisPlus的Example的时候不生效,这个有人遇到过咋解决的吗?

@gooym
Copy link

gooym commented Sep 23, 2021

目前框架层面貌似没解决,我这里的解决方案是继承LambdaQueryWrapper然后重写自己需要的查询比如eq,in,like,然后针对要处理的字段进行处理,处理之后作为入参执行sql

@342523895
Copy link

342523895 commented Sep 8, 2022

新版本中,支持typeHandler属性的如: @TableField(typeHandler = MyArrayTypeHandler.class),可按官方文档配置即可
老版本中,不支持typeHandler属性的,有下面两个步骤
查询时,需要在xml文件中的resultMap中,指定该字段handler,如
typeHandler="cn.com.mgcc.kol.mybatis.MyArrayTypeHandler"
插入时,需要在实体类上用el表达式指定handler,如
@TableField(el = "words, typeHandler=cn.com.mgcc.kol.mybatis.MyArrayTypeHandler")

@forayl
Copy link

forayl commented Jun 9, 2023

@TableName(value = "project",autoResultMap = true) 加上autoResultMap可以了!

typeHandlersPackage倒是无所谓加不加

感谢,通过增加这个注解确实可以

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests