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属性指定了查询对应数据的方法。

相关内容

热门资讯

江畔开演,嗦粉迎客!四川南充国... 封面新闻记者 刘彦君10 月 7 日,四川南充国庆假期文旅成绩单正式出炉:全市A级景区接待游客322...
国庆外骨骼助力器销售额涨12.... 【文/王力 编辑/吕栋】国庆假期,爬山这件事开始有了点“开外挂”的味道。过去登山装备是登山杖、护膝和...
这个假期,天衢新区活力拉满! 德百奥莱广场上,大型实景演出《德运星河》再度精彩上演;董子文化街里,祭董大典庄严肃穆,文化市集青春洋...
走进印尼帕达尔岛 俯瞰蓝色海洋   印尼科莫多国家公园的核心岛屿——帕达尔岛由古老火山喷发形成,登顶观景台,四片海湾同时铺展在眼前,...
美丽中国行·大河长歌|一河穿沙...   黄河自黑山峡奔涌而入,从甘肃进入宁夏首站中卫,在沙坡头,与腾格里沙漠迎面相拥,形成沙水交融、河漠...
来上海别只吃小笼包了!新场古镇... 在上海市浦东新区,有一座被称为“小小新场赛苏州”的古镇,你知道是哪一座古镇吗?它北靠上海迪士尼度假区...
双节文旅收官盘点:古韵山西破壁... 本报(chinatimes.net.cn)记者赵文娟 平遥、忻州、太原报道2026年中秋国庆双节叠加...
“双节”期间 河南全省博物馆接... 来源:中国新闻网中新网郑州10月8日电(记者 韩章云)10月8日,记者从河南省文物局获悉,中秋国 庆...
外媒关注国庆假期:消费市场持续... 来源:中国新闻网中新网10月7日电10月7日是2026年国庆假期的最后一天。假期旅游热潮吸引国际媒体...
清镇国庆文旅收官:18场活动、... 2026年国庆期间,清镇市推出18项重点活动,涵盖主题乐园、商业综合体、民俗文化、体育赛事、亲子研学...