065-MyBatis

1.Mybatis多表查询

1.1 一对一查询

1.1.1 一对一查询的模型MapperScannerConfigurer

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

1.1.2 一对一查询的语句

对应的sql语句:select * from orders o,user u where o.uid=u.id;

查询的结果如下:

1.1.3 创建Order和User实体
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
1.1.4 创建OrderMapper接口
1
2
3
public interface OrderMapper {
List<Order> findAll();
}
1.1.5 配置OrderMapper.xml
1
2
3
4
5
6
7
8
9
10
11
<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>

其中还可以配置如下:

1
2
3
4
5
6
7
8
9
10
11
<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.1.6 测试结果
1
2
3
4
5
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
List<Order> all = mapper.findAll();
for(Order order : all){
System.out.println(order);
}

1.2 一对多查询

1.2.1 一对多查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户

一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单

1.2.2 一对多查询的语句

对应的sql语句:select *,o.id oid from user u left join orders o on u.id=o.uid;

查询的结果如下:

1.2.3 修改User实体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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; //代表当前用户具备哪些订单
}
1.2.4 创建UserMapper接口
1
2
3
public interface UserMapper {
List<User> findAll();
}
1.2.5 配置UserMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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.2.6 测试结果
1
2
3
4
5
6
7
8
9
10
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
for(User user : all){
System.out.println(user.getUsername());
List<Order> orderList = user.getOrderList();
for(Order order : orderList){
System.out.println(order);
}
System.out.println("----------------------------------");
}

1.3 多对多查询

1.3.1 多对多查询的模型

用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用

多对多查询的需求:查询用户同时查询出该用户的所有角色

1.3.2 多对多查询的语句

对应的sql语句: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;

查询的结果如下:

1.3.3 创建Role实体,修改User实体
1
2
3
4
5
6
7
8
9
10
11
12
13
public class User {
private int id;
private String username;
private String password;
private Date birthday;
private List<Order> orderList; //代表当前用户具备哪些订单
private List<Role> roleList; //代表当前用户具备哪些角色
}

public class Role {
private int id;
private String rolename;
}
1.3.4 添加UserMapper接口方法
1
List<User> findAllUserAndRole();
1.3.5 配置UserMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<mapper namespace="com.itheima.mapper.UserMapper">
<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>
</mapper>
1.3.6 测试结果
1
2
3
4
5
6
7
8
9
10
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAllUserAndRole();
for(User user : all){
System.out.println(user.getUsername());
List<Role> roleList = user.getRoleList();
for(Role role : roleList){
System.out.println(role);
}
System.out.println("----------------------------------");
}

1.4 知识小结

MyBatis多表配置方式:

  • 一对一配置:使用做配置
  • 一对多配置:使用+做配置
  • 多对多配置:使用+做配置

065-MyBatis
https://flepeng.github.io/021-Java-01-course-065-MyBatis/
作者
Lepeng
发布于
2020年2月2日
许可协议