V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shayang888
V2EX  ›  Java

springboot 中 mapper 查询操作返回指定的字段时,总是报错

  •  
  •   shayang888 · 2023-03-11 23:29:40 +08:00 · 1351 次点击
    这是一个创建于 411 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我的实体类

    public record User(Integer id, String email, String password, String nickname, Integer is_active) {
    }
    

    这是 mapper 的查询语句

    @Select("select email,nickname from user where email = #{email} limit 1")
    User findUserByEmail(@Param("email") String email);
    

    现在情况是我如果查询的字段不用*或者写完所有的字段,都会报错

    org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'email' from result set.  Cause: java.sql.SQLDataException: Cannot determine value type from string 'XXX'
    ; Cannot determine value type from string 'XXX'
    

    我的数据库表结构的字段和里面的值都是正确的,所以不存在数据库的错误

    第 1 条附言  ·  2023-03-12 00:04:10 +08:00
    @Terminator0826
    public record User(Integer id, String email, String password, String nickname, Integer is_active) {
    public User(String email, String nickname, Integer is_active) {
    this(null, email, null, nickname, is_active);
    }
    }
    

    我现在添加了个这个构造器,但是返回的结果就是

    "data": {
    "id": null,
    "email": "XXX",
    "password": null,
    "nickname": "XXX",
    "is_active": 0
    }
    

    id 和 password 还是查出来了

    15 条回复    2023-03-13 09:27:32 +08:00
    Terminator0826
        1
    Terminator0826  
       2023-03-11 23:32:24 +08:00 via Android
    我猜应该是因为你写了全仓构造限制了他,因为你只差部分字段,试下把全仓改掉或者再定一个你要查询字段的构造函数
    Terminator0826
        2
    Terminator0826  
       2023-03-11 23:34:16 +08:00 via Android
    @Terminator0826 妈耶这错别字😅 差->查, 改掉-> 干掉
    oneisall8955
        3
    oneisall8955  
       2023-03-11 23:35:15 +08:00
    盲猜 record 原因,你再缕清楚 record 的特性
    shayang888
        4
    shayang888  
    OP
       2023-03-11 23:56:44 +08:00
    @oneisall8955 并不是 我去掉 record ,换回普通的 class 也是一样的结果
    shayang888
        5
    shayang888  
    OP
       2023-03-11 23:57:22 +08:00
    @oneisall8955 record 只是帮忙补全了 gettersetter 无参构造器已经类变成 final 而已
    shayang888
        6
    shayang888  
    OP
       2023-03-11 23:59:04 +08:00
    @Terminator0826 确实 如果设置了构造函数,可以正常查出来,但是对于我没查询的字段它还是会在数据里返回,只是值为 null 。关键是我压根就不想要那个字段在结果里
    shayang888
        7
    shayang888  
    OP
       2023-03-12 00:03:24 +08:00
    @Terminator0826
    public record User(Integer id, String email, String password, String nickname, Integer is_active) {
    public User(String email, String nickname, Integer is_active) {
    this(null, email, null, nickname, is_active);
    }
    }
    我现在添加了个这个构造器,但是返回的结果就是
    "data": {
    "id": null,
    "email": "XXX",
    "password": null,
    "nickname": "XXX",
    "is_active": 0
    }
    id 和 password 还是查出来了
    tairan2006
        8
    tairan2006  
       2023-03-12 00:14:38 +08:00 via Android
    你不想要多余的字段就把返回值改成其他 DTO ,或者用 Map 啊…
    shayang888
        9
    shayang888  
    OP
       2023-03-12 00:18:59 +08:00
    @tairan2006 确实可以这么整,就是觉得有点麻烦
    Terminator0826
        10
    Terminator0826  
       2023-03-12 00:37:55 +08:00 via Android
    @shayang888 大哥呀,按照规范,本来就不建议将表实体直接放回前端的。可以另外定一个 dto 映射返回,或者在查询时指明 SQL 结果封装到某个 dto 也行。当然非要用 user 返回但是想排除 null 还是有办法的,可以全局配置一下或者在实体类上用 spring 提供的注解过滤 null 字段返回,这个你可以查查资料
    Leviathann
        11
    Leviathann  
       2023-03-12 00:42:11 +08:00
    null 字段不序列化?那不是配 jackson 就行了?
    shayang888
        12
    shayang888  
    OP
       2023-03-12 00:49:32 +08:00
    @Terminator0826 谢谢老哥 搞定了
    zed1018
        13
    zed1018  
       2023-03-12 09:27:38 +08:00
    这个难道不是应该用 projections 吗,单独定义个 XXXXOnly 的 interface 用这个 interface 去查询

    https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
    xcccer
        14
    xcccer  
       2023-03-12 22:28:48 +08:00
    我猜你是想屏蔽掉 password 这个字段,如果你用的 jackson 框架你可以试试 @JsonIgnore ,或者就像楼上说的写个 DTO 。
    dif
        15
    dif  
       2023-03-13 09:27:32 +08:00
    @JsonIgnore 与 @Transient 应该都能满足
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:15 · PVG 05:15 · LAX 14:15 · JFK 17:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.