Mybatis-Plus提供了注解方式进行多表查询

Mybatis-Plus提供了多种方式进行多表查询,其中注解方式是其中的一种。以下是几个使用注解方式进行多表查询的例子:

1.一对一查询

        假设我们有两张表:user表和address表,每个用户对应一个地址,这是一个典型的一对一关系。我们可以使用注解方式进行一对一查询,如下所示:

@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    @TableField(exist = false)
    private Address address;
}
@TableName("address")
public class Address {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String detail;
    private Long userId;
}

        我们在User类中添加了一个Address类型的字段,并使用@TableField(exist = false)注解标记该字段不是user表中的数据。然后我们可以使用@One注解指定该字段与address表中的数据对应:

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where id = #{id}")
    @Results({
        @Result(property = "address", column = "id",
            one = @One(select = "com.example.mapper.AddressMapper.selectByUserId"))
    })
    User selectById(@Param("id") Long id);
}
@Mapper
public interface AddressMapper extends BaseMapper<Address> {
    @Select("select * from address where user_id = #{userId}")
    Address selectByUserId(@Param("userId") Long userId);
}

        这里我们使用了@Results注解来指定对应关系,其中@One注解表示对应关系是一对一的,select属性指定了查询对应数据的方法。

2.一对多查询

        假设我们有两张表:user表和order表,一个用户可以有多个订单,这是一个典型的一对多关系。我们可以使用注解方式进行一对多查询,如下所示:

@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    @TableField(exist = false)
    private List<Order> orders;
}
@TableName("order")
public class Order {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private BigDecimal price;
    private Long userId;
}

        我们在User类中添加了一个List<Order>类型的字段,并使用@TableField(exist = false)注解标记该字段不是user表中的数据。然后我们可以使用@Many注解指定该字段与order表中的数据对应:

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where id = #{id}")
    @Results({
        @Result(property = "orders", column = "id",
            many = @Many(select = "com.example.mapper.OrderMapper.selectByUserId"))
    })
    User selectById(@Param("id") Long id);
}
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    @Select("select * from order where user_id = #{userId}")
    List<Order> selectByUserId(@Param("userId") Long userId);
}

        这里我们使用了@Results注解来指定对应关系,其中@Many注解表示对应关系是一对多的,select属性指定了查询对应数据的方法。

3.多对多查询

3.1多表查询(普通方法)

        假设我们有三张表:user表、role表和user_role表,一个用户可以有多个角色,一个角色可以被多个用户拥有,这是一个典型的多对多关系。我们可以使用注解方式进行多对多查询,如下所示:

@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    @TableField(exist = false)
    private List<Role> roles;
}
@TableName("role")
public class Role {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    @TableField(exist = false)
    private List<User> users;
}
@TableName("user_role")
public class UserRole {
    private Long userId;
    private Long roleId;
}

        我们在User类中添加了一个List<Role>类型的字段,并使用@TableField(exist = false)注解标记该字段不是user表中的数据;在Role类中添加了一个List<User>类型的字段,并使用@TableField(exist = false)注解标记该字段不是role表中的数据。然后我们可以使用@Many注解指定两个类之间的多对多关系:

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where id = #{id}")
    @Results({
        @Result(property = "roles", column = "id",
            many = @Many(select = "com.example.mapper.RoleMapper.selectByUserId"))
    })
    User selectById(@Param("id") Long id);
}
@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    @Select("select * from role where id in (select role_id from user_role where user_id = #{userId})")
    List<Role> selectByUserId(@Param("userId") Long userId);
}

        这里我们使用了@Results注解来指定对应关系,其中@Many注解表示对应关系是多对多的,select属性指定了查询对应数据的方法。

3.2多表查询(使用中间表对象)

        前面的例子中,我们使用了一条SQL语句来查询用户所拥有的角色。这种方式在数据量较小的情况下可以使用,但是对于数据量较大的情况下,可能会导致性能问题。因此,我们可以考虑使用中间表对象来进行多对多查询,如下所示:

@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    @TableField(exist = false)
    private List<UserRole> userRoles;
    @TableField(exist = false)
    private List<Role> roles;
}
@TableName("role")
public class Role {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    @TableField(exist = false)
    private List<UserRole> userRoles;
    @TableField(exist = false)
    private List<User> users;
}
@TableName("user_role")
public class UserRole {
    private Long id;
    private Long userId;
    private Long roleId;
}

        我们在User类和Role类中都添加了一个List<UserRole>类型的字段,并使用@TableField(exist = false)注解标记该字段不是user表或role表中的数据。然后我们可以使用@Many注解指定两个类之间的多对多关系:

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where id = #{id}")
    @Results({
        @Result(property = "userRoles", column = "id",
            many = @Many(select = "com.example.mapper.UserRoleMapper.selectByUserId")),
        @Result(property = "roles", column = "id",
            many = @Many(select = "com.example.mapper.RoleMapper.selectByUserId"))
    })
    User selectById(@Param("id") Long id);
}
@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    @Select("select * from role where id in (select role_id from user_role where user_id = #{userId})")
    List<Role> selectByUserId(@Param("userId") Long userId);
}
@Mapper
public interface UserRoleMapper extends BaseMapper<UserRole> {
    @Select("select * from user_role where user_id = #{userId}")
    List<UserRole> selectByUserId(@Param("userId") Long userId);
}

        这里我们使用了@Results注解来指定对应关系,其中@Many注解表示对应关系是多对多的,select属性指定了查询对应数据的方法。

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superboy@.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值