Mybatis多表查询
Mybatis多表查询,针对复杂的Sql语句,因涉及到多张表的操作,建议在mapper.xml 文件中进行编写Sql语句,方便查看分析。
此篇笔记是基于案例进行学习,只是侧重于实体类中成员变量怎样定义、xml文件中代码实现思路。对于接口的编写此处省略。
对于数据库中表之间的关系有一对一、一对多、多对多。
针对于多表查询,依据表的关系,可分为一对一查询、一对多查询、多对多查询
文章目录
一、一对一查询
案例:查询一个订单,与此同时查询出该订单所属的用户
分析:用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
1. 实体类中添加User类型的成员变量
创建Order和User实体类,在Order类中添加User成员变量
public class Order {
private int id;
private Date ordertime;
private double total;
// 代表当前订单从属于哪一个客户
private User user;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
}
2. 配置OrderMapper.xml文件
对于pojo实体类对象,有以下两种方式配置< resultMap>
- 方法一: property=“user.username”
<mapper namespace="com.itheima.mapper.OrderMapper">
<resultMap id="orderMap" type="com.itheima.domain.Order">
<result column="uid" property="user.id"></result>
<result column="username" property="user.username"></result>
<result column="password" property="user.password"></result>
<result column="birthday" property="user.birthday"></result>
</resultMap>
<select id="findAll" resultMap="orderMap">
select * from orders o,user u where o.uid=u.id
</select>
</mapper>
- 方法二:使用 < association >标签
<resultMap id="orderMap" type="com.itheima.domain.Order">
<result property="id" column="id"></result>
<result property="ordertime" column="ordertime"></result>
<result property="total" column="total"></result>
<association property="user" javaType="com.itheima.domain.User">
<result column="uid" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
二、一对多查询
案例:查询一个用户,与此同时查询出该用户具有的订单。
分析:用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
1. 实体类中添加List< Order> 成员变量
修改User类
public class Order {
private int id;
private Date ordertime;
private double total;
// 代表当前订单从属于哪一个客户
private User user;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
// 代表当前用户具备哪些订单
private List<Order> orderList;
}
2. 配置UserMapper.xml文件
<mapper namespace="com.itheima.mapper.UserMapper">
<resultMap id="userMap" type="com.itheima.domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="orderList" ofType="com.itheima.domain.Order">
<result column="oid" property="id"></result>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
select *,o.id oid from user u left join orders o on u.id=o.uid
</select>
</mapper>
三、多对多查询
案例:查询用户同时查询出该用户的所有角色
分析:用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询出该用户的所有角色
1. 实体类中添加 List< Role> 成员变量
创建Role实体,修改User实体
public class Role {
private int id;
private String rolename;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
// 代表当前用户具备哪些订单
private List<Order> orderList;
// 代表当前用户具备哪些角色
private List<Role> roleList;
}
2. 配置UserMapper.xml文件
<resultMap id="userRoleMap" type="com.itheima.domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="roleList" ofType="com.itheima.domain.Role">
<result column="rid" property="id"></result>
<result column="rolename" property="rolename"></result>
</collection>
</resultMap>
<select id="findAllUserAndRole" resultMap="userRoleMap">
select u.*,r.*,r.id rid from user u left join user_role ur on
u.id=ur.user_id
inner join role r on ur.role_id=r.id
</select>
总结:
MyBatis多表配置方式:
查询关系 | 配置方式 |
---|---|
一对一 | 使用 < resultMap> 做配置 |
一对多 | 使用 < resultMap> + < collection> 做配置 |
多对多 | 使用 < resultMap> + < collection> 做配置 |