多表情况适用:
使用resultMap,即可将数据库查询的list转换为单个实体,自动聚合相应的字段到list中;
collection标签中包含两个关于javaBean的Type属性分别是ofType和javaType。其中ofType指定的这个List所存放的javaBean的类型,而javaType指定的当前这个配置的标签所对应的属性
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<resultMap type="xxx.User" id="userResult"> <result property="id" column="id"/> <!--可以将groupId字段映射到User实体 List<String> groupIds上--> <collection property="groupIds" ofType="String" > <result column="groupId"/> </collection> <!--可以将address_code/detail字段映射到实体Address中,然后整体映射到User中List<Address> addresses上输出为一个User或List<User>--> <collection property="addresses" resultMap="address"/> </resultMap> <resultMap id="address" type="xxx.Address"> <result property="adressCode" column="address_code"/> <result property="detail" column="detail"/> </resultMap>
最后
以上就是大意秀发最近收集整理的关于mybatis查询返回单个实体,实体含有集合元素的全部内容,更多相关mybatis查询返回单个实体内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复