<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 值)
1
gomorebug OP <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 ) |
2
gomorebug OP 主题中的查询语句
<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> |
3
gomorebug OP @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> |