赞
踩
现在有question,tag,question_tag三张表,一个question对应多个tag标签,使用question_tag表将question和tag两个表进行关联。问题是:在调用mybatisplus的service的方法时,默认只会查询question信息,如何查询question的时候将它的tag信息一起查询出来呢?
@TableName(value ="question",resultMap = "BaseResultMap")
@Data
public class Question implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
private String title;
private String content;
private Integer score;
@TableField(exist = false)
private ArrayList<Tag> tags;
}
<resultMap id="BaseResultMap" type="com.sundae.questionserver.entity.Question"> <id property="id" column="id" jdbcType="INTEGER"/> <result property="title" column="title" jdbcType="VARCHAR"/> <result property="content" column="content" jdbcType="VARCHAR"/> <result property="score" column="score" jdbcType="INTEGER"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> <result property="createUserId" column="create_user_id" jdbcType="VARCHAR"/> <collection property="tags" column="{question_id=id}" select="getTagsByQuestionId"></collection> </resultMap> <!-- 查询tag的sql语句,根据question的id查询出该question对应的tag --> <select id="getTagsByQuestionId" resultType="com.sundae.questionserver.entity.Tag"> select t.id,t.tag_name from tag t where id in ( select qt.tag_id from question_tag qt where qt.question_id = #{question_id} ) </select>
这样使用QuestionService,public interface QuestionService extends IService 这个类查询出来的question都默认添加有tags字段
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。