我有一张桌子t_comment和一张桌子t_comment_photo。
t_comment具有列ID,内容; t_comment_photo具有列ID,comment_id,photo_url。 t_comment_pohto包含评论照片的网址,一个评论可以包含0到许多评论照片。
这些是他们的java类:
public class ToyComment {
private Integer id;
private String content;
private List<String> urls;
...
}
public class CommentPhoto {
private Integer id;
private String commentId;
private String comment_url;
...
}
以下是映射器中的<select>
内容:
<select id="getToyCommentList"
resultType="com.domain.ToyComment">
c.id, c.content,p.user_photo_url userPhoto
from t_comment c left join t_comment_photo p
on c.id = p.comment_id
</select>
当我尝试将从表t_comment_photo查询的userPhoto映射到Java类ToyComment内的列表元素时,出现错误。
我要修复的resultMap是:
<resultMap id="ToyCommentResultMap" type="com.domain.ToyComment">
<result column="id" jdbcType="VARCHAR" property="id" />
<result column="content" jdbcType="VARCHAR" property="content" />
<!-- <result property="urls" jdbcType="VARCHAR" javaType="java.util.ArrayList" column="userPhoto" /> -->
<collection property="urls" javaType="list" jdbcType="VARCHAR" column="userPhoto"></collection>
</resultMap>
我尝试了<property>
和<collection>
,但是都没有用。
使用<collection>
时,出现“映射语句集合已经包含com.toy.mapper.ToyCommentMapper.getToyCommentListByToyId”的值,当我使用<result>
时,出现了“找不到用于属性userPhotos”。
为javaType使用“ java.util.ArrayList”或“ list”不会更改输出错误。
我试图搜索“ mybatis映射到字符串列表”,但是大多数都与映射到对象列表有关。
如何正确解决?
答案 0 :(得分:1)
结果图应如下所示:
<resultMap id="ToyCommentResultMap" type="com.domain.ToyComment">
<id column="id" property="id" />
<result column="content" property="content" />
<collection property="urls" javaType="list"
ofType="string">
<result column="userPhoto" />
</collection>
</resultMap>
一些注意事项:
<id />
时指定<collection />
。它可以提高效率,在某些情况下是必需的。<select />
中,您需要指定resultMap
而不是resultType
(显然)。