Mybatis-Plus提供了多种方式进行多表查询,其中注解方式是其中的一种。以下是几个使用注解方式进行多表查询的例子:
假设我们有两张表: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
@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
@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
@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属性指定了查询对应数据的方法。
上一篇:【宝剑出鞘】第二式:公告发布项目
下一篇:时间差函数