mybatis配置一对多map映射(基本数据类型)
在查询用户的角色列表时,用户与角色是一对多的关系,查询的SQL结果集一般都是多条,我们定义的映射关系一般是角色是一个List。,因为示例中的List元素是基本数据类型,所以使用ofType设置具体的基本数据类型。
·
场景
在查询用户的角色列表时,用户与角色是一对多的关系,查询的SQL结果集一般都是多条,我们定义的映射关系一般是角色是一个List
解决方法
示例数据表
create table mall_admin_user_role
(
id bigint auto_increment
primary key,
uid bigint not null,
role_id int not null
);
示例实体
public class UserRoleModel {
private Long uid;
private List<Integer> roleIds;
}
示例map映射
<select id="getRolesByUid" resultMap="userRoleMap">
select * from mall_admin_user_role where uid=#{uid}
</select>
<resultMap id="userRoleMap" type="UserRoleModel">
<result property="uid" column="uid"/>
<collection property="roleIds" javaType="java.util.List" ofType="java.lang.Integer">
<result column="role_id" />
</collection>
</resultMap>
关键是collection标签的javaType和ofType,因为示例中的List元素是基本数据类型,所以使用ofType设置具体的基本数据类型
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)