Mybatis-Plus提供了注解方式进行多表查询
迪丽瓦拉
2025-06-01 10:56:50
0

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 {@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
{@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 orders;
}
@TableName("order")
public class Order {@TableId(type = IdType.AUTO)private Long id;private String name;private BigDecimal price;private Long userId;
}

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

@Mapper
public interface UserMapper extends BaseMapper {@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 {@Select("select * from order where user_id = #{userId}")List 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 roles;
}
@TableName("role")
public class Role {@TableId(type = IdType.AUTO)private Long id;private String name;@TableField(exist = false)private List users;
}
@TableName("user_role")
public class UserRole {private Long userId;private Long roleId;
}

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

@Mapper
public interface UserMapper extends BaseMapper {@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 {@Select("select * from role where id in (select role_id from user_role where user_id = #{userId})")List 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 userRoles;@TableField(exist = false)private List roles;
}
@TableName("role")
public class Role {@TableId(type = IdType.AUTO)private Long id;private String name;@TableField(exist = false)private List userRoles;@TableField(exist = false)private List users;
}
@TableName("user_role")
public class UserRole {private Long id;private Long userId;private Long roleId;
}

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

@Mapper
public interface UserMapper extends BaseMapper {@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 {@Select("select * from role where id in (select role_id from user_role where user_id = #{userId})")List selectByUserId(@Param("userId") Long userId);
}
@Mapper
public interface UserRoleMapper extends BaseMapper {@Select("select * from user_role where user_id = #{userId}")List selectByUserId(@Param("userId") Long userId);
}

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

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
A.机器学习入门算法(三):基... 机器学习算法(三):K近邻(k-nearest neigh...
数字温湿度传感器DHT11模块... 模块实例https://blog.csdn.net/qq_38393591/article/deta...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
Redis 所有支持的数据结构... Redis 是一种开源的基于键值对存储的 NoSQL 数据库,支持多种数据结构。以下是...
win下pytorch安装—c... 安装目录一、cuda安装1.1、cuda版本选择1.2、下载安装二、cudnn安装三、pytorch...
MySQL基础-多表查询 文章目录MySQL基础-多表查询一、案例及引入1、基础概念2、笛卡尔积的理解二、多表查询的分类1、等...
keil调试专题篇 调试的前提是需要连接调试器比如STLINK。 然后点击菜单或者快捷图标均可进入调试模式。 如果前面...
MATLAB | 全网最详细网... 一篇超超超长,超超超全面网络图绘制教程,本篇基本能讲清楚所有绘制要点&#...