码迷,mamicode.com
首页 > 其他好文 > 详细

<resultMap>中 <collection>的使用

时间:2017-09-02 18:01:13      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:nbsp   查询语句   man   entity   color   imp   问题   删除   内容   

public class Question implements Serializable {
   
    private int id;                 //问题Id
    private int accountId;     //用户id
    private String content;    //问题
    private Date createTime;
    private String createBy;
    private Date updateTime;
    private String updateBy;
    private int status;        //问题状态  0:逻辑删除,1:未回答,2:已回答
   private List<Answer> answers;  //注意这个属性
}        
public class Answer implements Serializable{
    
    private int id;    //答案Id
    private int questionId;    //问题Id 
    private int managerId;   //回复者Id(默认管理员)
    private String content;   //回复内容
    private Date createTime; //回复时间
    private String createBy;
    private Date updateTime;
    private String updateBy;
    private int status;

}    

 

mybatis中的查询语句

<select id="showAnswers" resultMap="linkedResultMap"  parameterType="int">
        SELECT 
            a.id,a.`questionId`,a.`content` AS answer,a.`createTime` as aCreateTime,a.`managerId`,
            q.id,q.`accountId`,q.`content` AS question,q.`status`,q.`createTime` as qCreateTime
        FROM 
            answers AS a 
        LEFT JOIN     
            questions AS q 
        ON     
            q.`id` = a.`questionId` 
        WHERE 
            q.`status` = 2 
        AND 
            q.`accountId`=#{acconutId} ;
    </select>
    
    <resultMap type="entity.Question" id="linkedResultMap" >  <!-- 注意ID的对应 -->
        <id property="id" column="id"/>
        <result property="accountId" column="accountId"/>
        <result property="content" column="question"/>
        <result property="status" column="status"/>
        <result property="createTime" column="qCreateTime"/>
        <collection property="answers" ofType="entity.Answer">
        <!-- property指 Answer类中的属性, column指数据库中的字段名-->
            <id property="id" column="id"/>
            <result property="questionId" column="questionId"/>
            <result property="managerId" column="managerId"/> 
            <result property="content" column="answer"/>
            <result property="createTime" column="aCreateTime"/> 
        </collection>
    </resultMap>

正确返回。

<resultMap>中 <collection>的使用

标签:nbsp   查询语句   man   entity   color   imp   问题   删除   内容   

原文地址:http://www.cnblogs.com/cc-freiheit/p/7467022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!