https://www.bilibili.com/video/av47952553 p48~56
表之间的几种关系:一对一、一对多、多对一、多对多
MyBatis把多对一看成一对一
示例:用户和账户 一个用户可以有多个账户
一个账户只能属于一个用户
步骤:
两张表(账户表 外键)
两个实体类(体现一对多的关系)
两个配置文件
实现配置
查用户的所有账户
查账户的用户信息
两张表 user表
id
username
address
sex
birthday
account表
id
uid
money
1
46
1000
2
45
1000
3
46
2000
外键uid,引用user表的id
两个实体类 User 1 2 3 4 5 6 7 8 9 10 11 12 13 public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; private List<Account> accounts; }
1 2 3 4 5 6 7 8 9 public interface IUserDao { List<User> findAll () ; User findById (Integer userId) ; }
Account 1 2 3 4 5 6 7 8 9 10 11 public class Account implements Serializable { private Integer id; private Integer uid; private Double money; private User user; }
1 2 3 4 5 6 7 8 9 public interface IAccountDao { List<Account> findAll () ; List<AccountUser> findAllAccount () ; }
一对一查询 查询所有账户,和账户对应的用户信息
方法一 新建一个AccountUser类,保存用户信息
1 2 3 4 5 6 7 8 public class AccountUser extends Account { private String username; private String address; }
IAccountDao.xml中
1 2 3 <select id ="findAllAccount" resultType ="accountuser" > select a.*,u.username,u.address from account a , user u where u.id = a.uid; </select >
这种写一个子类的方式实际用得不多
方法二 更常用 -> Account中User对象引用
配置IAccountDao.xml时使用association标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <resultMap id ="accountUserMap" type ="account" > <id property ="id" column ="aid" > </id > <result property ="uid" column ="uid" > </result > <result property ="money" column ="money" > </result > <association property ="user" column ="uid" javaType ="user" > <id property ="id" column ="id" > </id > <result column ="username" property ="username" > </result > <result column ="address" property ="address" > </result > <result column ="sex" property ="sex" > </result > <result column ="birthday" property ="birthday" > </result > </association > </resultMap > <select id ="findAll" resultMap ="accountUserMap" > select u.*,a.id as aid,a.uid,a.money from account a , user u where u.id = a.uid; </select >
一对多查询 查询所有用户,和用户下所有账户的信息
IUserDao.xml中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <resultMap id ="userAccountMap" type ="user" > <id property ="id" column ="id" > </id > <result property ="username" column ="username" > </result > <result property ="address" column ="address" > </result > <result property ="sex" column ="sex" > </result > <result property ="birthday" column ="birthday" > </result > <collection property ="accounts" ofType ="account" > <id column ="aid" property ="id" > </id > <result column ="uid" property ="uid" > </result > <result column ="money" property ="money" > </result > </collection > </resultMap > <select id ="findAll" resultMap ="userAccountMap" > select * from user u left outer join account a on u.id = a.uid </select >
不能再用inner join(,),改成left outer join
多对多查询(用户-角色) 步骤:
三张表(用户表 角色表 还需使用中间表)
两个实体类(体现多对多的关系)
两个配置文件
实现配置
查询用户&用户的角色信息
查询角色&角色的用户信息
三张表 用户表
id
username
address
sex
birthday
角色表
id
role_name
role_desc
1
院长
管理整个学院
2
总裁
管理整个公司
3
校长
管理整个学校
中间表
两个实体类 User 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; private List<Role> roles; }
1 2 3 4 5 6 7 8 public interface IUserDao { List<User> findAll () ; User findById (Integer userId) ; }
Role 1 2 3 4 5 6 7 8 9 10 11 12 public class Role implements Serializable { private Integer roleId; private String roleName; private String roleDesc; private List<User> users; }
1 2 3 4 5 6 public interface IRoleDao { List<Role> findAll () ; }
两个配置文件 User 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <resultMap id ="userMap" type ="user" > <id property ="id" column ="id" > </id > <result property ="username" column ="username" > </result > <result property ="address" column ="address" > </result > <result property ="sex" column ="sex" > </result > <result property ="birthday" column ="birthday" > </result > <collection property ="roles" ofType ="role" > <id property ="roleId" column ="rid" > </id > <result property ="roleName" column ="role_name" > </result > <result property ="roleDesc" column ="role_desc" > </result > </collection > </resultMap > <select id ="findAll" resultMap ="userMap" > select u.*,r.id as rid,r.role_name,r.role_desc from user u left outer join user_role ur on u.id = ur.uid left outer join role r on r.id = ur.rid </select > <select id ="findById" parameterType ="INT" resultType ="user" > select * from user where id = #{uid} </select >
Role 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <resultMap id ="roleMap" type ="role" > <id property ="roleId" column ="rid" > </id > <result property ="roleName" column ="role_name" > </result > <result property ="roleDesc" column ="role_desc" > </result > <collection property ="users" ofType ="user" > <id column ="id" property ="id" > </id > <result column ="username" property ="username" > </result > <result column ="address" property ="address" > </result > <result column ="sex" property ="sex" > </result > <result column ="birthday" property ="birthday" > </result > </collection > </resultMap > <select id ="findAll" resultMap ="roleMap" > select u.*,r.id as rid,r.role_name,r.role_desc from role r left outer join user_role ur on r.id = ur.rid left outer join user u on u.id = ur.uid </select >
长语句换行的时候注意加空格,防止r left变成rleft