Mybatis如何将另一个表中的列映射到字符串列表? [英] Mybatis How to map a column from another table to a String list?
问题描述
我有一张桌子t_comment和一张桌子t_comment_photo.
I have a table t_comment, and a table t_comment_photo.
t_comment具有列ID,内容; t_comment_photo具有列ID,comment_id,photo_url. t_comment_pohto包含评论照片的网址,一个评论可以包含0到许多评论照片.
t_comment has column id, content; t_comment_photo has column id, comment_id, photo_url. t_comment_pohto contains the url of comment photos, a comment can have 0 to many comment photos.
这些是他们的java类:
These are their java classes:
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内的列表元素时,出现错误.
When I try to map the userPhoto queried from table t_comment_photo to a element of the list inside the java class ToyComment, I am getting errors.
我要修复的resultMap是:
The resultMap I am trying to fix is:
<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>
,但是都没有用.
I tried both <property>
and <collection>
, but neither are working.
使用<collection>
时,出现映射语句集合已经包含com.toy.mapper.ToyCommentMapper.getToyCommentListByToyId的值"错误,当我使用<result>
时,出现未为属性userPhotos找到类型处理程序".
为javaType使用"java.util.ArrayList"或"list"不会更改输出错误.
When I used <collection>
, I got "Mapped Statements collection already contains value for com.toy.mapper.ToyCommentMapper.getToyCommentListByToyId" error, when I used <result>
, I got "No typehandler found for property userPhotos".
Use "java.util.ArrayList" or "list" for javaType doesn't change the output error.
我尝试搜索"mybatis映射到字符串列表",但是大多数都与映射到对象列表有关.
I tried to search for "mybatis map to string list", but most are about mapping to a list of objects.
如何正确解决?
推荐答案
结果图应如下所示:
<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>
一些注意事项:
- 使用
<collection />
时指定<id />
.它可以提高效率,在某些情况下是必需的. - 在
<select />
中,您需要指定resultMap
而不是resultType
(显然). - 这是一个棘手的案例,我们添加了
- Specify
<id />
when using<collection />
. It improves efficiency and is required in some cases. - In
<select />
, you need to specifyresultMap
instead ofresultType
(obviously). - This is a tricky case and we have added an FAQ entry just recently.
这篇关于Mybatis如何将另一个表中的列映射到字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!