baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.3k stars 4.3k forks source link

通用枚举 带属性枚举和不带属性枚举 无法共存,导致查询出现SQLFeatureNotSupportedException问题 #5768

Closed cctyl closed 2 months ago

cctyl commented 10 months ago

当前使用版本(必填,否则不予处理)

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
</dependency>

使用的是hikari ,未使用德鲁伊连接,springboot 2.7, sqlite 3

该问题是如何引起的?(确定最新版也有问题再提!!!)

由于版本我无法更改,所以没有尝试最新版本是否可行。 情况是这样的,系统中存在两种类型的枚举:

1.带成员的枚举:

public enum  AccessType  {
    /**
     * 黑名单
     */
    BLACK(1),
    /**
     * 白名单
     */
    WHITE(2),
    ;

    @EnumValue
    private final int code;

    AccessType(int code) {
        this.code = code;
    }

}

2.无成员枚举

public enum  HandleType  {

    THUMB_UP,

    DISLIKE,

    OTHER;

}

对于第一种情况,我以根据通用枚举的配置方式,在成员上添加了 @EnumValue,希望它存储的是code。 对于情况2,我希望它存储枚举本身的字符串。

事实上,对于存储来说,一切正常。都可以正常存储,并且存储的类型也和期待的一致。

但是,查询时出现了:

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2527082e] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1958409130 wrapping org.sqlite.jdbc4.JDBC4Connection@22bd8ce7] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) AS total FROM dict WHERE is_deleted = 0
==> Parameters: 
<==    Columns: total
<==        Row: 1
<==      Total: 1
==>  Preparing: SELECT id,access_type,dict_type,value,outer_id,created_date,is_deleted,version,last_modified_date FROM dict WHERE is_deleted=0 LIMIT ?
==> Parameters: 100(Long)
<==    Columns: id, access_type, dict_type, value, outer_id, created_date, is_deleted, version, last_modified_date
<==        Row: 1722473414721048577, WHITE, TAG, ssssssss, , 2023-11-09T12:37:18.740604900, 0, 1, 2023-11-09T12:37:18.742105800
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2527082e]
2023-11-09 13:45:51.118 ERROR 2172 --- [nio-9000-exec-1] i.g.cctyl.controller.DictController      : Exception in getList() with cause = 'java.sql.SQLFeatureNotSupportedException' and exception = 'Error attempting to get column 'access_type' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
; null; nested exception is java.sql.SQLFeatureNotSupportedException'

org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'access_type' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
; null; nested exception is java.sql.SQLFeatureNotSupportedException
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:96)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:79)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
    at com.sun.proxy.$Proxy100.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:121)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:85)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
    at com.sun.proxy.$Proxy105.selectPage(Unknown Source)

为了验证是无法共存的问题,我把AccessType这个枚举也变成没有成员的情况:

public enum  AccessType  {
    /**
     * 黑名单
     */
    BLACK,
    /**
     * 白名单
     */
    WHITE,
    ;

}

此时,再尝试存储,查询,一切正常。

此外,根据文档 通用枚举 ,提示3.5.2 开始,无需配置“配置扫描通用枚举”,于是没有配置。

这时候出现的就是上述报错情况。

而配置typeEnumsPackage:,没有变化。

而DefaultEnumTypeHandler,此时情况2由于没有@EnumValue或者实现IEnum 接口,会报另一个错,提示该枚举找不到@EnumValue.

尝试了一圈下来,没有发现可以共存的情况。

重现步骤(如果有就写完整)

编写两种情况的枚举类,在实体类中声明,然后赋值存储。再查询,即出现

报错信息

如上所示

nieqiurong commented 4 months ago

提供复现工程

babalash0728 commented 2 months ago

我和你碰到了同样的问题,我们使用的环境也基本一样,没有使用druid。mybatisplus也是3.5.2。不过我的springboot版本是2.7.5。 当我把mybatisplus版本切换至3.5.1就没有这个问题了。高版本没试过。希望对你有帮助