mybatis配置一对多map映射(基本数据类型)

场景

在查询用户的角色列表时,用户与角色是一对多的关系,查询的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标签的javaTypeofType,因为示例中的List元素是基本数据类型,所以使用ofType设置具体的基本数据类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 是一款支持自定义 SQL、存储过程以及高级映射的持久层框架。在 MyBatis 中,一对多关系可以通过两种方式来实现: 1. 嵌套查询 在嵌套查询中,我们可以在主查询中通过子查询来查询关联表的数据。在 MyBatis 中,我们可以使用 `resultMap` 来定义嵌套查询的结果映射。 例如,我们有两个表 `user` 和 `order`,一个用户可以拥有多个订单,那么我们可以使用以下 SQL 查询用户及其所有订单信息: ```sql SELECT * FROM user WHERE id = #{id}; SELECT * FROM order WHERE user_id = #{id}; ``` 然后,在 MyBatis 中,我们可以将上面的 SQL 语句分别定义为两个不同的 `select`,并在 `resultMap` 中引用: ```xml <resultMap id="userOrderMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <collection property="orders" ofType="Order"> <id property="id" column="id"/> <result property="orderName" column="order_name"/> <result property="price" column="price"/> </collection> </resultMap> <select id="getUserById" resultMap="userOrderMap"> SELECT * FROM user WHERE id = #{id} </select> <select id="getOrdersByUserId" resultType="Order"> SELECT * FROM order WHERE user_id = #{id} </select> ``` 注意,我们在 `resultMap` 中使用了 `collection` 标签来表示关联的多个对象,`ofType` 属性指明了关联对象的类型。 2. 嵌套结果 在嵌套结果中,我们可以在查询主表的同时,通过 `association` 标签来查询关联表的数据,并将其封装成对象。在 MyBatis 中,我们可以使用 `resultMap` 来定义嵌套结果的映射。 例如,我们有两个表 `user` 和 `role`,一个用户可以拥有多个角色,那么我们可以使用以下 SQL 查询用户及其所有角色信息: ```sql SELECT user.*, role.* FROM user LEFT JOIN user_role ON user.id = user_role.user_id LEFT JOIN role ON user_role.role_id = role.id WHERE user.id = #{id}; ``` 然后,在 MyBatis 中,我们可以将上面的 SQL 语句定义为一个 `select`,并在 `resultMap` 中引用: ```xml <resultMap id="userRoleMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <association property="roles" javaType="java.util.List" resultMap="roleMap"/> </resultMap> <resultMap id="roleMap" type="Role"> <id property="id" column="id"/> <result property="roleName" column="role_name"/> </resultMap> <select id="getUserById" resultMap="userRoleMap"> SELECT user.*, role.* FROM user LEFT JOIN user_role ON user.id = user_role.user_id LEFT JOIN role ON user_role.role_id = role.id WHERE user.id = #{id}; </select> ``` 注意,我们在 `resultMap` 中使用了 `association` 标签来表示关联的单个对象,`javaType` 属性指明了关联对象的类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值