• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
superboy@.
帮助1

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

1.一对一查询

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

  1.  
    @TableName("user")
  2.  
    public class User {
  3.  
    @TableId(type = IdType.AUTO)
  4.  
    private Long id;
  5.  
    private String name;
  6.  
    private Integer age;
  7.  
    @TableField(exist = false)
  8.  
    private Address address;
  9.  
    }
  10.  
    @TableName("address")
  11.  
    public class Address {
  12.  
    @TableId(type = IdType.AUTO)
  13.  
    private Long id;
  14.  
    private String detail;
  15.  
    private Long userId;
  16.  
    }

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

  1.  
    @Mapper
  2.  
    public interface UserMapper extends BaseMapper<User> {
  3.  
    @Select("select * from user where id = #{id}")
  4.  
    @Results({
  5.  
    @Result(property = "address", column = "id",
  6.  
    one = @One(select = "com.example.mapper.AddressMapper.selectByUserId"))
  7.  
    })
  8.  
    User selectById(@Param("id") Long id);
  9.  
    }
  10.  
    @Mapper
  11.  
    public interface AddressMapper extends BaseMapper<Address> {
  12.  
    @Select("select * from address where user_id = #{userId}")
  13.  
    Address selectByUserId(@Param("userId") Long userId);
  14.  
    }

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

2.一对多查询

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

  1.  
    @TableName("user")
  2.  
    public class User {
  3.  
    @TableId(type = IdType.AUTO)
  4.  
    private Long id;
  5.  
    private String name;
  6.  
    private Integer age;
  7.  
    @TableField(exist = false)
  8.  
    private List<Order> orders;
  9.  
    }
  10.  
    @TableName("order")
  11.  
    public class Order {
  12.  
    @TableId(type = IdType.AUTO)
  13.  
    private Long id;
  14.  
    private String name;
  15.  
    private BigDecimal price;
  16.  
    private Long userId;
  17.  
    }

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

  1.  
    @Mapper
  2.  
    public interface UserMapper extends BaseMapper<User> {
  3.  
    @Select("select * from user where id = #{id}")
  4.  
    @Results({
  5.  
    @Result(property = "orders", column = "id",
  6.  
    many = @Many(select = "com.example.mapper.OrderMapper.selectByUserId"))
  7.  
    })
  8.  
    User selectById(@Param("id") Long id);
  9.  
    }
  10.  
    @Mapper
  11.  
    public interface OrderMapper extends BaseMapper<Order> {
  12.  
    @Select("select * from order where user_id = #{userId}")
  13.  
    List<Order> selectByUserId(@Param("userId") Long userId);
  14.  
    }

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

3.多对多查询

3.1多表查询(普通方法)

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

  1.  
    @TableName("user")
  2.  
    public class User {
  3.  
    @TableId(type = IdType.AUTO)
  4.  
    private Long id;
  5.  
    private String name;
  6.  
    private Integer age;
  7.  
    @TableField(exist = false)
  8.  
    private List<Role> roles;
  9.  
    }
  10.  
    @TableName("role")
  11.  
    public class Role {
  12.  
    @TableId(type = IdType.AUTO)
  13.  
    private Long id;
  14.  
    private String name;
  15.  
    @TableField(exist = false)
  16.  
    private List<User> users;
  17.  
    }
  18.  
    @TableName("user_role")
  19.  
    public class UserRole {
  20.  
    private Long userId;
  21.  
    private Long roleId;
  22.  
    }

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

  1.  
    @Mapper
  2.  
    public interface UserMapper extends BaseMapper<User> {
  3.  
    @Select("select * from user where id = #{id}")
  4.  
    @Results({
  5.  
    @Result(property = "roles", column = "id",
  6.  
    many = @Many(select = "com.example.mapper.RoleMapper.selectByUserId"))
  7.  
    })
  8.  
    User selectById(@Param("id") Long id);
  9.  
    }
  10.  
    @Mapper
  11.  
    public interface RoleMapper extends BaseMapper<Role> {
  12.  
    @Select("select * from role where id in (select role_id from user_role where user_id = #{userId})")
  13.  
    List<Role> selectByUserId(@Param("userId") Long userId);
  14.  
    }

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

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

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

  1.  
    @TableName("user")
  2.  
    public class User {
  3.  
    @TableId(type = IdType.AUTO)
  4.  
    private Long id;
  5.  
    private String name;
  6.  
    private Integer age;
  7.  
    @TableField(exist = false)
  8.  
    private List<UserRole> userRoles;
  9.  
    @TableField(exist = false)
  10.  
    private List<Role> roles;
  11.  
    }
  12.  
    @TableName("role")
  13.  
    public class Role {
  14.  
    @TableId(type = IdType.AUTO)
  15.  
    private Long id;
  16.  
    private String name;
  17.  
    @TableField(exist = false)
  18.  
    private List<UserRole> userRoles;
  19.  
    @TableField(exist = false)
  20.  
    private List<User> users;
  21.  
    }
  22.  
    @TableName("user_role")
  23.  
    public class UserRole {
  24.  
    private Long id;
  25.  
    private Long userId;
  26.  
    private Long roleId;
  27.  
    }

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

  1.  
    @Mapper
  2.  
    public interface UserMapper extends BaseMapper<User> {
  3.  
    @Select("select * from user where id = #{id}")
  4.  
    @Results({
  5.  
    @Result(property = "userRoles", column = "id",
  6.  
    many = @Many(select = "com.example.mapper.UserRoleMapper.selectByUserId")),
  7.  
    @Result(property = "roles", column = "id",
  8.  
    many = @Many(select = "com.example.mapper.RoleMapper.selectByUserId"))
  9.  
    })
  10.  
    User selectById(@Param("id") Long id);
  11.  
    }
  12.  
    @Mapper
  13.  
    public interface RoleMapper extends BaseMapper<Role> {
  14.  
    @Select("select * from role where id in (select role_id from user_role where user_id = #{userId})")
  15.  
    List<Role> selectByUserId(@Param("userId") Long userId);
  16.  
    }
  17.  
    @Mapper
  18.  
    public interface UserRoleMapper extends BaseMapper<UserRole> {
  19.  
    @Select("select * from user_role where user_id = #{userId}")
  20.  
    List<UserRole> selectByUserId(@Param("userId") Long userId);
  21.  
    }

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhaega
系列文章
更多 icon
同类精品
更多 icon
继续加载