一对一查询(多对一)
方式一:
1.定义账户信息实体类
public class Account implements Serializable { private Integer id; private Integer uid; private Double money; // ------------get和set方法------------ }
2.定义AccountUser类继承Account类,加入成员变量(所需要的用户信息)
public class AccountUser extends Account { private String username; private String address; // ------------get和set方法------------ }
3.定义账户的持久层 Dao 接口
public interface AccountDao { /** * 查询所有账户信息,以及账户所对应用户的信息 * @return */ List<AccountUser> findAll(); }
4.定义 AccountDao.xml 文件中的查询配置信息
<select id="findAll" resultType="accountUser"> select u.username, u.address, a.* from account a, user u where a.uid = u.id </select>
5.测试
@Test public void testfindAll() { List<AccountUser> list = accountDao.findAll(); for (AccountUser accountUser : list) { System.out.println(accountUser); } }
方式二:
1.定义账户信息实体类,加入 User 类的对象作为 Account 类的一个属性
public class Account implements Serializable { private Integer id; private Integer uid; private Double money; //从表实体应该包含一个主表实体的对象引用 private User user; // ------------get和set方法------------ }
public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; // ------------get和set方法------------ }
2.定义账户的持久层 Dao 接口
public interface AccountDao { /** * 查询所有账户信息,以及账户所对应用户的信息 * @return */ List<Account> findAll2(); }
3.定义 AccountDao.xml 文件中的查询配置信息
<!-- 定义封装account和user的resultMap --> <resultMap id="accountUserMap" type="account"> <id property="id" column="aid"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> <!-- 一对一的关系映射:配置封装user的内容 --> <association property="user" column="uid" javaType="user"> <id property="id" column="id"></id> <result property="username" column="username"></result> <result property="birthday" column="birthday"></result> <result property="sex" column="sex"></result> <result property="address" column="address"></result> </association> </resultMap> <select id="findAll2" resultMap="accountUserMap"> select u.*, a.id as aid, a.uid, a.money from account a, user u where a.uid = u.id </select>
4.测试
@Test public void testfindAll2() { List<Account> list = accountDao.findAll2(); for (Account account : list) { System.out.println(account); System.out.println(account.getUser()); } }
一对多查询
1.定义 User 实体类,加入 List<Account>,一个用户可以有多个账户
public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; //一对多关系映射:主表实体应该包含从表实体的集合引用 private List<Account> accounts; // ------------get和set方法------------ }
2.定义用户的持久层 Dao 接口
public interface UserDao { List<User> findAll(); }
3.用户持久层 Dao 映射文件配置
<!-- 定义User的resultMap --> <resultMap id="userAccountMap" type="user"> <id property="id" column="id"></id> <result property="username" column="username"></result> <result property="birthday" column="birthday"></result> <result property="sex" column="sex"></result> <result property="address" column="address"></result> <!-- 配置User对象中accounts集合的映射 --> <collection property="accounts" ofType="account"> <id property="id" column="aid"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> </collection> </resultMap> <select id="findAll" resultMap="userAccountMap"> SELECT u.*, a.id AS aid, a.uid, a.money FROM `user` u LEFT JOIN account a ON u.id = a.uid; </select>
collection 部分定义了用户关联的账户信息。表示关联查询结果集
property="accounts" :关联查询的结果集,存储在 User 对象的上那个属性。
ofType="account" :指定关联查询的结果集中的对象类型即List中的对象类型。在主配置文件已定义别名,此处可以使用别名,也可以使用全限定名。
4.测试
@Test public void testfindAll() { List<User> users = userDao.findAll(); for (User user : users) { System.out.println(user); for (Account account : user.getAccounts()) { System.out.println(account); } } }
多对多查询:可以看成双向的一对多查询
1.编写角色实体类和用户实体类
public class Role implements Serializable { private Integer roleId; private String roleName; private String roleDesc; //多对多的关系映射:一个角色可以赋予多个用户 private List<User> users; // ------------get和set方法------------ }
public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; //多对多的关系映射:一个用户可以具备多个角色 private List<Role> roles; // ------------get和set方法------------ }
2.编写 Role 持久层接口
public interface RoleDao { List<Role> findAll(); }
3.编写映射文件
<mapper namespace="com.fgy.dao.RoleDao"> <!-- 定义role表的resultMap --> <resultMap id="roleUserMap" type="role"> <id property="roleId" column="rid"></id> <result property="roleName" column="role_name"></result> <result property="roleDesc" column="role_desc"></result> <!-- 配置Role对象中users集合的映射 --> <collection property="users" ofType="user"> <id property="id" column="id"></id> <result property="username" column="username"></result> <result property="birthday" column="birthday"></result> <result property="address" column="address"></result> <result property="sex" column="sex"></result> </collection> </resultMap> <select id="findAll" resultMap="roleUserMap"> SELECT r.id AS rid, r.role_name, r.role_desc, u.* FROM role r LEFT JOIN user_role ur ON (ur.rid = r.id) LEFT JOIN `user` u ON (ur.uid = u.id); </select> </mapper>
4.测试
@Test public void testFindAll() { List<Role> list = roleDao.findAll(); for (Role role : list) { System.out.println(role); for (User user : role.getUsers()) { System.out.println(user); } } }