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

mybatis-plus version : 3.5.1 mysql : 8.0.30 springboot : 2.6.11

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

我不确定是否是我的使用方法有误,还望指正。

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

执行测试代码如下: MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

数据库内数据如下: MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

@Data
@NoArgsConstructor
@TableName(value="tags",autoResultMap = true)
public class Tag implements Serializable {

    /**
     * 标签ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 标签名字
     */
    private String name;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    /**
     * 文章ID列表
     */
    @TableField(typeHandler = ObjectToJson.class)
    private List<Note> noteIdList;

    /**
     * 是否逻辑删除
     */
    @TableLogic
    private Integer isDelete;

    @TableField(exist = false)
    private static final long serialVersionUID =51235L;

    public Tag(String name){
        this.name=name;
    }
}

测试执行结果如下: MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

报错信息

如果SelectOne正常查找到的数据的话会报错,毕竟数据库中有多条数据。

Comment From: miemieYaho

你连错库了吧,执行sql和结果封装都是mybatis的事

Comment From: RedCrazyGhost

你好,我用是本地docker的mysql容器

Docker version 20.10.20, build 9fdeb9c

MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

下方为数据相关配置:

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      url: jdbc:mysql://localhost:3306/website?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
      validation-query: SELECT 1

Comment From: RedCrazyGhost

你好,我这边使用idea连接数据库用相同的SQL语句是可以获取到数据的

结果如下图: MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

Comment From: miemieYaho

那你写个git形式的复现demo发出来看看

Comment From: RedCrazyGhost

你好,我尝试依照mybatis-plus的快速开始,来复现此问题,但没有成功,出现了问题下方的问题。麻烦大佬有时间看一下。

java.lang.IllegalStateException: Failed to load ApplicationContext

项目地址:https://github.com/RedCrazyGhost/QueryWarpperDemo

Comment From: miemieYaho

@SpringBootTest(classes = QueryWarpperTest.class)改成@SpringBootTest

Comment From: RedCrazyGhost

你好,我这边没有能复现出问题,QueryWarpper功能按照预想的正常执行了。

详细情况请看项目。

项目地址:https://github.com/RedCrazyGhost/QueryWarpperDemo

Comment From: miemieYaho

既然无法复现那就不是mp的问题

Comment From: RedCrazyGhost

你好,我拿两个项目进行DEBUG发现var6获取的值有差异。

MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

两者的参数的区别只存在resultMaps

MyBatis-Plus QueryWrapper条件查询SelectCount为0,SelectOne为NULL,SelectList为[]

Comment From: miemieYaho

无法复现说什么都没意义

Comment From: RedCrazyGhost

你好,这个问题我已经找到了,我自己写的@TableField(typeHandler = ObjectToJson.class)导致输出结果为空值。

ObjectToJson.class主要是想做通用解析的。

@Component
public class ObjectToJson extends BaseTypeHandler<Object> {
    @Resource
    private ObjectMapper objectMapper;

    private <T> String ObjectToJson(T o){
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new ControllerException(RCode.SYSTEM_ERROR,e.getMessage());
        }
    }

    private <T> T jsonToObject(String json, TypeReference<T> typeReference){
        if (json==null)return null;
        try {
            return objectMapper.readValue(json,typeReference);
        }catch (JsonProcessingException e) {
            throw new ControllerException(RCode.SYSTEM_ERROR,e.getMessage());
        }
    }



    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        String json=ObjectToJson(o);
        preparedStatement.setString(i,json);
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String str=resultSet.getString(s);
        return jsonToObject(str, new TypeReference<>() {
        });
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String str=resultSet.getString(i);
        return jsonToObject(str, new TypeReference<>() {
        });
    }

    @Override
    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String str=callableStatement.getString(i);
        return jsonToObject(str, new TypeReference<>() {
        });
    }
}

🙏谢谢你的支持! @miemieYaho