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

Mybatis 中的 xml 配置问题

  •  
  •   gomorebug · 2023-11-22 15:53:59 +08:00 · 736 次点击
    这是一个创建于 368 天前的主题,其中的信息可能已经有所发展或是发生改变。
        <resultMap id="historyResultMap" type="org.hugcode.common.pojo.User">
            <id column="uid" property="uid"/>
            <result column="username" property="username"/>
            <result column="password" property="password"/>
            <result column="updated" property="updated"/>
            <result column="created" property="created"/>
    
            <collection property="userHistories" ofType="org.hugcode.common.pojo.UserHistory">
                <result column="correct" property="correct"/>
                <result column="reply" property="reply"/>
                <result column="created" property="created"/>
                <result column="updated" property="updated"/>
                <association property="question" javaType="org.hugcode.common.pojo.Question">
                    <!--   这一块出错了-->
                    <id column="qid" property="qid"/>
                    <result column="description" property="description"/>
                    <result column="answer" property="answer"/>
                    <result column="type" property="type"/>
                </association>
            </collection>
        </resultMap>
    

    万能的 v 友们,很奇怪 idea 报了错 property (qid) can not found setter method ,但实际能正常运行,但结果存在问题:sql 语句查询后有八行,但 mybatis 返回的只有五行(数据库中并未存在 null 值)

    gomorebug
        1
    gomorebug  
    OP
       2023-11-22 16:12:27 +08:00
    <resultMap id="questionResultMap" type="org.hugcode.common.pojo.User">
    <id column="uid" property="uid"/>
    <result column="username" property="username"/>
    <result column="password" property="password"/>
    <result column="updated" property="updated"/>
    <result column="created" property="created"/>
    <collection property="questions" ofType="org.hugcode.common.pojo.Question">
    <id column="qid" property="qid"/>
    <result column="description" property="description"/>
    <result column="answer" property="answer"/>
    <result column="type" property="type"/>
    </collection>

    </resultMap>
    <select id="findQuestionsByUid" resultMap="questionResultMap">
    SELECT question.*,user.* FROM question,user,user_history WHERE user.uid = #{uid} AND user_history.uid = user.uid AND user_history.qid = question.qid
    </select>


    单纯在 User 类中一对多是没有问题的,但我想在关系表 user_history 保存用户的做题记录所以想新弄个实体类 UserHistory 存其他信息,结果就出现问题了😭,下面是包含关系
    ( 1 )正确:User ->List<Question>( collection )
    ( 2 )错误:User ->List<UserHistory> ( collection )->Question ( association )
    gomorebug
        2
    gomorebug  
    OP
       2023-11-22 16:30:22 +08:00
    主题中的查询语句
    <select id="findQuestionsByUid" resultMap="questionResultMap">
    SELECT question.*, user.*, user_history.reply,user_history.h_updated,user_history.h_created,user_history.correct
    FROM question,
    user,
    user_history
    WHERE user.uid = #{uid}
    AND user_history.uid = user.uid
    AND user_history.qid = question.qid
    </select>
    gomorebug
        3
    gomorebug  
    OP
       2023-11-22 16:34:32 +08:00
    @gomorebug id 搞错了,是这个
    <select id="findHistoryByUid" resultMap="historyResultMap">
    SELECT question.*, user.*, user_history.reply,user_history.h_updated,user_history.h_created,user_history.correct
    FROM question,
    user,
    user_history
    WHERE user.uid = #{uid}
    AND user_history.uid = user.uid
    AND user_history.qid = question.qid
    </select>
    gomorebug
        4
    gomorebug  
    OP
       2023-11-22 16:48:36 +08:00
    @gomorebug 解决了,在 UserHistory 添加成员变量 qid 和 uid ,在映射中也添加
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2695 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:51 · PVG 18:51 · LAX 02:51 · JFK 05:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.