Mybatis 中的 xml 配置问题

2023-11-22 15:53:59 +08:00
 gomorebug
    <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 值)

755 次点击
所在节点    Java
4 条回复
gomorebug
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
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
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
2023-11-22 16:48:36 +08:00
@gomorebug 解决了,在 UserHistory 添加成员变量 qid 和 uid ,在映射中也添加

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/994206

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX