mybatis plus多表查询
时间: 2023-07-26 17:04:13 浏览: 88
MyBatis Plus支持多表查询,可以通过注解方式或XML方式进行配置。以下是一个简单的示例:
```java
@TableName("user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
@TableName("order")
public class Order {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private Long userId;
private String productName;
private Integer price;
}
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT u.*, o.product_name, o.price FROM user u LEFT JOIN order o ON u.id = o.user_id WHERE u.id = #{userId}")
UserWithOrder selectUserWithOrder(@Param("userId") Long userId);
}
public class UserWithOrder {
private User user;
private String productName;
private Integer price;
// getters and setters
}
```
在上面的示例中,通过注解方式将User和Order类与表进行映射,然后在UserMapper中定义了一个多表查询的方法selectUserWithOrder,该方法使用了LEFT JOIN方式连接了user表和order表,并将结果映射到UserWithOrder类中。通过这种方式,可以轻松实现多表查询。
阅读全文