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>
正确返回。
时间: 2024-10-05 19:37:09