select *,o.uid as UserID ,o.id as OrderID from user as u inner join orders as o on o.uid=u.id order by u.。Mybatis(三)|Mybatis(三) 一对一,一对多,多对多查询。" />

Mybatis(三)|Mybatis(三) 一对一,一对多,多对多查询

订单:

public class Order{ private int id; private Date ordertime; private User user; private int total;

="findAll" resultMap="orderMap"> select *,o.uid as UserID ,o.id as OrderID from user as u inner join orders as o on o.uid=u.id order by u.id

用户:
public class User { private int id; private String name; private String password; private List orderList;

="findAll" resultMap="userMap"> select * , u.id as UserID ,o.id as OrderID from user as u inner join orders as o on o.uid=u.id order by UserID

订单的Mapper查询结果为一对一,主要是在Order里如何配置一个User对象Mybatis(三)|Mybatis(三) 一对一,一对多,多对多查询
文章图片

用户的Mapper查询结果为一对多,主要是在User里如何配置一个Order的集合
Mybatis(三)|Mybatis(三) 一对一,一对多,多对多查询
文章图片

多对多==========
有三张表,user、role、role_user,查询的目的是吧每个人的职业都显示出来,所以需要在user里添加一个role的集合,配置文件操作与一对多相同,主要是 sql语句不同。
userMapper.xml
id="findUserAndRole" resultMap="userAndRoleMap"> select u.id as UserID,u.name,u.password,r.id as RoleID,r.roleName,r.roleDesc from user as u ,role as r,role_user as ru where u.id=ru.userid and ru.roleid=r.id order by UserID

【Mybatis(三)|Mybatis(三) 一对一,一对多,多对多查询】查询的结果为
Mybatis(三)|Mybatis(三) 一对一,一对多,多对多查询
文章图片

若要查询每个职业的人员,同理需建立RoleMapper.xml,以及在Role实体类中加入一个User的集合。

    推荐阅读