1. 一对一查询
1.1 一对一查询的模型
- 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
- 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
1.2 一对一查询的语句
- 对应的sql语句:
select * from orders o,user u where o.uid=u.id;
查询的结果如下:
1.3 创建Order和User实体
public class User {
private int id;
private String username;
private String password;
private Date birthday;
}
public class Order {
private int id;
private Date ordertime;
private double total;
//当前订单属于哪一个用户
private User user;
}
1.4 创建OrderMapper接口
public interface OrderMapper {
//查询全部的方法
public List<Order> findAll();
}
1.5 配置OrderMapper.xml
<mapper namespace="com.xdr630.mapper.OrderMapper">
<resultMap id="orderMap" type="order">
<!--手动指定字段和实体属性的映射关系
column:数据表的字段名称
property:实体的属性名称
-->
<id column="oid" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<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>
- 其中
<resultMap>
还可以配置如下:
<resultMap id="orderMap" type="order">
<!--手动指定字段和实体属性的映射关系
column:数据表的字段名称
property:实体的属性名称
-->
<id column="oid" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<!--<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>-->
<!--
property:当前实体(order)中的属性名称(private User user)
javaType:当前实体(order)中的属性名称(User)
-->
<association property="user" javaType="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
1.6 查询结果
2. 一对多查询
2.1 一对多查询的模型
- 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
- 一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
2.2 一对多查询的语句
-
对应的sql语句:
SELECT *, o.id oid FROM user u, orders o WHERE u.id=o.id
查询的结果如下: -
查看一个用户对应的所有订单信息
2.3 修改User实体
public class User {
private int id;
private String username;
private String password;
private Date birthday;
//描述当前用户存在哪些订单
private List<Order> orderList;
}
2.4 创建UserMapper接口
public interface UserMapper {
public List<User> findAll();
}
2.5 配置UserMapper.xml
<mapper namespace="com.xdr630.mapper.UserMapper">
<resultMap id="userMap" type="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--配置集合信息
property:集合名称
ofType:当前集合中的数据类型
-->
<collection property="orderList" ofType="order">
<!--封装order的数据-->
<id column="oid" property="id"></id>
<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, orders o WHERE u.id=o.id
</select>
</mapper>
2.6 测试结果
public void test2() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.findAll();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
- 可以看到每个用户对应的订单详情
3. 多对多查询
3.1 多对多查询的模型
- 用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
- 多对多查询的需求:查询用户同时查询出该用户的所有角色
3.2 多对多查询的语句
SELECT *
FROM user u, sys_user_role ur, sys_role r
WHERE u.id=ur.userId AND ur.roleId=r.id
3.3 创建Role实体,修改User实体
public class Role {
private int id;
private String roleName;
private String roleDesc;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
//描述的是当前用户具备哪些角色
private List<Role> roleList;
}
3.4 添加UserMapper接口方法
public interface UserMapper {
public List<User> findUserAndRoleAll();
}
3.5 配置UserMapper.xml
<resultMap id="userRoleMap" type="user">
<!--user的信息-->
<id column="userId" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--user内部的roleList信息-->
<collection property="roleList" ofType="role">
<id column="roleId" property="id"></id>
<result column="roleName" property="roleName"></result>
<result column="roleDesc" property="roleDesc"></result>
</collection>
</resultMap>
<select id="findUserAndRoleAll" resultMap="userRoleMap">
SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
</select>
3.6 测试结果
public void test4() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userAndRoleAll = mapper.findUserAndRoleAll();
for (User user : userAndRoleAll) {
System.out.println(user);
}
sqlSession.close();
}
4. 知识小结
MyBatis多表配置方式:
- 一对一配置:使用
<resultMap>
做配置 - 一对多配置:使用
<resultMap>+<collection>
做配置 - 多对多配置:使用
<resultMap>+<collection>
做配置