父评论(表名:comment)和子评论(表名:comment_child)分别用到两张表,但是因为建表设置的两个表的id(回复id)和content(回复文本),导致查询出来的列表,子评论是父评论的数据
这就需要更改别名了。
1.子评论的表 id,content分别在column设置别名child_id,child_content
<!-- !!! !特别注意,因为二级评论id,content和一级评论id,content一样,所以要改数据库别名(column)为child_id,child_content -->
<collection property="commentChild" ofType="com.fgh.spring.generator.CommentChild">
<id column="child_id" jdbcType="INTEGER" property="id"/>
<result column="p_id" jdbcType="INTEGER" property="pId"/>
<result column="child_content" jdbcType="VARCHAR" property="content"/>
</collection>
2.查询的时候通过别名去查
<!-- 返回子评论 注意,因为两个表的id,content一样,所以要修改别名请看清下面的区别,否则导致返回的子评论是父评论-->
<sql id="Child_Column_List">
comment.id , comment.content ,
comment_child.id child_id,
comment_child.content child_content
</sql>
<select id="selectByOidAndType" parameterType="map" resultMap="BaseResultMap">
select
<include refid="Child_Column_List"/>
from comment
left join comment_child
on comment.id = comment_child.p_id
</select>