Mybatis-Plus提供了注解方式进行多表查询
Mybatis-Plus提供了多种方式进行多表查询,其中注解方式是其中的一种。以下是几个使用注解方式进行多表查询的例子:
1.一对一查询
假设我们有两张表:user表和address表,每个用户对应一个地址,这是一个典型的一对一关系。我们可以使用注解方式进行一对一查询,如下所示:
-
-
public class User {
-
-
private Long id;
-
private String name;
-
private Integer age;
-
-
private Address address;
-
}
-
-
public class Address {
-
-
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表,一个用户可以有多个订单,这是一个典型的一对多关系。我们可以使用注解方式进行一对多查询,如下所示:
-
-
public class User {
-
-
private Long id;
-
private String name;
-
private Integer age;
-
-
private List<Order> orders;
-
}
-
-
public class Order {
-
-
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表,一个用户可以有多个角色,一个角色可以被多个用户拥有,这是一个典型的多对多关系。我们可以使用注解方式进行多对多查询,如下所示:
-
-
public class User {
-
-
private Long id;
-
private String name;
-
private Integer age;
-
-
private List<Role> roles;
-
}
-
-
public class Role {
-
-
private Long id;
-
private String name;
-
-
private List<User> users;
-
}
-
-
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语句来查询用户所拥有的角色。这种方式在数据量较小的情况下可以使用,但是对于数据量较大的情况下,可能会导致性能问题。因此,我们可以考虑使用中间表对象来进行多对多查询,如下所示:
-
-
public class User {
-
-
private Long id;
-
private String name;
-
private Integer age;
-
-
private List<UserRole> userRoles;
-
-
private List<Role> roles;
-
}
-
-
public class Role {
-
-
private Long id;
-
private String name;
-
-
private List<UserRole> userRoles;
-
-
private List<User> users;
-
}
-
-
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属性指定了查询对应数据的方法。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhaega
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24